#Two way Analysis of variance for balanced designs (equal group size) install.packages("car") library(car) my_data <- read.table("twaov.txt", header=T) attach(my_data) #####Two-way ANOVA without interaction##### #Type does not matter in a balanced design# model2 <- lm(activity ~ predator_density + timing, data = my_data) Anova(model2,type="II") ####Check model assumptions#### #Assumption 1: Normal distribution of the model residuals plot(model2,2) #Assumption 2: Homogeneity of variance of the groups plot(model2,1) #Post-hoc test: In case of a non-significant interaction effect, we conduct tests for each main effect seperately install.packages("lsmeans") library("lsmeans") lsmeans(model2, pairwise ~ predator_density) lsmeans(model2, pairwise ~ timing) #####data visualization##### install.packages("ggplot2") library(ggplot2) ggplot(aes(y = activity, x = timing, fill = predator_density), data = my_data) + geom_boxplot() + theme_bw()