Under 5 mortality in Madagascar

Setp by step analysis

Ranto Ramananjato
Statistics and monitoring specialist, UNICEF ESARO

Introduction

  • The objective of the application is to explore relationship between Under 5 mortality rate (U5MR) and some child key indicators: immunization, contraception, skilled attendance at delivery, stunting prevalence, untreated diarrhea
  • Data source is Madagascar MICS 2018. It can be downloaded from mics.unicef.org/surveys
  • Each of 22 rows correspond to administrative region
  • Indicators are aggregated at regional level (in %)
  • The idea is to create a dynamic plot with U5MR on Y-axis and let the user choose the variable on X-axis. Plot will automatically fit a linear model and will show its coefficients and R square

Ui.R Part 1

library(shiny)
shinyUI(fluidPage(
  titlePanel("Modelling Under 5 mortality rate"),
  sidebarLayout(
    sidebarPanel(
      h3("Instructions:"),
      h5("The purpose of this app is to explain Under 5 mortality rate (U5MR)
         in Madagascar by some child key indicators. Data are taken from 
         Madagascar MICS 2018 (mics.unicef.org/surveys) 
         Choose the indicator you want to use as independent variable and 
         you will get summary of related linear model (coefficients and R square). 
         You can also choose to show X and Y axis label or not"),
      radioButtons("x", "Independant variable:",
                   c("Immunization"="imcov",
                     "Contraception"="contraprev",
                     "Skilled attendance"="skilledatt",
                     "Stunting"= "stunting",
                     "Untreated diarrhea"="untreateddia")),

Ui.R Part 2

      checkboxInput("show_xlab", "Show/Hide X Axis Label", value = TRUE),
      checkboxInput("show_ylab", "Show/Hide Y Axis Label", value = TRUE),
    ),  
  mainPanel(
      h3("Under 5 mortality rate by chosen independant variable"),
      plotOutput("plot1"),
      h3("Estimated intercept:"),
      textOutput("intercept"),
      h3("Estimated slope:"),
      textOutput("slope"),
      h3("Estimated R square:"),
      textOutput("Rsq"),
    )
  )
))

Server.R Part 1

library(shiny)
data <- data.frame(read.csv("C:/Temp/um5r.csv"))
shinyServer(function(input, output) {
    output$plot1 <- renderPlot({
      dataX <- switch(input$x, imcov = data$imcov, contraprev = data$contraprev, 
        skilledatt = data$skilledatt, stunting = data$stunting, untreateddia = data$untreateddia)
      dataY <- data$u5mr
      model <- lm(dataY ~ dataX)
      modelpred <- reactive({
        predict(model, newdata = data.frame(dataX))
      })
    x <- switch(input$x,imcov = "Immunization coverage (%)", contraprev = 
        "Contraceptive prevalence (%)", skilledatt = "Skilled attendance at delivery (%)", 
        stunting = "Stunting rate (%)", untreateddia = "Untreated diarrhea (%)")
    xlab <- ifelse(input$show_xlab, x, "")
    ylab <- ifelse(input$show_ylab, "Under 5 mortality rate (per 1,000 LB)", "")
    plot(dataX, dataY, xlab = xlab, ylab = ylab, col="blue")
    abline(model, col = "red", lwd = 2)
  })

Server.R Part 2

  output$intercept <- renderText({
    dataX <- switch(input$x, imcov = data$imcov, contraprev = data$contraprev,
                    skilledatt = data$skilledatt, stunting = data$stunting,
                    untreateddia = data$untreateddia)
    dataY <- data$u5mr
    model <- lm(dataY ~ dataX)
    coef(model)[1]
  })
  output$slope <- renderText({
    dataX <- switch(input$x,imcov = data$imcov, contraprev = data$contraprev,
      skilledatt = data$skilledatt, stunting = data$stunting,untreateddia = data$untreateddia)
    dataY <- data$u5mr
    model <- lm(dataY ~ dataX)
    coef(model)[2]
  })
  output$Rsq <- renderText({
    dataX <- switch(input$x, imcov = data$imcov, contraprev = data$contraprev,
     skilledatt = data$skilledatt, stunting = data$stunting, untreateddia = data$untreateddia)
    dataY <- data$u5mr
    model <- lm(dataY ~ dataX)
    summary(model)[[8]]
  })
})