Skip to content

Instantly share code, notes, and snippets.

@dalejbarr
Created November 4, 2016 12:26
Show Gist options
  • Save dalejbarr/138a2d10b1aec2de2d3133a01d0302d3 to your computer and use it in GitHub Desktop.
Save dalejbarr/138a2d10b1aec2de2d3133a01d0302d3 to your computer and use it in GitHub Desktop.
script for video walkthrough on using R/RStudio for 3-way ANOVA
## youtube video: https://youtu.be/AJDo9gpkEcg
library("readr")
library("ggplot2")
## read.csv() <- from base R. DON'T USE!
hw5 <- read_csv("homework_5.csv") # from readr
hw5$A <- factor(hw5$A)
hw5$B <- factor(hw5$B)
hw5$C <- factor(hw5$C)
## aov(Y ~ A + B + C + A:B + A:C + B:C + A:B:C, hw5)
my_anova <- aov(Y ~ A * B * C, hw5)
summary(my_anova)
## base R interaction.plot()
## ggplot2 geom_line() geom_point()
## dplyr: filter
## logical expression:
## an expression that returns
## TRUE or FALSE
hw5_c1 <- subset(hw5, C == "C1")
hw5_c2 <- subset(hw5, C == "C2")
par(mfrow = c(1, 2))
## interaction.plot(hw5_c1$A,
## hw5_c1$B,
## hw5_c1$Y)
with(hw5_c1, interaction.plot(A, B, Y, legend = TRUE))
## interaction.plot(hw5_c2$A,
## hw5_c2$B,
## hw5_c2$Y)
with(hw5_c2, interaction.plot(A, B, Y, legend = TRUE))
cell_means <- aggregate(Y ~ A + B + C,
hw5, mean)
ggplot(cell_means,
aes(A, Y, linetype = B, shape = B, group = B)) +
geom_line() + geom_point(size = 3) +
facet_wrap(~C)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment