tidy-anova.Rmd
The tidy_anova()
function can be used to create an ANOVA
table that is outputted as a data frame. This is useful for
implementation with kable()
and other R functions. The
function takes as its input an lm
object.
# Fit model with lm()
lm.1 = lm(mpg ~ 1 + wt + hp, data = mtcars)
# Load educate package
library(educate)
# Obtain ANOVA table
tidy_anova(lm.1)
## Source df SS MS F p
## 1 wt 1 847.72525 847.725250 126.04109 4.488360e-12
## 2 hp 1 83.27418 83.274183 12.38133 1.451229e-03
## 3 Residuals 29 195.04775 6.725785 NA NA
The function takes an optional argument of model=TRUE
.
This argument will create the model-level ANOVA table that is commonly
presented in APA publications.
# Obtain model-level ANOVA table
tidy_anova(lm.1, model = TRUE)
## Source df SS MS F p
## 1 Model 2 930.9994 465.499716 69.21121 9.109047e-12
## 2 Residuals 29 195.0478 6.725785 NA NA
Since tidy_anova()
outputs a data frame, you can use
knitr’s kable()
function to make it look
great in a knitted document.
#Load libraries
library(dplyr)
library(knitr)
# Set knitr options to remove NA values
options(knitr.kable.NA = '')
# Obtain model-level ANOVA table and make it pretty
tidy_anova(lm.1, model = TRUE) %>%
kable(digits = 2)
Source | df | SS | MS | F | p |
---|---|---|---|---|---|
Model | 2 | 931.00 | 465.50 | 69.21 | 0 |
Residuals | 29 | 195.05 | 6.73 |