# 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
ABC2010<-read.csv("http://www.courseserve.info/files/ABC2010r.csv")
attach(ABC2010)
# we create a index variable by combining answers on a series of related questions
Q12r=0; for (i in 1:length(Q12)) { if(is.na(Q12[i])==FALSE & Q12[i]==5) Q12r[i]=NA else Q12r[i]=Q12[i] }
OilSpill<-Q11_1+Q11_2+Q11_3+Q12r # after recoding an answer on Q12 as missing, this creates the index
# we can inspect the new index variable
# a lower score indicates an attitude of more blame
CrossTable(OilSpill, prop.r=F, prop.c=T, prop.t=F, prop.chisq=F, chisq = F)
# Since this index variable is numeric (even though the original variables were categorical)
# we can look at central tendency and variability and the confidence interval
describe(OilSpill)
t.test(OilSpill, mu=10)
# we can look at the means on the index variable by group
by(OilSpill, Q22, stat.desc) #Q22 is attitude toward the Tea Party (1=strongly fav)
# 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
summary(lm(OilSpill ~ Q22))
# there is another way to produce the output that is a bit more readable
summary(aov(OilSpill ~ Q22))
# now we need to do post-hoc tests to determine where the significant differences lie
pairwise.t.test(OilSpill, Q22, p.adjust.method="bonferroni")