---
title: "ncappc  report^[Acharya, C., Hooker, A. C., Turkyilmaz, G. Y., Jonsson, S., Karlsson, M. O.; A diagnostic tool for population models using non-compartmental analysis: The ncappc package for R, *Computer Methods and Programs in Biomedicine*, 2016, Vol. 127, 83-93]"
output:
  bookdown::pdf_document2:
    number_sections: yes
    toc: no
    includes:
      in_header: styles.tex
      before_body: styles2.tex
  bookdown::html_document2:
    number_sections: yes
    toc: yes
---

<!-- toc: yes -->
<!-- css: custom.css -->

```{r setup_ncappc_report, include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE, comment=NA, tidy = TRUE, 
               fig.align = 'center',
               fig.show='asis', out.width='80%', fig.pos='H')
```


```{r main, echo=FALSE}
arglist  <- fnOut$arglist
case     <- fnOut$case
txt      <- fnOut$TXT
pddf     <- fnOut$pddf
prnTab   <- fnOut$prnTab
nsim     <- fnOut$NSIM
conc     <- fnOut$conc
histobs  <- fnOut$histobs
pop      <- fnOut$pop
dev      <- fnOut$dev
outlier  <- fnOut$outlier
forest   <- fnOut$forest
npde     <- fnOut$npde
histnpde <- fnOut$histnpde
hth      <- fnOut$phth
wth      <- fnOut$pwth

if(fnOut$spread=="ppi"){
  sprtxt <- "95% parametric prediction interval of the NCA metrics"
}else if(fnOut$spread=="npi"){
  sprtxt <- "95% nonparametric prediction interval of the NCA metrics"
}

```


# Results Summary

**Arguments passed to the *ncappc* function:**

```{r cmd, results='asis'}
print(arglist)
```

**Data, settings and results:**

```{r txt}
cat("Main directory: ", getwd(),"\n")
cat(txt)
```

```{r popplot3,fig.align="center",fig.show='asis',out.width='80%',fig.cap=x,eval.after='fig.cap',fig.pos='H'}
x <- NULL
if(exists("pop_hist_list")){
  if (length(pop_hist_list)>0){
    x <- c()
    for (i in 1:length(pop_hist_list)){
      print(pop_hist_list[[i]])
      if(i ==1){
        #cat("## Summary NCA metrics : {-}")
        
        x <- c(x,paste0('Histogram of the population mean and variance of the NCA metrics. The observed metric is compared to percentiles of the simulated metric computed from ', nsim, ' simulations.'))
      } else{
        x <- c(x,'(Continued) Histogram of the population mean and variance of the NCA metrics.')
      }
    }
  }
}
```


```{r table, echo=FALSE}
if(onlyNCA){
  cap <- "Dataset summary"
} else {
  cap <- "Dataset summary and outlying individuals. Outlying individuals for a given NCA metric are defined as individuals where the NCA metric computed from the observed data does not fall within the 95% nonparametric confidence interval of that metric based on the simulated datasets."
}
kable(pddf, align='c',booktabs = TRUE,longtable = TRUE,
      caption = cap)
```


# Tabular output

A number of tables are produced from the *ncappc* tool and are found in the same directory as this report.  Descriptions of these tables are below.  All tables are produced as tab separated value (.tsv) files, *i.e.*, the columns in the tables are separated by tabs.  "NaN" or "NA" is produced if a metric or value is irrelevant for a specified data type or case.


```{r ncaOutput cond text, echo=FALSE, results='asis'}
if(onlyNCA){
  cat('**ncaOutput.tsv:** The estimated values of the NCA metrics for each individual along with other stratifiers (eg. population group ID, dose ID, etc.) if specified in the input command.')
} else {
  cat('**ncaOutput.tsv:** The estimated values of the NCA metrics for each individual along with other stratifiers (eg. population group ID, dose ID, etc.) if specified in the input command. This table also reports three additional columns for each of the NCA metrics that can be used for diagnostics. sim[metric_name] represents the median of the metric obtained from the set of simulated data. d[metric_name] represents the deviation of the median of the simulated metrics from the metric obtained from the observed data, scaled by the "spread" of the simulated distribution (as defined in the "spread" argument of the ncappc function). npde[metric_name] represents the NPDE value of the metric estimated from the simulated data with respect to the observed data.')
}
```


```{r ncaOutput, echo=FALSE}
cap <- 'A representative portion of the table ncaOutput.tsv.'
kable(prnTab, align='c',booktabs = TRUE,longtable = TRUE,
      caption = cap)
```


<!-- ```{r Final table, echo=FALSE, results='asis'} -->
<!-- suppressPackageStartupMessages(require(xtable)) -->
<!-- print(xtable(prnTab, align=rep("c",ncol(prnTab)+1), rotate.colnames = T), include.rownames=FALSE, type = 'latex') -->
<!-- ``` -->

**ObsStat.tsv:** A set of statistical parameters calculated for the entire population or the stratified population for the following NCA metrics estimated from the *observed data*: `r paste0(names(statData)[-1],collapse=", ")`. Calculated statistical parameters: **Ntot:** Total number of data points, **Nunique:**  number of unique data points, **Min:** minimum value, **Max:** maximum value, **Mean:**  mean/average value, **SD:**  standard deviation, **SE:** standard error, **CVp:**  coefficient of variation in percent, **a95CIu:** upper limit of 95% arithmetic confidence interval, **a95CIl:** lower limit of 95% arithmetic confidence interval, **gMean:** geometric mean, **gCVp:** geometric coefficient of variation in percent. 
  
```{r ObsStat, echo=FALSE}
tmp_data <- statData[,1:9]
if(nrow(tmp_data)>20) tmp_data <- tmp_data[1:20,]
kable(tmp_data, align='c',booktabs = TRUE,longtable = TRUE,
      caption = 'A representative portion of the table ObsStat.tsv.')
```


```{r SimStat cond text, echo=FALSE, results='asis'}
if(!onlyNCA){
  cat('**SimStat.tsv:** A set of statistical parameters calculated for the entire population or the stratified population for the NCA metrics estimated from the *simulated data*. The table is structured as "ObsStat.tsv".')
} 
```

```{r SimStat, echo=FALSE}
if(!onlyNCA){
  cap <- ' A representative portion of the table SimStat.tsv. '
  
  tmp_data <- statDataSim[,1:9]
  if(nrow(tmp_data)>20) tmp_data <- tmp_data[1:20,]
  kable(tmp_data, align='c',booktabs = TRUE,longtable = TRUE,
        caption = cap)
}
```

```{r ncaSimData cond text, echo=FALSE, results='asis'}
if(!onlyNCA){
  cat('**ncaSimData.tsv.gz:** Simulated concentration-time profiles for each individual obtained from each simulation. "NSUB" column denotes the simulation number.  The file is compressed in the gzip format to save space.')
} 
```

```{r ncaSimData, echo=FALSE}
if(!onlyNCA){
  cap <- 'A representative portion of the table ncaSimData.tsv. '
  
  if(exists("nmdf")){
    tmp_table <- nmdf[1:20,]
  } else {
    #tmp_table <- read.table(file=paste0(usrdir,"/ncaSimData.tsv"), sep="\t",nrows=20,
    #                        header = T)
    tmp_table <- suppressMessages(readr::read_delim(file.path(usrdir,"ncaSimData.tsv.gz"),delim = "\t",n_max=20))
  }

  if(ncol(tmp_data)>10) tmp_data <- tmp_data[,1:10]

  kable(tmp_table, align='c',booktabs = TRUE,longtable = TRUE,
        caption = cap)
}
```

```{r ncaSimEst cond text, echo=FALSE, results='asis'}
if(!onlyNCA){
  cat('**ncaSimEst.tsv:** Estimated NCA metrics for each individual using the simulated concentration-time profile obtained from each simulation. The "NSUB" column denotes the simulation number.')
} 
```

```{r ncaSimEst, echo=FALSE}
if(!onlyNCA){
  cap <- 'A representative portion of the table ncaSimEst.tsv. '
  
  tmp_data <- dasdf[,1:9]
  if(nrow(tmp_data)>20) tmp_data <- tmp_data[1:20,]
  kable(tmp_data, align='c',booktabs = TRUE,longtable = TRUE,
        caption = cap)
}
```

# Graphical output

```{r concplot, fig.align="center",fig.show='asis',fig.cap=x,eval.after='fig.cap'}
if (length(conc)>0){
  for (i in 1:length(conc)){
    if(is.gtable(conc[[i]])){ 
      suppressMessages(suppressWarnings(grid.arrange(conc[[i]])))
    } else {
      print(conc[[i]])
    }
    if(i ==1){
      x <- c(x,"Concentration vs. time profile for each individual (stratified by dose or population group, if any). The left panel shows the concentration data on the linear scale, while the right panel shows the concentration data on semi-logarithmic scale.")
    } else{
      x <- c(x,'(Continued) Concentration vs. time profile for each individual (stratified by dose or population group, if any).')
    }
  }
}

```

(ref:cap-histobsplot) [Population level] Histogram of four selected NCA metrics (AUClast, AUCINF_obs, Cmax, Tmax) estimated from the observed data. The solid blue vertical and dotted lines represent the population median and the "spread" of the data. The "spread" is defined by `r sprtxt` obtained from the observed data.

```{r histobsplot, echo=FALSE, fig.height=9, fig.width=8, fig.align='center', fig.cap='(ref:cap-histobsplot)'}
if (length(histobs)>0){
  for (i in 1:length(histobs)){suppressMessages(suppressWarnings(grid.draw(histobs[[i]]))); Sys.sleep(0.5); grid.newpage()}
}
```

<!-- (ref:cap-popplot) [Population level] Histogram of the population median of the NCA metrics obtained from the simulated data from the `r nsim` simulations. The red and blue solid vertical lines represent the population median of the NCA matric obtained from the observed data and the median of the population medians of the same NCA metric obtained from the `r nsim` number of simulations, respectively. The blue dashed vertical lines represent the "spread" of the simulated distribution. The "spread" is defined as `r sprtxt` obtained from the simulated data. -->


<!-- ```{r popplot, echo=FALSE, fig.height=9, fig.width=8, fig.align='center', fig.cap='(ref:cap-popplot)'} -->
<!-- if (length(pop)>0){ -->
<!--   suppressPackageStartupMessages(require(gridExtra)) -->
<!--   for (i in 1:length(pop)){suppressMessages(suppressWarnings(grid.draw(pop[[i]]))); Sys.sleep(0.5); grid.newpage()} -->
<!-- } -->
<!-- ``` -->



```{r popplot2,fig.align="center",fig.show='asis',out.width='80%',fig.cap=x,eval.after='fig.cap'}
x <- NULL
if(exists("pop_hist_list")){
  if (length(pop_hist_list)>0){
    x <- c()
    for (i in 1:length(pop_hist_list)){
      print(pop_hist_list[[i]])
      if(i ==1){
        x <- c(x,paste0('Histogram of the population mean and variance of the NCA metrics. The observed metric is compared to percentiles of the simulated metric computed from ', nsim, ' simulations.'))
      } else{
        x <- c(x,'(Continued) Histogram of the population mean and variance of the NCA metrics.')
      }
    }
  }
}

```

(ref:cap-devplot) [Individual level] Deviation of the median of the NCA metrics for each individual estimated from the simulated data obtained from `r nsim` simulations (medianSim) from the corresponding values estimated from the observed data (Obs). The deviation is scaled by the boundary of the "spread" of the simulated data (`r sprtxt`) proximal to the observed value (** Deviation = (Obs - medianSim)/ spread **). The negative value of the deviation signifies over-prediction of the corresponding NCA matric, while a positive value of the deviation signifies under-prediction of the same.



```{r devplot, echo=FALSE, fig.height=9, fig.width=8, fig.align='center', fig.cap='(ref:cap-devplot)'}
if (length(dev)>0){
  for (i in 1:length(dev)){suppressMessages(suppressWarnings(print(dev[[i]]))); Sys.sleep(0.5)}
}
```

(ref:cap-outlierplot) [Individual level] Distribution of the NCA metrics obtained from the simulated data for the outlier individuals. The individuals are labeled as outliers because the absolute value of the scaled deviation for at least one of the NCA metrics used in diagnosis is larger than 1. The red and blue solid vertical lines represent the observed NCA matric value and the median of the simulated NCA matric values for that individual, respectively. The dashed blue vertical lines represent the "spread" (`r sprtxt`) of the simulated distribution.

```{r outlierplot, echo=FALSE, fig.height=9, fig.width=8, fig.align='center', fig.cap='(ref:cap-outlierplot)'}
if (length(outlier)>0){
  suppressPackageStartupMessages(require(gridExtra))
  for (i in 1:length(outlier)){suppressMessages(suppressWarnings(grid.draw(outlier[[i]]))); Sys.sleep(0.5); grid.newpage()}
}
```


(ref:cap-histnpdeplot) [Population level] Histogram of the NPDE values of the NCA metrics for all individuals within a given population group. The red solid vertical represents the mean of the ideal NPDE distribution, which is the theoretical normal distribution (mean=0, SD=1). The blue solid vertical lines represent the mean of the NPDE distribution for the corresponding population group. The dashed blue vertical lines represent the standard deviation of the distribution of the NPDE values within that population group.


```{r histnpdeplot, echo=FALSE, fig.height=9, fig.width=8, fig.align='center', fig.cap='(ref:cap-histnpdeplot)'}
if (length(histnpde)>0){
  for (i in 1:length(histnpde)){suppressMessages(suppressWarnings(print(histnpde[[i]]))); Sys.sleep(0.5)}
}
```

(ref:cap-forestplot) [Population level] Forest plot of the NPDE type analysis displaying the mean and standard deviation of the NPDE vaues of the NCA metrics for different population groups. The red and green dots represent the mean and the standard deviation of the NPDE, respectively while the horizontal red and green lines represent the corresponding 95% confidence intervals.


```{r forestplot, echo=FALSE, fig.height=9, fig.width=8, fig.align='center', fig.cap='(ref:cap-forestplot)'}
if (length(forest)>0){
  for (i in 1:length(forest)){suppressMessages(suppressWarnings(print(forest[[i]]))); Sys.sleep(0.5)}
}
```

(ref:cap-npdeplot) [Individual level] NPDE values of the NCA metrics for each individual within a given population group calculated from the corresponding observed and simulated values of the NCA metrics. The negative value of the NPDE signifies over-prediction of the corresponding NCA matric, while a positive value of the NPDE signifies under-prediction of the same.

```{r npdeplot, echo=FALSE, fig.height=9, fig.width=8, fig.align='center', fig.cap='(ref:cap-npdeplot)'}
if (length(npde)>0){
  for (i in 1:length(npde)){suppressMessages(suppressWarnings(print(npde[[i]]))); Sys.sleep(0.5)}
}
```


