---
name: "Main Report"
author: "HIV Estimates Accuracy tool"
title: "Adjustments report"
date: "`r format(Sys.time(), '%d %B %Y, %H:%M:%S')`"
editor_options: 
  chunk_output_type: console
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \usepackage{titling}
- \pretitle{\begin{center}\LARGE\includegraphics[width=3cm]{resources/ECDC_logo.pdf}\\[\bigskipamount]}
- \posttitle{\end{center}}  
output:
  html_fragment:
    smart: yes
    toc: yes
    number_sections: yes
    dev: svg
  html_document:
    highlight: null
    mathjax: null
    number_sections: yes
    dev: svg
    self_contained: yes
    theme: default
    toc: yes
    toc_float:
      collapsed: false
      smooth_scroll: false
  latex_document:
    toc: yes
    template: default
    highlight: null
    citation_package: none
    number_sections: yes
  pdf_document:
    toc: yes
    template: default
    highlight: null
    citation_package: none
    number_sections: yes
  word_document:
    reference_docx: ./resources/template_ECDC.docx
    fig_width: 7
    fig_height: 4
    toc: yes
params:
  Artifacts: NA
  ReportingDelay:
    label: "Adjust count of cases for reporting delay"
    value: TRUE
    input: checkbox
  Smoothing:
    label: "Apply plot curves smoothing"
    value: FALSE
    input: checkbox
  CD4ConfInt:
    label: "Plot inter-quartile range in CD4 count plots"
    value: FALSE
    input: checkbox
---

```{r setup, eval = TRUE, include = FALSE}

IsEmptyString <- hivEstimatesAccuracy::IsEmptyString

knitr::opts_chunk$set(echo = FALSE, 
                      cache = FALSE,
                      results = "asis", 
                      message = FALSE, 
                      warning = FALSE, 
                      include = TRUE, 
                      fig.height = 4,
                      fig.width = 10)
```

--------------------------------------------------------------------------------

# Introduction

```{r}
cat("Input data:", 
    "\n\n")
cat(sprintf("* File name: %s", params$Artifacts$FileName),
    "\n")
cat(sprintf("* %s", 
            ifelse(params$Artifacts$DiagYearRangeApply, 
                   sprintf("Filter on diagnosis year applied: [%s]", paste(params$Artifacts$DiagYearRange, collapse = ", ")),
                   "No filter on diagnosis year applied.")),
    "\n")


cat("\n", 
    "Adjustments:", 
    "\n\n")
for (adjustment in params$Artifacts$Adjustments) {
  cat(sprintf("* %s", adjustment$Name), "\n")
  for (parameter in adjustment$Parameters) {
    cat(sprintf("  * %s: %s", parameter$Label, as.character(parameter$Value)), "\n")
  }
}

cat("\n", "Report options:", "\n\n") 
cat(sprintf("* %s", 
            ifelse(params$ReportingDelay, 
                   "Correction of count of cases for reporting delay.",
                   "Original count of case.")),
    "\n")
cat(sprintf("* %s", 
            ifelse(params$Smoothing, 
                   "Smoothed version of calendar time.",
                   "Original calendar time.")),
    "\n")
cat(sprintf("* %s", 
            ifelse(params$CD4ConfInt, 
                   "CD4 plots with inter-quartile range.",
                   "CD4 plots without inter-quartile range.")),
    "\n")

if (!params$Artifacts$CD4Present) {
  cat("\n", "CD4 count is missing in both the unadjusted and the adjusted data. All CD4 related tables and figures are omitted in this report.", "\n")  
}
```

--------------------------------------------------------------------------------

# Comparison of data by Gender

## Number of diagnoses per year

Data stratified by *gender* is shown only for unadjusted data since there were 
no missing data on *gender* after pre-processing, which takes place before 
adjustments.

### Before and after adjustments

```{r, count-observed-by-gender}
params$Artifacts$PlotOrigGenderCount
params$Artifacts$TblOrigGenderCount
```

## Median CD4 cell count (cells/&micro;L)

```{r, cd4-observed-by-gender, eval = params$Artifacts$CD4Present}
cat("### Before adjustments", "\n")
params$Artifacts$PlotOrigGenderCD4
params$Artifacts$TblOrigGenderCD4
```

```{r, cd4-imputed-by-gender, eval = params$Artifacts$CD4Present & params$Artifacts$MIPresent}
cat("### After adjustments", " \n")
params$Artifacts$PlotMIGenderCD4
params$Artifacts$TblMIGenderCD4
if (!IsEmptyString(params$Artifacts$DataMIGenderCD4Message)) {
  cat(params$Artifacts$DataMIGenderCD4Message, "\n")
}
```

```{r, eval = params$Artifacts$CD4Present & !params$Artifacts$MIPresent}
cat("CD4 cell count after adjustments is not available since no imputation adjustements have been run.", "\n")
```

```{r, eval = !params$Artifacts$CD4Present}
cat("CD4 cell count results are not available due to full missingness of this attribute in the input data.", "\n")
```

--------------------------------------------------------------------------------

# Comparison of data by Transmission

## Number of diagnoses per year

### Before adjustments

```{r, count-observed-by-trans}
params$Artifacts$PlotOrigTransCount
params$Artifacts$TblOrigTransCount
```

```{r, count-imputed-by-trans, eval = params$Artifacts$MIPresent}
cat("### After adjustments", "\n")
params$Artifacts$PlotMITransCount
params$Artifacts$TblMITransCount
if (!IsEmptyString(params$Artifacts$dataMITransCountMessage)) {
  cat(params$Artifacts$dataMITransCountMessage, "\n")
}
```

## Median CD4 cell count (cells/&micro;L)

```{r, cd4-observed-by-trans, eval = params$Artifacts$CD4Present}
cat("### Before adjustments", "\n")
params$Artifacts$PlotOrigTransCD4
params$Artifacts$TblOrigTransCD4
```

```{r, cd4-imputed-by-trans, eval = params$Artifacts$CD4Present & params$Artifacts$MIPresent}
cat("### After adjustments", "\n")
params$Artifacts$PlotMITransCD4
params$Artifacts$TblMITransCD4
if (!IsEmptyString(params$Artifacts$DataMITransCD4Message)) {
  cat(params$Artifacts$DataMITransCD4Message, "\n")
}
```

```{r, eval = params$Artifacts$CD4Present & !params$Artifacts$MIPresent}
cat("CD4 cell count after adjustments is not available since no imputation adjustements have been run.", "\n")
```

```{r, eval = !params$Artifacts$CD4Present}
cat("CD4 cell count results are not available due to full missingness of this attribute in the input data.", "\n")
```

--------------------------------------------------------------------------------

# Comparison of data by Migration

## Number of diagnoses per year

### Before adjustments

```{r, count-observed-by-migr}
params$Artifacts$PlotOrigMigrCount
params$Artifacts$TblOrigMigrCount
```

```{r, count-imputed-by-migr, eval = params$Artifacts$MIPresent}
cat("### After adjustments", "\n")
params$Artifacts$PlotMIMigrCount
params$Artifacts$TblMIMigrCount
if (!IsEmptyString(params$Artifacts$DataMIMigrCountMessage)) {
  cat(params$Artifacts$DataMIMigrCountMessage, "\n")
}
```

## Median CD4 cell count (cells/&micro;L)

```{r, cd4-observed-by-migr, eval = params$Artifacts$CD4Present}
cat("### Before adjustments", "\n")
params$Artifacts$PlotOrigMigrCD4
params$Artifacts$TblOrigMigrCD4
```

```{r, cd4-imputed-by-migr, eval = params$Artifacts$CD4Present & params$Artifacts$MIPresent}
cat("### After adjustments", "\n")
params$Artifacts$PlotMIMigrCD4
params$Artifacts$TblMIMigrCD4
if (!IsEmptyString(params$Artifacts$DataMIMigrCD4Message)) {
  cat(params$Artifacts$DataMIMigrCD4Message, "\n")
}
```

```{r, eval = params$Artifacts$CD4Present & !params$Artifacts$MIPresent}
cat("CD4 cell count after adjustments is not available since no imputation adjustements have been run.", "\n")
```

```{r, eval = !params$Artifacts$CD4Present}
cat("CD4 cell count results are not available due to full missingness of this attribute in the input data.", "\n")
```

```{r, eval = params$Artifacts$RDPresent}
cat("-------------------------------------------------------------------", "\n")
cat("\n")
cat("# Comparison of the reported and estimated number of diagnoses per year", "\n")
params$Artifacts$TblRd
```
