-
Notifications
You must be signed in to change notification settings - Fork 6
/
README.Rmd
306 lines (206 loc) · 14.7 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "80%"
)
library(supernova)
set.seed(123)
```
# supernova <img src='man/figures/logo.png' align="right" height="138" />
<!-- badges: start -->
[![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)
<!-- badges: end -->
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