# 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
by(V085064J, V081101, stat.desc) # gender (1=men)
by(V085064J, V083093, stat.desc) # which party better on economy (1=Dems, 2=GOP, 3=no diff)
by(V085064J, list(V081101, V083093), 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(V085064J ~ V081101 + V083093 + V081101:V083093))
# 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(V085064J ~ V081101*V083093))
# 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&V083093>0&V085064J>0],V083093[V081101>0&V083093>0&V085064J>0],V085064J[V081101>0&V083093>0&V085064J>0],ylim=c(40,70))