---
title: "Preliminary Report"
author: "MBECS"
# output: pdf_document
output:
  html_document:
    toc: true
    toc_float: true
    theme: cerulean
    highlight: kate
params:
  date: !r Sys.Date()
  region: Europe
  printcode: TRUE
  data: file.csv
  report.data: !r list()
  report.vars: !r character()
  report.list: !r list()
  report.methods: !r data.frame(matrix(data=TRUE, nrow=1, ncol=10, dimnames=list(NULL,c("mosaic","pca","rle","heat","box","linmod","linmixmod","rda","pvca","scoef"))))
  something: !r c(1,2,3)

---


```{r SETUP, echo=FALSE}
knitr::opts_chunk$set(echo=FALSE)
```


# Study Summary {.tabset}

A synopsis of covariates, sample dissemination over study grouping/batches and sample clustering.


## Covariates

A summary of the data-sets covariate information.

```{r COVARIATES, comment=''}
# Summarize the covariate information.
summary(params$report.data[[2]])
```


## Sample Distribution

How are the samples disseminated over batches and the effect of interest.

```{r MOSAIC}
# Mosaic style plot
plot(params$report.list$mosaic)

```


## Sample Separation

The Principal Component Analysis shows sample-relatedness (clustering) on a 2D plane and can help identify the presence of confounding factors.

```{r PCA}

# PCA plot
plot(params$report.list$pca)

```


# Visualization {.tabset}

Show how feature abundances are distributed over study grouping and batches.


## Relative Log Expression (RLE)

Separate samples by covariate of interest (CoI), e.g., treatment or study group, calculate the median value for every feature count and subtract it from all samples respectively.

```{r RLE}

# RLE plot
plot(params$report.list$rle)

```


## Heatmap

Show the top ten most dispersed features by interquartile range.

```{r HEAT}

# Heatmap features of interest and description.
print(params$report.list$heat)

```


## BOX-plot

Show the top five most dispersed features by interquartile range and show within batches.

```{r BOX}
for( p.idx in params$report.list$box ) {

  plot(p.idx)

}
```

# Variance Assessment {.tabset}


Several different approaches are used to estimate the amount of variability attributable to covariates of interest.


## Linear Model (LM)

This method fits the linear model 'y ~ group + batch' to every feature respectively and estimates the proportion of variance that the modeled covariates of interest (coi) account for. The results are visualized in a box-plot that shows the coi and the residual values.

```{r LINMOD}
# avg. variance in linear model plot
print(params$report.list$linmod)
```


## Linear (Mixed) Model (LMM)

This method fits the linear mixed model 'y ~ group + (1|batch)' to every feature respectively and estimates the proportion of variance that the modeled covariates of interest (coi) account for. The results are visualized in a box-plot that shows the coi and the residual values.

```{r LINMIXMOD}
# avg. variance in linear model plot
print(params$report.list$linmixmod)
```


## Redundancy Analysis (pRDA)

A linear regression model is fitted to the feature-matrix (i.e. counts) while conditioning on one COI at a time to extract the proportion of explained variance for the variables. Then this procedure is repeated with switched covariates.

Basically, it takes 'counts ~ group + Condition(batch)'  and subtract counts ~ group and see how much variance batch accounts for - then repeat with group as Condition

```{r RDA}
# RDA plot and description.
print(params$report.list$rda)
```


## Principal Variance Component Analysis (PVCA)

Select the number of principal components that are required to account for more than 65% of variance and iterate over all PCs and fit a linear mixed model that contains all covariates as random effect and all unique interactions between two covariates. Compute variance covariance components form the resulting model and extract the variance that each covariate contributes to this particular PC. Standardize variance by dividing it through the sum of variance for that model. Scale each PCs results by the proportion this PC accounted for in the first place. And then do it again by dividing it through the total amount of explained variance, i.e. the cutoff to select the number of PCs to take, or rather the actual values for the selected PCs. Finally take the average over each random variable and interaction term and display in a bar-plot.

```{r PVCA}

# PVCA plot and description.
print(params$report.list$pvca)

```


## Silhouette Coefficient

Calculate principal components and get sample-wise distances on the resulting sxPC matrix.
Then iterate over all the covariates and calculate the cluster silhouette. This is essentially either zero, if the cluster contains only a single element, or it is the distance to the closest different cluster minus the distance of the sample within its own cluster divided (scaled) by the maximum distance. Average over each element in a cluster for all clusters respectively and obtain a representation of how good the clustering is.

```{r SCOEF}

# Average silhouette-coef plot and description.
print(params$report.list$scoef)

```

