<file_sep>#
# This is the server logic of a Shiny web application. You can run the 
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
# 
#    http://shiny.rstudio.com/
#

library(shiny)
library(lmtest)
library(ggplot2)
library(plotly)
library(rpart)
library(rpart.plot)
library(readr)


# Define server logic required to plot
shinyServer(function(input, output) {
  
  output$distPlot <- renderPlot({
    
    birthwt <- as.data.frame(birthwt)
    birthwt_e <- as.data.frame(birthwt[ , 1:9])
    
    birthwt_tree <- rpart(low ~ . ,data = birthwt_e, method = 'class', cp = input$cp)
    rpart.plot(birthwt_tree, type = 1, extra = 1)
    
    
  })
  
  output$distPlot_2 <- renderPlot({
    birthwt_tree <- rpart(low ~ . ,data = birthwt_e, method = 'class', cp = input$cp)
    plotcp(birthwt_tree)
  })

})

<file_sep>
# Predicting mothers with pregnancy giving rise to babies with low birthweight

library(lmtest)
library(ggplot2)
library(plotly)
library(rpart)
library(rpart.plot)
library(readr)


View(birthwt)
birthwt <- as.data.frame(birthwt)
summary(birthwt)



birthwt_e <- as.data.frame(birthwt[ , 1:9])
View(birthwt_e)

# Build tree
birthwt_tree <- rpart(low ~ . ,data = birthwt_e, method = 'class', cp = 0.013)
rpart.plot(birthwt_tree, type = 1, extra = 1)
plotcp(birthwt_tree)

# ^ important parameters are 4: ptl, lwt , ht, age 


hist(birthwt$bwt)


############## Using low as the binary variable

birthwt_tree <- rpart(low ~ . ,data = birthwt_e, method = 'class', cp = 0.013)
rpart.plot(birthwt_tree, type = 1, extra = 1)

birthwt_tree <- rpart(low ~ . ,data = birthwt_e, method = 'class', cp = 0.001)
rpart.plot(birthwt_tree, type = 1, extra = 1)

# ^ no difference when decrease cp

birthwt_tree <- rpart(low ~ . ,data = birthwt_e, method = 'class', cp = 0.05)
rpart.plot(birthwt_tree, type = 1, extra = 1)

# Increase cp, important parameters: 3 : ptl, lwt, age



############## Using ftv as the binary variable
 
 
birthwt_e_ftv <- as.data.frame(birthwt_e)
View(birthwt_e_ftv)

for(i in 1:nrow(birthwt_e_ftv)){
  if(birthwt_e_ftv$ftv[i] == 0 || birthwt_e_ftv$ftv[i] == 1 || birthwt_e_ftv$ftv[i] == 2) {birthwt_e_ftv$ftv_bin[i] <- 0} 
  else {birthwt_e_ftv$ftv_bin[i] <- 1}
}

View(birthwt_e_ftv)
summary(birthwt_e_ftv)

birthwt_ftv_tree <- rpart(ftv_bin ~ ui + ht + smoke, data = birthwt_e_ftv, method = 'class', cp = 0.05)
rpart.plot(birthwt_ftv_tree, type = 1, extra = 1)

## No correleation for above with ftv_bin

birthwt_ftv_tree <- rpart(ui ~ ., data = birthwt_e_ftv, method = 'class', cp = 0.013)
rpart.plot(birthwt_ftv_tree, type = 1, extra = 1)
# ^ important: lwt, ptl, age


 
############## Using low as the binary variable 
birthwt_logit <- glm(low ~ ptl + lwt + ht + age , data = birthwt_e, family = 'binomial')
summary(birthwt_logit)
exp(birthwt_logit$coefficients)


############## Using ftv as the binary variable

birthwt_logit <- glm(ftv_bin ~ ui + ht + smoke, data = birthwt_e_ftv, family = 'binomial')
summary(birthwt_logit)
# implies not significant 






<file_sep>#
# This is the user-interface definition of a Shiny web application. You can
# run the application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
# 
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that plots
shinyUI(fluidPage(
  
  # Application title
  titlePanel("My Tree"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("cp",
                  "Select cp:",
                  min = 0,
                  max = 0.05,
                  value = 0.013)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  ) ,
  
  mainPanel(" ###### Ptl = Number of premature labour 
            ###### lwt = Mother's weight in pounds at last menstrual period
            ###### age = Mother's age in years
            ###### ht = History of hypertension
            ###### This tree showcases the classifications for someone to give birth to a baby whose weight is less than 2.5kg.
            Depending on the cp level chosen, there are generally either 3 (ptl, lwt, age) or 4 (ptl, lwt, ht, age) attributes which influence the baby's weight"), 
  
  plotOutput("distPlot_2")
  
))

<file_sep>
### R Shiny Dashboard

**Intro**  There are many factors that influence pregnancy and sometimes the babies are born healthy, sometimes they are not (<2.5kg or <5.5 pounds). 

**Objective:** Create a R Shiny dashboard to see how factors affect the outcomes of pregnancies. 


**Tools used:** Shiny Server, Shiny UI, Markdown, Rpart, Rpartplot

