---
name: "Multiple Imputations (jomo) Intermediate"
author: "HIV Estimates Accuracy tool"
title: "Multiple Imputations (jomo)"
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 = "asis", include = TRUE, fig.height = 10, fig.width = 10, warning = FALSE, message = FALSE)
```

```{r, results='asis'}
cat(sprintf("# %d. Joint Modelling Multiple Imputation", 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>")
}
```

<!--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", "Autocorrelation", "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='#jomoTab{{tabIdx}}' data-toggle='tab' data-value='jomoVal{{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]

  # Trace plot
  tabIdx <- tabIdx + 1
  class <- ifelse(tabIdx == 1, " active", "")
  cat(knitr::knit_expand(text = "<div class='tab-pane{{class}}' id='jomoTab{{tabIdx}}' data-value='jomoVal{{tabIdx}}'>"))
  knitr::normal_print(GetMCMCBetasPlot(params$InputData$Artifacts[[gender]]$Beta))
  colNames <- colnames(params$InputData$Artifacts$F$Beta[, , 1])
  categoricalNames <- grep("\\.", colNames, value = TRUE)
  continuousNames <- setdiff(colNames, categoricalNames)
  rowNames <- row.names(params$InputData$Artifacts$F$Beta[, , 1])
  coefNames <- grep("(Intercept|SplineKnot)", rowNames, value = TRUE)
  covariateNames <- setdiff(rowNames, coefNames)
  cat("\n\n",
      "Legend:", 
      "\n\n")
  cat(sprintf("Continuous outcomes: %s<br />", paste(continuousNames, collapse = ", ")))
  cat(sprintf("Categorical outcomes (latent normal variables): %s<br />", paste(categoricalNames, collapse = ", ")))
  cat(sprintf("Covariate variables: %s<br />", paste(covariateNames, collapse = ", ")))
  cat(sprintf("Cubic spline coefficients and intercept: %s<br />", paste(coefNames, collapse = ", ")))
  cat("</div>")

  # Autocorrelation plot
  tabIdx <- tabIdx + 1
  cat(knitr::knit_expand(text = "<div class='tab-pane' id='jomoTab{{tabIdx}}' data-value='jomoVal{{tabIdx}}'>"))
  knitr::normal_print(GetMCMCAutoCorrelationPlot(params$InputData$Artifacts[[gender]]$Covariance))
  cat("\n\n",
      "Legend:", 
      "\n\n")
  colNames <- colnames(params$InputData$Artifacts$M$Covariance[, , 1])
  categoricalNames <- grep("\\.", colNames, value = TRUE)
  continuousNames <- setdiff(colNames, categoricalNames)
  cat(sprintf("Continuous outcomes: %s<br />", paste(continuousNames, collapse = ", ")))
  cat(sprintf("Categorical outcomes (latent normal variables): %s<br />", paste(categoricalNames, collapse = ", ")))
  cat("</div>")
  
  # Distribution plot
  tabIdx <- tabIdx + 1
  cat(knitr::knit_expand(text = "<div class='tab-pane' id='jomoTab{{tabIdx}}' data-value='jomoVal{{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-->



<!-- ## Diagnostic plots for Gender = \"Female\" -->

<!-- **Figure 1. Trace plot**<br /> -->
<!-- ```{r} -->
<!-- print(GetMCMCBetasPlot(params$InputData$Artifacts$F$Beta)) -->
<!-- ``` -->

<!-- Legend:<br /> -->
<!-- ```{r} -->
<!-- colNames <- colnames(params$InputData$Artifacts$F$Beta[, , 1]) -->
<!-- categoricalNames <- grep("\\.", colNames, value = TRUE) -->
<!-- continuousNames <- setdiff(colNames, categoricalNames) -->

<!-- rowNames <- row.names(params$InputData$Artifacts$F$Beta[, , 1]) -->
<!-- coefNames <- grep("(Intercept|SplineKnot)", rowNames, value = TRUE) -->
<!-- covariateNames <- setdiff(rowNames, coefNames) -->

<!-- cat(sprintf("Continuous outcomes: %s<br />", paste(continuousNames, collapse = ", "))) -->
<!-- cat(sprintf("Categorical outcomes (latent normal variables): %s<br />", paste(categoricalNames, collapse = ", "))) -->
<!-- cat(sprintf("Covariate variables: %s<br />", paste(covariateNames, collapse = ", "))) -->
<!-- cat(sprintf("Cubic spline coefficients and intercept: %s<br />", paste(coefNames, collapse = ", "))) -->
<!-- ``` -->

<!-- **Figure 2. Autocorrelation plot**<br /> -->
<!-- ```{r} -->
<!-- print(GetMCMCAutoCorrelationPlot(params$InputData$Artifacts$F$Covariance)) -->
<!-- ``` -->

<!-- Legend:<br /> -->
<!-- ```{r} -->
<!-- colNames <- colnames(params$InputData$Artifacts$F$Covariance[, , 1]) -->
<!-- categoricalNames <- grep("\\.", colNames, value = TRUE) -->
<!-- continuousNames <- setdiff(colNames, categoricalNames) -->

<!-- cat(sprintf("Continuous outcomes: %s<br />", paste(continuousNames, collapse = ", "))) -->
<!-- cat(sprintf("Categorical outcomes (latent normal variables): %s<br />", paste(categoricalNames, collapse = ", "))) -->
<!-- ``` -->

<!-- **Figure 3. Distributions of imputed values**<br /> -->
<!-- ```{r, fig.height = 3} -->
<!-- colNames <- params$InputData$Artifacts$F$Y_COLS$Kept -->
<!-- dtF <- params$InputData$Table[Gender == "F"] -->
<!-- for (colName in colNames) { -->
<!--   PlotMultipleCharts( -->
<!--     list( -->
<!--       GetImputedDistPlots(colName = colName, dt = dtF, missingOnly = TRUE), -->
<!--       GetImputedDistPlots(colName = colName, dt = dtF, missingOnly = FALSE) -->
<!--     ), -->
<!--     cols = 2 -->
<!--   ) -->
<!-- } -->
<!-- ``` -->

<!-- ## Diagnostic plots for Gender = \"Male\" -->

<!-- **Figure 4. Trace plot**<br /> -->
<!-- ```{r} -->
<!-- print(GetMCMCBetasPlot(params$InputData$Artifacts$M$Beta)) -->
<!-- ``` -->

<!-- Legend:<br /> -->
<!-- ```{r} -->
<!-- colNames <- colnames(params$InputData$Artifacts$M$Beta[, , 1]) -->
<!-- categoricalNames <- grep("\\.", colNames, value = TRUE) -->
<!-- continuousNames <- setdiff(colNames, categoricalNames) -->

<!-- rowNames <- row.names(params$InputData$Artifacts$M$Beta[, , 1]) -->
<!-- coefNames <- grep("(Intercept|SplineKnot)", rowNames, value = TRUE) -->
<!-- covariateNames <- setdiff(rowNames, coefNames) -->

<!-- cat(sprintf("Continuous outcomes: %s<br />", paste(continuousNames, collapse = ", "))) -->
<!-- cat(sprintf("Categorical outcomes (latent normal variables): %s<br />", paste(categoricalNames, collapse = ", "))) -->
<!-- cat(sprintf("Covariate variables: %s<br />", paste(covariateNames, collapse = ", "))) -->
<!-- cat(sprintf("Cubic spline coefficients and intercept: %s<br />", paste(coefNames, collapse = ", "))) -->
<!-- ``` -->

<!-- **Figure 5. Autocorrelation plot**<br /> -->
<!-- ```{r} -->
<!-- print(GetMCMCAutoCorrelationPlot(params$InputData$Artifacts$M$Covariance)) -->
<!-- ``` -->

<!-- Legend:<br /> -->
<!-- ```{r} -->
<!-- colNames <- colnames(params$InputData$Artifacts$M$Covariance[, , 1]) -->
<!-- categoricalNames <- grep("\\.", colNames, value = TRUE) -->
<!-- continuousNames <- setdiff(colNames, categoricalNames) -->

<!-- cat(sprintf("Continuous outcomes: %s<br />", paste(continuousNames, collapse = ", "))) -->
<!-- cat(sprintf("Categorical outcomes (latent normal variables): %s<br />", paste(categoricalNames, collapse = ", "))) -->
<!-- ``` -->

<!-- **Figure 6. Distributions of imputed values**<br /> -->
<!-- ```{r, fig.height = 3} -->
<!-- colNames <- params$InputData$Artifacts$M$Y_COLS$Kept -->
<!-- dtM <- params$InputData$Table[Gender == "M"] -->
<!-- for (colName in colNames) { -->
<!--   PlotMultipleCharts( -->
<!--     list( -->
<!--       GetImputedDistPlots(colName = colName, dt = dtM, missingOnly = TRUE), -->
<!--       GetImputedDistPlots(colName = colName, dt = dtM, missingOnly = FALSE) -->
<!--     ), -->
<!--     cols = 2 -->
<!--   ) -->
<!-- } -->
<!-- ``` -->
