Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/UCLATALL/supernova

An R package for teaching statistics from a modeling perspective
https://github.com/UCLATALL/supernova

Last synced: 3 months ago
JSON representation

An R package for teaching statistics from a modeling perspective

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "80%"
)

library(supernova)
set.seed(123)
```

# supernova

[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/supernova)](https://cran.r-project.org/package=supernova)
[![R build status](https://github.com/uclatall/supernova/workflows/R-CMD-check/badge.svg)](https://github.com/uclatall/supernova/actions)
[![codecov](https://codecov.io/gh/uclatall/supernova/branch/main/graph/badge.svg?token=HEenoYyHcn)](https://app.codecov.io/gh/uclatall/supernova)

The goal of `supernova` is to create ANOVA tables in the format used by Judd, McClelland, and Ryan (2017, ISBN: 978-1138819832) in their introductory textbook, *Data Analysis: A Model Comparison Approach to Regression, ANOVA, and Beyond* [(book website)](http://www.dataanalysisbook.com/index.html).\* These tables include proportional reduction in error, a useful measure for teaching the underlying concepts of ANOVA and regression, and formatting to ease the transition between the book and R.

*\* Note: we are NOT affiliated with the authors or their institution.*

In keeping with the approach in Judd, McClelland, and Ryan (2017), the ANOVA tables in this package are calculated using a model comparison approach that should be understandable given a beginner's understanding of base R and the information from the book (so if you have these and don't understand what is going on in the code, let us know because we are missing the mark!). Here is an explanation of how the tables are calculated for fully independent predictor variables (i.e. between-subjects designs):

1. The "Total" row is calculated by updating the model passed to an empty model. For example, `lm(mpg ~ hp * disp, data = mtcars)` is updated to `lm(mpg ~ NULL, data = mtcars)`. From this empty model, the sum of squares and df can be calculated.

2. If there is at least one predictor in the model, the overall model row and the error row are calculated. In the vernacular of the book, the compact model is represented by the updated empty model from 1 above, and the augmented model is the original model passed to ```supernova()```. From these models the SSE(A) is calculated by `sum(resid(null_model) ^ 2)`, the SSR is calculated by SSE(C) - SSE(A), the PRE for the overall model is extracted from the fit (`r.squared`), the df for the error row is extracted from the `lm()` fit (`df.residuals`).

3. If there are more than one predictors, the single term deletions are computed using `drop1()`. For the model `y ~ a * b` (which expands to `y ~ a + b + a:b`, where `a:b` is the interaction of `a` and `b`), `drop1()` essentially creates three models each with one term removed: `y ~ a + a:b`, `y ~ b + a:b`, and `y ~ a + b`. These models are considered the compact models which do not include the tested terms `a`, `b`, and `a:b`, respectively. `drop1()` computes the SSR (`Sum Sq`) and SSE(C) (`RSS`) for each of these augmented and compact model pairs, and these values are used to compute the SSR and PRE for each.

4. Finally, the `MS` (`SS / df`), `F` (`MSR / MSE`), and `p` columns are calculated from already-computed values in the table.

### Supported models

The following models are explicitly tested and supported by `supernova()`, _for independent samples (between-subjects) data only_. For these models, there is also support for datasets with missing or unbalanced data.

* empty models: `y ~ NULL`
* simple regression: `y ~ a`
* multiple regression: `y ~ a + b`
* interactive regression: `y ~ a * b`

Additionally, a subset of within-subjects designs are supported and explicitly tested. To accommodate these models `supernova()` can accept models fit via `lmer()` as in the [Examples](#examples) below. Only models like those included in those examples have been tested for within-subjects designs.

Anything not included above is not (yet) explicitly tested and may yield errors or incorrect statistics. This includes, but is not limited to

* one-sample _t_-tests

### Other features

In addition to the ANOVA table provided by `supernova()`, the `supernova` package provides some useful functions for teaching ANOVA and pairwise comparisons:

**Generate models**: Generate the models that were compared to create each row of an ANOVA table using `generate_models()`. This can be done for each of the different SS Types as described in [Using Different SS Types](#using-different-ss-types) below.

**Pairwise comparisons**: Test each categorical group in a model against the others using `pairwise()`. This function supports Tukey and Bonferroni corrections. See the [Pairwise Comparisons](#pairwise-comparisons) section below.

## Installing

You can install the released version of supernova from [CRAN](https://CRAN.R-project.org) with:

```{r, eval=FALSE}
install.packages("supernova")
```

Alternatively you can download the package directly from this repository using `remotes`:

```{r, eval=FALSE}
library(remotes)
install_github("UCLATALL/supernova")
```

## Examples

Here are some basic examples of the code and output for this package:

### Between Subjects Models

#### A model with no predictors (null model)

```{r}
supernova(lm(mpg ~ NULL, data = mtcars))
```

#### A regression model with a single predictor
```{r}
supernova(lm(mpg ~ hp, data = mtcars))
```

#### A multiple regression model
```{r}
supernova(lm(mpg ~ hp + disp, data = mtcars))
```

#### A multiple regression with an interaction
```{r}
supernova(lm(mpg ~ hp * disp, data = mtcars))
```

#### Turn off the description column
```{r}
supernova(lm(mpg ~ hp * disp, data = mtcars), verbose = FALSE)
```

### Within Subjects Models

First let's load up `lme4` which gives us `lmer()`, the function we will use to fit within-subjects models. Additionally, install and load the `JMRData` package which has some short datasets with non-independent observations, and load `dplyr` and `tidyr` so that we can tidy the data.

```{r, message = FALSE}
# Run this line if you do not have the JMRData package
# remotes::install_github("UCLATALL/JMRData")

library(lme4)
library(tidyr)
library(dplyr)

simple_crossed <- JMRData::ex11.9 |>
gather(condition, puzzles_completed, -subject) |>
mutate_at(vars(subject, condition), as.factor)

multiple_crossed <- JMRData::ex11.17 |>
gather(condition, recall, -Subject) |>
separate(condition, c("type", "time"), -1) |>
mutate(across(c(Subject, type, time), as.factor))
```

#### A one-way within-subjects design (crossed)

Fitting the `simple_crossed` data with `lm()` would ignore the non-independence due to observations coming from the same `subject`. Compare this output with the following output where the model was fit with `lmer()` and the specification of `subject` as a random factor:

```{r}
simple_crossed |>
lm(puzzles_completed ~ condition, data = _) |>
supernova(verbose = FALSE)

# use lmer() to specify the non-independence
simple_crossed |>
lmer(puzzles_completed ~ condition + (1 | subject), data = _) |>
supernova()
```

#### A two-way within-subjects design (crossed)

Here is another example like the previous, but here multiple variables (`time`, `type`) and their interaction have been specified:

```{r}
# fitting this with lm would ignore the non-independence due to Subject
multiple_crossed |>
lm(recall ~ type * time, data = _) |>
supernova(verbose = FALSE)

# using lmer() we can specify the non-independence
multiple_crossed |>
lmer(recall ~ type * time + (1 | Subject) + (1 | type:Subject) + (1 | time:Subject), data = _) |>
supernova()
```

#### A one-way between-groups design (nested)

In this example, each person in a group of three generates a rating. If we fit the data with `lm()` that would ignore the non-independence due to the people being in the same `group`. Compare this output with the following output where the `group` is specified as a random factor.

```{r}
simple_nested <- JMRData::ex11.1 |>
gather(id, value, starts_with("score")) |>
mutate(across(c(group, instructions, id), as.factor))

# fitting this with lm would ignore the non-independence due to group
simple_nested |>
lm(value ~ instructions, data = _) |>
supernova(verbose = FALSE)

# using lmer() we can specify the non-independence
simple_nested |>
lmer(value ~ instructions + (1 | group), data = _) |>
supernova()
```

#### A three-way design with two between-groups variables and a nested variable

In this example, each person in heterosexual marriage generates a rating of satisfaction. Additionally, these couples were chosen such that they either have children or not, and have been married 15 vs. 30 years. If we fit the data with `lm()` that would ignore the non-independence due to the people being in the same `couple`. Compare this output with the following output where the `group` is specified as a random factor.

```{r}
complex_nested <- JMRData::ex11.22 |>
gather(sex, rating, Male, Female) |>
mutate(across(c(couple, children, sex, yearsmarried), as.factor))

# fitting this with lm would ignore the non-independence due to group
complex_nested |>
lm(rating ~ sex * yearsmarried * children, data = _) |>
supernova(verbose = FALSE)

# using lmer() we can specify the non-independence
complex_nested |>
lmer(rating ~ sex * yearsmarried * children + (1 | couple), data = _) |>
supernova()
```

### Using Different SS Types

#### Type III: Orthogonal (default)

```{r}
supernova(lm(mpg ~ hp * disp, data = mtcars))
```

These are equivalent to the above:

```{r, eval=FALSE}
supernova(lm(mpg ~ hp * disp, data = mtcars), type = 3)
supernova(lm(mpg ~ hp * disp, data = mtcars), type = "III")
supernova(lm(mpg ~ hp * disp, data = mtcars), type = "orthogonal")
```

#### Type I: Sequential

```{r}
supernova(lm(mpg ~ hp * disp, data = mtcars), type = 1)
```

These are equivalent to the above:

```{r, eval=FALSE}
supernova(lm(mpg ~ hp * disp, data = mtcars), type = "I")
supernova(lm(mpg ~ hp * disp, data = mtcars), type = "sequential")
```

#### Type II: Hierarchical

```{r}
supernova(lm(mpg ~ hp * disp, data = mtcars), type = 2)
```

These are equivalent to the above:

```{r, eval=FALSE}
supernova(lm(mpg ~ hp * disp, data = mtcars), type = "II")
supernova(lm(mpg ~ hp * disp, data = mtcars), type = "hierarchical")
```

### Displaying which models were compared

This package is based on a model comparison approach to understanding regression and ANOVA. As such it is useful to know which models are being compared for any given term in an ANOVA table. The `generate_models()` function accepts a linear model and the desired type of SS and returns a list of the models that should be compared to appropriately evaluate each term in the full model.

```{r}
generate_models(lm(mpg ~ hp * disp, data = mtcars), type = 2)
```

### Pairwise Comparisons

The `pairwise()` function takes a linear model and performs the requested pairwise comparisons on the categorical terms in the model. For simple one-way models where a single categorical variable predicts and outcome, you will get output similar to other methods of computing pairwise comparisons (e.g. `TukeyHSD()` or `t.test()`). Essentially, the differences on the outcome between each of the groups defined by the categorical variable are compared with the requested test, and their confidence intervals and *p*-values are adjusted by the requested `correction`.

However, when more than two variables are entered into the model, the outcome will diverge somewhat from other methods of computing pairwise comparisons. For traditional pairwise tests you need to estimate an error term, usually by pooling the standard deviation of the groups being compared. This means that when you have other predictors in the model, their presence is ignored when running these tests. For the functions in this package, we instead compute the pooled standard error by using the mean squared error (*MSE*) from the full model fit.

Let's take a concrete example to explain that. If we are predicting a car's miles-per-gallon (`mpg`) based on whether it has an automatic or manual transmission (`am`), we can create that linear model and get the pairwise comparisons like this:

```{r}
pairwise(lm(mpg ~ factor(am), data = mtcars))
```

The output of this code will is one table showing the comparison of manual and automatic transmissions with regard to miles-per-gallon. The pooled standard error is the same as the square root of the *MSE* from the full model.

In these data the `am` variable did not have any other values than *automatic* and *manual*, but we can imagine situations where the predictor has more than two levels. In these cases, the pooled SD would be calculated by taking the MSE of the full model (not of each group) and then weighting it based on the size of the groups in question (divide by *n*).

To improve our model, we might add the car's displacement (`disp`) as a quantitative predictor:

```{r}
pairwise(lm(mpg ~ factor(am) + disp, data = mtcars))
```

Note that the output still only has a table for `am`. This is because we can't do a pairwise comparison using `disp` because there are no groups to compare. Most functions will drop or not let you use this variable during pairwise comparisons. Instead, `pairwise()` uses the same approach as in the 3+ groups situation: we use the *MSE* for the full model and then weight it by the size of the groups being compared. Because we are using the MSE for the full model, the effect of `disp` is accounted for in the error term even though we are not explicitly comparing different displacements. **Importantly**, the interpretation of the outcome is different than in other traditional t-tests. Instead of saying, "there is a difference in miles-per-gallon based on the type of transmission," we must add that this difference is found "after accounting for displacement."

Finally, the output can be plotted either by using `plot()` on the returned object, or specifying `plot = TRUE`:

```{r}
output <- pairwise(lm(mpg ~ factor(am) + disp, data = mtcars))
plot(output)
```

This would produce an identical plot:

```r
pairwise(lm(mpg ~ factor(am) + disp, data = mtcars), plot = TRUE)
```

# Contributing

If you see an issue, problem, or improvement that you think we should know about, or you think would fit with this package, please let us know on our [issues page](https://github.com/UCLATALL/supernova/issues). Alternatively, if you are up for a little coding of your own, submit a pull request:

1. Fork it!
2. Create your feature branch: ```git checkout -b my-new-feature```
3. Commit your changes: ```git commit -am 'Add some feature'```
4. Push to the branch: ```git push origin my-new-feature```
5. Submit a [pull request](https://github.com/UCLATALL/supernova/pulls) :D