# another example of factorial analysis of variance
# the library() function loads the modules that we need
library(gmodels)
library(psych)
library(pastecs)
# next, we open the data file we want to analyze and add it to the search string
ANES2008<-read.csv("http://www.courseserve.info/files/ANES2008r.csv")
attach(ANES2008)
# we can look at the means on the index variable by groups and their combinations
# our dependent variable is favorability toward atheists
by(V085065H, V081101, stat.desc) # gender (1=men)
by(V085065H, V081204, stat.desc) # region (1=NE, 2=NC, 3=S, 4=W)
by(V085065H, list(V081101, V081204), stat.desc)
# now we can calculate the analysis of variance -- the means test for more than two groups
# note that this is a nested function; lm() calculates the model and summary() prints the statistics
# the model is for two main effects and an interaction -- the third term on the right is the interaction
summary(lm(V085065H ~ V081101 + V081204 + V081101:V081204))
# there is a short-hand notation for a model such as this. we supply the interaction term using '*'
# and the main effects are implied
summary(lm(V085065H ~ V081101*V081204))
# it is helpful to plot the means to look for the interaction
# we use the conditional [...] attached to each variable to eliminate the missing cases
interaction.plot(V081101[V081101>0&V081204>0&V085065H>0],V081204[V081101>0&V081204>0&V085065H>0],V085065H[V081101>0&V081204>0&V085065H>0],ylim=c(45,55))