---
title: "A Brief Critique of Proportionality"
author: "Thomas Quinn"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{3. A Brief Critique of Proportionality}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

## Introduction

We recognize that this package uses concepts that are not necessarily intuitive. As such, we offer a brief critique of proportionality analysis. Although the user may feel eager to start here, we strongly recommend first reading the companion vignette, "An Introduction to Proportionality".

## Sample data

To facilitate discussion, we simulate count data for 5 features (e.g., genes) labeled "a", "b", "c", "d", and "e", as measured across 100 subjects.

```{r}
library(propr)
N <- 100
a <- seq(from = 5, to = 15, length.out = N)
b <- a * rnorm(N, mean = 1, sd = 0.1)
c <- rnorm(N, mean = 10)
d <- rnorm(N, mean = 10)
e <- rep(10, N)
X <- data.frame(a, b, c, d, e)
```

Let us assume that these data $X$ represent absolute abundance counts (i.e., not relative data). We can build a relative dataset, $Y$, by constraining and scaling $X$:

```{r}
Y <- X / rowSums(X) * abs(rnorm(N))
```

## Spurious correlation

Next, we compare pairwise scatterplots for the absolute count data and the corresponding relative count data. We see quickly how these relative data suggest a *spurious correlation*: although genes "c" and "d" do not correlate with one another absolutely, their relative quantities do.

```{r, dpi = 66, fig.width = 8, fig.height = 8, fig.show = "hold"}
pairs(X) # absolute data
```

```{r, dpi = 66, fig.width = 8, fig.height = 8, fig.show = "hold"}
pairs(Y) # relative data
```

Spurious correlation is evident by the correlation coefficients too.

```{r, warning = FALSE}
suppressWarnings(cor(X)) # absolute correlation
cor(Y) # relative correlation
```

## An in-depth look at VLR

In contrast, the **variance of the log-ratios** (VLR), defined as the variance of the logarithm of the ratio of two feature vectors, offers a measure of dependence that (a) does not change with respect to the nature of the data (i.e., absolute or relative), and (b) does not change with respect to the number of features included in the computation. As such, the VLR, constituting the numerator portion of the $\phi$ metric, and a portion of the $\rho$ metric as well, is considered *sub-compositionally coherent*. Yet, while VLR yields valid results for compositional data, it lacks a meaningful scale.

```{r}
propr:::proprVLR(Y[, 1:4]) # relative VLR
propr:::proprVLR(X) # absolute VLR
```

## An in-depth look at clr

In proportionality, we adjust the arbitrarily large VLR by the variance of its individual constituents. To do this, we need to place samples on a comparable scale. Log-ratio transformation, such as the **centered log-ratio** (clr) transformation, shifts the data onto a "standardized" scale that allows us to compare differences in the VLR-matrix.

In the next figures, we compare pairwise scatterplots for the clr-transformed absolute count data and the corresponding clr-transformed relative count data. While equivalent, we see a relationship between "c" and "d" that should not exist based on what we know from the non-transformed absolute count data. This demonstrates that, although the clr-transformation helps us compare values across samples, it does not rescue information lost by making absolute data relative.

```{r, dpi = 66, fig.width = 8, fig.height = 8, fig.show = "hold"}
pairs(propr:::proprCLR(Y[, 1:4])) # relative clr-transformation
```

```{r, dpi = 66, fig.width = 8, fig.height = 8, fig.show = "hold"}
pairs(propr:::proprCLR(X)) # absolute clr-transformation
```

Proportionality is a compromise between the advantages of VLR and the disadvantages of clr to establish a measure of dependence that is robust yet interpretable. As such, spurious proportionality is possible when the clr does not adequately approximate an ideal reference.

```{r, message = FALSE}
propr(Y[, 1:4])@matrix # relative proportionality with clr
```

```{r, message = FALSE}
propr(X)@matrix # absolute proportionality with clr
```

## An in-depth look at alr

The **additive log-ratio** (alr) adjusts each subject vector by the value of one its own components, chosen as a *reference*. If we select as a reference some feature $D$ with an *a priori* known fixed absolute count across all subjects, we can effectively "back-calculate" absolute data from relative data. When initially crafting the data $X$, we included "e" as this fixed value.

The following figures compare pairwise scatterplots for alr-transformed relative count data (i.e., $\textrm{alr}(Y)$ with "e" as the reference) and the corresponding absolute count data. We see here how the alr-transformation can eliminate the *spurious correlation* between "c" and "d".

```{r, dpi = 66, fig.width = 8, fig.height = 8, fig.show = "hold"}
pairs(propr:::proprALR(Y, ivar = 5)) # relative alr
```

```{r, dpi = 66, fig.width = 8, fig.height = 8, fig.show = "hold"}
pairs(X[, 1:4]) # absolute data
```

Again, this gets reflected in the results of `propr` when we select "e" as the reference.

```{r, message = FALSE}
propr(Y, ivar = 5)@matrix # relative proportionality with alr
```
