---
title: "Analysis of Deviance"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Analysis of Deviance}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# Classic One-Way ANOVA

As an introduction, three random variables following a normal distribution with a common standard deviation are created. For this test, the null hypothesis is

$$ H_{0}: \mu_0 = \mu_1 = \mu_2  $$

```{r part1}
library(LRTesteR)

set.seed(123)
x <- c(
  rnorm(n = 50, mean = 1, sd = 1),
  rnorm(n = 50, mean = 3, sd = 1),
  rnorm(n = 50, mean = 5, sd = 1)
)
fctr <- c(rep(1, 50), rep(2, 50), rep(3, 50))
fctr <- factor(fctr, levels = c("1", "2", "3"))
gaussian_mu_one_way_test(x = x, fctr = fctr, conf.level = 0.95)
```
Statistically, there are a number of things worth noting:

* The statistic is the log likelihood statistic, not the F statistic.
* The p value is based on the chi-squared distribution, not the F distribution.
* The individual confidence intervals use the Bonferroni correction.

For a normal distribution, none of this is optimal. The F statistic is exact while the log likelihood statistic is only an asymptotic approximation, and the Bonferroni correction leads to the widest confidence intervals of any correction technique.

The principal advantage of the likelihood approach is its generality. The same machinery applies nonparametrically and to other distributions. The rest of this vignette works through both.

# Nonparametric One-Way ANOVA

The same comparison of means, without assuming the data are normally distributed. The null hypothesis is unchanged.

$$ H_{0}: \mu_0 = \mu_1 = \mu_2  $$

```{r part2}
empirical_mu_one_way_test(x = x, fctr = fctr, conf.level = 0.95)
```

# Nonparametric Equality of Variance

Reusing the same data, the equality of variances is tested nonparametrically. The null hypothesis is

$$ H_{0}: \sigma^2_0 = \sigma^2_1 = \sigma^2_2  $$

```{r part3}
empirical_variance_one_way_test(x = x, fctr = fctr, conf.level = 0.95)
```

# Cauchy Random Variables

Here two random variables following a Cauchy distribution with a common location and different scales are created. For this test, the null hypothesis is 

$$ H_{0}: \gamma_0 = \gamma_1  $$

```{r part4}
set.seed(1)
x <- c(rcauchy(n = 50, location = 2, scale = 1), rcauchy(n = 50, location = 2, scale = 3))
fctr <- c(rep(1, 50), rep(2, 50))
fctr <- factor(fctr, levels = c("1", "2"))
cauchy_scale_one_way_test(x = x, fctr = fctr, conf.level = 0.95)
```

# Poisson Random Variables

Finally, three poisson random variables with different lambdas are created. The null hypothesis is

$$ H_{0}: \lambda_0 = \lambda_1 = \lambda_2  $$

```{r part5}
set.seed(1)
x <- c(rpois(n = 50, lambda = 1), rpois(n = 50, lambda = 2), rpois(n = 50, lambda = 3))
fctr <- c(rep(1, 50), rep(2, 50), rep(3, 50))
fctr <- factor(fctr, levels = c("1", "2", "3"))
poisson_lambda_one_way_test(x = x, fctr = fctr, conf.level = 0.95)
```
