---
name: "Multiple Imputations (mice) Intermediate"
author: "HIV Estimates Accuracy tool"
title: "Multiple Imputations (mice)"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  html_fragment:
    number_sections: no
    smart: yes
    fig_caption: yes
params:
  InputData: NA    
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = FALSE, results = "markup", include = TRUE, fig.height = 10, fig.width = 10, warning = FALSE, message = FALSE)
require(shiny)
```

```{r, results='asis'}
cat(sprintf("# %d. Multiple Imputation using Chained Equations - MICE\n", params$InputData$RunIdx))
```

```{r, results='asis'}
maleAllXCols <- params$InputData$Artifacts$M$X_COLS$All
maleKeptXCols <- params$InputData$Artifacts$M$X_COLS$Kept
femaleAllXCols <- params$InputData$Artifacts$F$X_COLS$All
femaleKeptXCols <- params$InputData$Artifacts$F$X_COLS$Kept
otherAllXCols <- params$InputData$Artifacts$O$X_COLS$All
otherKeptXCols <- params$InputData$Artifacts$O$X_COLS$Kept

maleRemovedXCols <- setdiff(maleAllXCols, maleKeptXCols) 
femaleRemovedXCols <- setdiff(femaleAllXCols, femaleKeptXCols)
otherRemovedXCols <- setdiff(otherAllXCols, otherKeptXCols)

maleAllYCols <- params$InputData$Artifacts$M$Y_COLS$All
maleKeptYCols <- params$InputData$Artifacts$M$Y_COLS$Kept
femaleAllYCols <- params$InputData$Artifacts$F$Y_COLS$All
femaleKeptYCols <- params$InputData$Artifacts$F$Y_COLS$Kept
otherAllYCols <- params$InputData$Artifacts$O$Y_COLS$All
otherKeptYCols <- params$InputData$Artifacts$O$Y_COLS$Kept

maleRemovedYCols <- setdiff(maleAllYCols, maleKeptYCols)
femaleRemovedYCols <- setdiff(femaleAllYCols, femaleKeptYCols)
otherRemovedYCols <- setdiff(otherAllYCols, otherKeptYCols)

if (length(maleRemovedXCols) > 0 | length(maleRemovedYCols) > 0 | 
    length(femaleRemovedXCols) > 0 | length(femaleRemovedYCols) > 0 | 
    length(otherRemovedXCols) > 0 | length(otherRemovedYCols) > 0) {
  cat("<p>Some attributes in the input data have not been processed with this adjustment due to insufficient data:</p>",
      "<ul>")
  if (length(maleRemovedXCols) > 0 | length(maleRemovedYCols) > 0) {
    cat("<li>Male gender<ul>")
    if (length(maleRemovedXCols) > 0) {
      cat("<li>Covariates: ", maleRemovedXCols, "</li>")
    }
    if (length(maleRemovedYCols) > 0) {
      cat("<li>Outputs: ", maleRemovedYCols, "</li>")
    }
    cat("</ul></li>")
  }
  if (length(femaleRemovedXCols) > 0 | length(femaleRemovedYCols) > 0) {
    cat("<li>Female gender<ul>")
    if (length(femaleRemovedXCols) > 0) {
      cat("<li>Covariates: ", femaleRemovedXCols, "</li>")
    }
    if (length(femaleRemovedYCols) > 0) {
      cat("<li>Outputs: ", femaleRemovedYCols, "</li>")
    }
    cat("</ul></li>")
  }
  if (length(otherRemovedXCols) > 0 | length(otherRemovedYCols) > 0) {
    cat("<li>Other gender<ul>")
    if (length(otherRemovedXCols) > 0) {
      cat("<li>Covariates: ", otherRemovedXCols, "</li>")
    }
    if (length(otherRemovedYCols) > 0) {
      cat("<li>Outputs: ", otherRemovedYCols, "</li>")
    }
    cat("</ul></li>")
  }
  
  cat("</ul>")
}
```

Maximum 5 first chains (imputations) are displayed.

<!--html_preserve-->
<div class="tabbable">
<!--/html_preserve-->
```{r, results='asis', echo = FALSE}
processedGenders <- names(params$InputData$Artifacts)
genderNameMapping <- c("M" = "Male", "F" = "Female", "O" = "Other")
genders <- names(genderNameMapping)
genders <- genders[genders %in% processedGenders]

tabIdx <- 0
plotTypes <- c("Convergence", "Observed vs. Imputed")
tabsHeader <- c()
for (gender in genders) {
  genderName <- genderNameMapping[gender]
  for (plotType in plotTypes) {
    tabIdx <- tabIdx + 1
    class <- ifelse(tabIdx == 1, " class='active'", "")
    tabTitle <- sprintf("%s - %s", genderName, plotType)
    tabsHeader[tabIdx] <- 
      knitr::knit_expand(
        text = "<li{{class}}><a href='#miceTab{{tabIdx}}' data-toggle='tab' data-value='miceVal{{tabIdx}}'>{{tabTitle}}</a></li>")
  }
}
tabsHeader <- paste(tabsHeader, collapse = "")
cat(knitr::knit_expand(text = "<ul class='nav nav-tabs'>{{tabsHeader}}</ul>"))

cat("<div class='tab-content'>")
tabIdx <- 0
for (gender in genders) {
  genderName <- genderNameMapping[gender]

  # Sample plot
  tabIdx <- tabIdx + 1
  class <- ifelse(tabIdx == 1, " active", "")
  cat(knitr::knit_expand(text = "<div class='tab-pane{{class}}' id='miceTab{{tabIdx}}' data-value='miceVal{{tabIdx}}'>"))
  knitr::normal_print(GetMiceDiagnosticPlot(params$InputData$Artifacts[[gender]]$Mids))
  cat("</div>")
  
  # Distribution plot
  tabIdx <- tabIdx + 1
  cat(knitr::knit_expand(text = "<div class='tab-pane' id='miceTab{{tabIdx}}' data-value='miceVal{{tabIdx}}'>"))
  colNames <- params$InputData$Artifacts[[gender]]$Y_COLS$Kept
  dt <- params$InputData$Table[Gender == gender]
  charts <- list()
  chartIdx <- 0
  for (colName in colNames) {
    chartIdx <- chartIdx + 1
    charts[[chartIdx]] <- GetImputedDistPlots(colName = colName, dt = dt, missingOnly = TRUE)
    
    chartIdx <- chartIdx + 1
    charts[[chartIdx]] <- GetImputedDistPlots(colName = colName, dt = dt, missingOnly = FALSE)
  }
  PlotMultipleCharts(charts, cols = 2)

  cat("</div>")
}
cat(sprintf("</div>"))
```
<!--html_preserve-->
<div>
<!--/html_preserve-->
