---
title: "Statistics for Anova Tables"
author: "Daniel Lüdecke"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Statistics for Anova Tables}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r set-options, echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", dev = "png", fig.width = 7, fig.height = 3.5, message = FALSE, warning = FALSE)
options(width = 800)
```

# Effect Size Statistics for Anova Tables

This vignettes demontrates those functions of the *sjstats*-package that deal with Anova tables. These functions report different effect size measures, which are useful beyond significance tests (p-values), because they estimate the magnitude of effects, independent from sample size. *sjstats* provides following functions:

* `eta_sq()`
* `omega_sq()`
* `epsilon_sq()`
* `anova_stats()`

Befor we start, we fit a simple model:

```{r}
library(sjstats)
# load sample data
data(efc)

# fit linear model
fit <- aov(
  c12hour ~ as.factor(e42dep) + as.factor(c172code) + c160age,
  data = efc
)
```

All functions accept objects of class `aov` or `anova`, so you can also use model fits from the *car* package, which allows fitting Anova's with different types of sum of squares. Other objects, like `lm`, will be coerced to `anova` internally. 

The following functions return the effect size statistic as named numeric vector, using the model's term names.

## Eta-Squared

The eta-squared is the proportion of the total variability in the dependent variable that is accounted for by the variation in the independent variable. It is the ratio of the sum of squares for each group level to the total sum of squares. It can be interpreted as percentage of variance accounted for by a variable.

For variables with 1 degree of freedeom (in the numerator), the square root of eta-squared is equal to the correlation coefficient _r_. For variables with more than 1 degree of freedom, eta-squared equals _R2_. This makes eta-squared easily interpretable. Furthermore, these effect sizes can easily be converted into effect size measures that can be, for instance, further processed in meta-analyses.

Eta-squared can be computed simply with:

```{r}
eta_sq(fit)
```

## Partial Eta-Squared

The partial eta-squared value is the ratio of the sum of squares for each group level to the sum of squares for each group level plus the residual sum of squares. It is more difficult to interpret, because its value strongly depends on the variability of the residuals. Partial eta-squared values should be reported with caution, and Levine and Hullett (2002) recommend reporting eta- or omega-squared rather than partial eta-squared.

Use the `partial`-argument to compute partial eta-squared values:

```{r}
eta_sq(fit, partial = TRUE)
```


## Omega-Squared

While eta-squared estimates tend to be biased in certain situations, e.g. when the sample size is small or the independent variables have many group levels, omega-squared estimates are corrected for this bias.

Omega-squared can be simply computed with:


```{r}
omega_sq(fit)
```

## Partial Omega-Squared

`omega_sq()` also has a `partial`-argument to compute partial omega-squared values. Computing the partial omega-squared statistics is based on bootstrapping. In this case, use `n` to define the number of samples (1000 by default.)


```{r}
omega_sq(fit, partial = TRUE, n = 100)
```

# Epsilon Squared

Espilon-squared is a less common measure of effect size. It is sometimes considered as an "adjusted r-squared" value. You can compute this effect size using `epsilon_sq()`.

```{r}
epsilon_sq(fit)
```

When the `ci.lvl`-argument is defined, bootstrapping is used to compute the confidence intervals.

```{r}
epsilon_sq(fit, ci.lvl = .95, n = 100)
```

# Complete Statistical Table Output

The `anova_stats()` function takes a model input and computes a comprehensive summary, including the above effect size measures, returned as tidy data frame:

```{r}
anova_stats(fit)
```

Like the other functions, the input may also be an object of class `anova`, so you can also use model fits from the *car* package, which allows fitting Anova's with different types of sum of squares:

```{r}
anova_stats(car::Anova(fit, type = 3))
```

# Confidence Intervals

`eta_sq()` and `omega_sq()` have a `ci.lvl`-argument, which - if not `NULL` - also computes a confidence interval.

For eta-squared, i.e. `eta_sq()` with `partial = FALSE`, due to non-symmetry, confidence intervals are based on bootstrap-methods. Confidence intervals for partial omega-squared, i.e. `omega_sq()` with `partial = TRUE` - is also based on bootstrapping. In these cases, `n` indicates the number of bootstrap samples to be drawn to compute the confidence intervals. 

```{r}
eta_sq(fit, partial = TRUE, ci.lvl = .8)

# uses bootstrapping - here, for speed, just 100 samples
omega_sq(fit, partial = TRUE, ci.lvl = .9, n = 100)
```

# References

Levine TR, Hullet CR. Eta Squared, Partial Eta Squared, and Misreporting of Effect Size in Communication Research. Human Communication Research 28(4); 2002: 612-625
