install.packages("tidyverse")
install.packages("ggpubr")
install.packages("rstatix")
install.packages("datarium")Two-way ANOVA
Prerequisites
Make sure you have the following R packages:
tidyverse for data manipulation and visualization
ggpubr for creating easily publication ready plots
rstatix provides pipe-friendly R functions for easy statistical analyses
datarium: contains required data sets for this chapter Load required R packages:
Get them this way:
Once they’re installed, you don’t have to do this again. You can find a list of all installed packages on the right side of R Studio:
But even if they’re installed, you do still have to load them into memory every time so that the rest of your code works. Load them using the “library” command, like this:
Alternatively, you can skip the code and simply check the box next to each package that you want to load into memory. That is more likely to cause errors, and will cause problems if you share your code with someone else.
Key R functions: anova_test() [rstatix package], wrapper around the function car::Anova().
Data preparation
We’ll use the jobsatisfaction dataset [datarium package], which contains the job satisfaction score organized by gender and education levels.
In this study, a research wants to evaluate if there is a significant two-way interaction between gender and education_level on explaining the job satisfaction score. An interaction effect occurs when the effect of one independent variable on an outcome variable depends on the level of the other independent variables. If an interaction effect does not exist, main effects could be reported.
Load the data and inspect one random row by groups:
library(datarium)Warning: package 'datarium' was built under R version 4.2.3
library(dplyr)
set.seed(123)
data("jobsatisfaction", package = "datarium") In this example, the effect of “education_level” is our focal variable, that is our primary concern. It is thought that the effect of “education_level” will depend on one other factor, “gender”, which are called a moderator variable.