inst/doc/anes48.R

### R code from vignette source 'anes48.Rnw'

###################################################
### code chunk number 1: anes48.Rnw:71-72
###################################################
options(width=72)


###################################################
### code chunk number 2: anes48.Rnw:74-78
###################################################
library(mtable)
library(codebooks)
options(digits=3)
nes1948.por <- UnZip("anes/NES1948.ZIP","NES1948.POR",package="codebooks")


###################################################
### code chunk number 3: anes48.Rnw:92-94
###################################################
nes1948 <- spss.portable.file(nes1948.por)
print(nes1948)


###################################################
### code chunk number 4: anes48.Rnw:99-100
###################################################
names(nes1948)


###################################################
### code chunk number 5: anes48.Rnw:103-104
###################################################
description(nes1948)


###################################################
### code chunk number 6: anes48.Rnw:107-108 (eval = FALSE)
###################################################
## codebook(nes1948)


###################################################
### code chunk number 7: anes48.Rnw:131-143
###################################################
vote.48 <- subset(nes1948,
              select=c(
                  v480018,
                  v480029,
                  v480030,
                  v480045,
                  v480046,
                  v480047,
                  v480048,
                  v480049,
                  v480050
                  ))


###################################################
### code chunk number 8: anes48.Rnw:154-155
###################################################
str(vote.48)


###################################################
### code chunk number 9: anes48.Rnw:178-189
###################################################
vote.48 <- rename(vote.48,
                  v480018 = "vote",
                  v480029 = "occupation.hh",
                  v480030 = "unionized.hh",
                  v480045 = "gender",
                  v480046 = "race",
                  v480047 = "age",
                  v480048 = "education",
                  v480049 = "total.income",
                  v480050 = "religious.pref"
        )


###################################################
### code chunk number 10: anes48.Rnw:193-194
###################################################
codebook(vote.48)


###################################################
### code chunk number 11: anes48.Rnw:232-254
###################################################
vote.48 <- within(vote.48,{
  vote3 <- recode(vote,
    1 -> "Truman",
    2 -> "Dewey",
    3:4 -> "Other"
    )
  occup4 <- recode(occupation.hh,
    10:20 -> "Upper white collar",
    30 -> "Other white collar",
    40:70 -> "Blue collar",
    80 -> "Farmer"
    )
  relig3 <- recode(religious.pref,
    1 -> "Protestant",
    2 -> "Catholic",
    3:5 -> "Other,none"
    )
   race2 <- recode(race,
    1 -> "White",
    2 -> "Black"
    )
  })


###################################################
### code chunk number 12: anes48.Rnw:265-266
###################################################
toLatex(xtabs(~vote3+occup4,data=vote.48))


###################################################
### code chunk number 13: anes48.Rnw:274-276
###################################################
toLatex(t(genTable(percent(vote3)~occup4,data=vote.48)),
  digits=c(1,1,1,0))


###################################################
### code chunk number 14: anes48.Rnw:286-288
###################################################
toLatex(t(genTable(percent(vote3)~relig3,data=vote.48)),
  digits=c(1,1,1,0))


###################################################
### code chunk number 15: anes48.Rnw:296-298
###################################################
toLatex(t(genTable(percent(vote3)~race2,data=vote.48)),
  digits=c(1,1,1,0))


###################################################
### code chunk number 16: anes48.Rnw:307-310
###################################################
inc.tab <- t(genTable(percent(vote3)~total.income,data=vote.48))
rownames(inc.tab) <- gsub("$","\\$",rownames(inc.tab),fixed=TRUE)
toLatex(inc.tab,digits=c(1,1,1,0))


###################################################
### code chunk number 17: anes48.Rnw:321-325
###################################################
vote.48 <- within(vote.48,{
  contrasts(occup4) <- contr("treatment",base = 3)
  contrasts(total.income) <- contr("treatment",base = 4)
  })


###################################################
### code chunk number 18: anes48.Rnw:336-346
###################################################
model1 <- glm((vote3=="Truman")~occup4,data=vote.48,
              family="binomial")
model2 <- glm((vote3=="Truman")~total.income,data=vote.48,
              family="binomial")
model3 <- glm((vote3=="Truman")~occup4+total.income,data=vote.48,
              family="binomial")
model4 <- glm((vote3=="Truman")~relig3,data=vote.48,
              family="binomial")
model5 <- glm((vote3=="Truman")~occup4+relig3,data=vote.48,
              family="binomial")


###################################################
### code chunk number 19: anes48.Rnw:353-354
###################################################
mtable(model1,model2,model3,summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N"))


###################################################
### code chunk number 20: anes48.Rnw:364-379
###################################################
toLatex(relabel(mtable(
            "Model 1"=model1,
            "Model 2"=model2,
            "Model 3"=model3,
            summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N")),
          "[(]Intercept[)]"="\\\\emph{Intercept}",
          "[$]"="\\\\$",
          UNDER="under",
          "AND OVER"="and over",
          occup4="Occup. class",
          total.income="Income",
          gsub=TRUE
          ),
          ddigits=5
        )


###################################################
### code chunk number 21: anes48.Rnw:396-407
###################################################
toLatex(relabel(mtable(
              "Model 1"=model1,
              "Model 4"=model4,
              "Model 5"=model5,
              summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N")),
          "[(]Intercept[)]"="\\\\emph{Intercept}",
            occup4="Occup. class",
            relig3="Religion",
            gsub=TRUE
            ),
          ddigits=5)

Try the codebooks package in your browser

Any scripts or data that you put into this service are public.

codebooks documentation built on May 2, 2019, 5:26 p.m.