---
title: "Multiple Bias Modeling"
author: "Denis Haine"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Multiple Bias Modeling}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

Epidemiologic studies can suffer from more than one bias.
Bias functions in `episensr` can be applied sequentially to quantify bias
resulting from multiple biases.

Following the example in [Lash et
al.](https://link.springer.com/book/10.1007/978-3-030-82673-4), we can use the study by [Chien et
al.](https://link.springer.com/article/10.1007/s10549-005-9056-0).
It is a case-control study looking at the association between antidepressant use and the
occurrence of breast cancer. The observed OR was 1.2 [0.9--1.6].

```{r chien}
chien <- matrix(c(118, 832, 103, 884),
				dimnames = list(c("BC+", "BC-"), c("AD+", "AD-")),
				nrow = 2, byrow = TRUE)
```

```{r chien-tab, echo=FALSE}
knitr::kable(chien)
```

Records on medication use differed between participants, from pharmacy records
and self-reported use, leading to misclassification:

```{r misclass_chien}
library(episensr)
seq_bias1 <- chien %>%
	misclass(., type = "exposure",
			 bias_parms = c(24/(24+19), 18/(18+13), 144/(144+2), 130/(130+4)))
seq_bias1
```

Controls and cases also enrolled into the study at different rates.
By storing the result of `misclass()` in an object named here `seq_bias1`, we have access to the various elements returned by the function (see the `help()` for a given function to know what is returned).
The bias-adjusted cell frequencies can be accessed as `corr_data`.

```{r chien-tab-misclass}
seq_bias1$corr_data
```

This 2-by-2 table will be used as starting values for the next sequential bias adjustment, `selection()`.

```{r misclass_sel}
seq_bias2 <- seq_bias1$corr_data %>%
	selection(., bias_parms = c(.734, .605, .816, .756))
seq_bias2
```

The adjusted OR is now `r round(seq_bias2$adj_measures[2, 1], 2)`
Again, the bias-adjusted cell frequencies can be used in the next bias analysis, `confounders()`.

```{r chien-tab-selection}
seq_bias2$corr_data
```

The association between antidepressant use and breast cancer was adjusted for
various confounders (race/ethnicity, income, etc.).
None of these confounders were found to change the association by more than 10%.
However, for illustration, we can add the effect of a potential confounder (e.g.
physical activity):

```{r misclass_sel_conf}
seq_bias3 <- seq_bias2$corr_data %>%
	confounders(., type = "OR", bias_parms = c(.8, .299, .436))
seq_bias3
```

The serially bias-adjusted OR is `r round(seq_bias3$adj_measures[2, 1], 3)`.
And the adjusted cells by confounders, for regular exercise:

```{r chien-tab-conf1}
seq_bias3$cfder_data
```

And for no regular exercise:

```{r chien-tab-conf2}
seq_bias3$nocfder_data
```

The same process can be realized in a probabilistic framework, as each `probsens()`, `probsens.sel()` and `probsens_conf()` provides the adjusted cell frequencies as A1, B1, C1, D1 in `sim_df`.

|          | Exposed | Not exposed |
|---------:|:-------:|:-----------:|
|    Cases | A1      | B1          |
| Controls | C1      | D1          |

```{r multi_prob}
mod1 <- chien %>%
	probsens(., type = "exposure",
			 seca = list("trapezoidal", c(.45, .5, .6, .65)),
			 seexp = list("trapezoidal", c(.4, .48, .58, .63)),
			 spca = list("trapezoidal", c(.95, .97, .99, 1)),
			 spexp = list("trapezoidal", c(.96, .98, .99, 1)),
			 corr_se = .8, corr_sp = .8)
str(mod1)
```

Each of the lines from `mod1$sim_df[, 5:8]` can then be tabulated and fed to the next bias function.
Be careful, as the number of observations can quickly become unmanageable.

```{r head_cells}
head(mod1$sim_df[, 5:8])
```