# We'll create a simple index variable as a sum of a series of related

# variables. This makes sense if all the variables are on the same

# answer set and if we can identify an order to the answer set -- that

# is, the level of measurement is ordinal.

#

# It will be necessary to recode the variables in this example prior

# to creating the sum score.

ANES2012<-read.csv("http://www.courseserve.info/files/ANES2012r.csv")

attach(ANES2012)

# We'll use a series of questions measuring attitude toward federal

# spending. The original variables are measured on a three point answer

# set, where 1=increase, 2=decrease, and 3=keep same. We can consider

# this an ordinal scale, from liberal to conservative (with regard to

# the role of government), if we recode to put the 'keep same' answer

# in the middle.

spend1=0; spend1[fedspend_ss==1]<-2; spend1[fedspend_ss==2]<-0; spend1[fedspend_ss==3]<-1

spend2=0; spend2[fedspend_schools==1]<-2; spend2[fedspend_schools==2]<-0; spend2[fedspend_schools==3]<-1

spend3=0; spend3[fedspend_scitech==1]<-2; spend3[fedspend_scitech==2]<-0; spend3[fedspend_scitech==3]<-1

spend4=0; spend4[fedspend_crime==1]<-2; spend4[fedspend_crime==2]<-0; spend4[fedspend_crime==3]<-1

spend5=0; spend5[fedspend_welfare==1]<-2; spend5[fedspend_welfare==2]<-0; spend5[fedspend_welfare==3]<-1

spend6=0; spend6[fedspend_child==1]<-2; spend6[fedspend_child==2]<-0; spend6[fedspend_child==3]<-1

spend7=0; spend7[fedspend_poor==1]<-2; spend7[fedspend_poor==2]<-0; spend7[fedspend_poor==3]<-1

spend8=0; spend8[fedspend_enviro==1]<-2; spend8[fedspend_enviro==2]<-0; spend8[fedspend_enviro==3]<-1

# Now we can create an index variable by summing the new spendX variables.

spending<-spend1+spend2+spend3+spend4+spend5+spend6+spend7+spend8

# Let's look at another example of recoding.

married=0; married<-ifelse(dem_marital==1,1,0)

# Now let's build a least squares regression model to predict attitude toward spending.

summary(lm(spending~married+dem_edugroup+dem_unionhh+gender_respondent+libcpo_self))