| 1 |
## ----echo=FALSE,message=FALSE------------------------------------------------- |
| 2 |
knitr::opts_chunk$set(comment=NA, |
| 3 |
fig.align="center", |
| 4 |
results="markup") |
| 5 |
|
| 6 |
## ----------------------------------------------------------------------------- |
| 7 |
knit_print.codebook <-function(x,...) |
| 8 |
knitr::asis_output(format_html(x,...)) |
| 9 |
|
| 10 |
knit_print.descriptions <-function(x,...) |
| 11 |
knitr::asis_output(format_html(x,...)) |
| 12 |
|
| 13 |
knit_print.ftable <-function(x,options,...) |
| 14 |
knitr::asis_output( |
| 15 |
format_html(x, |
| 16 |
digits=if(length(options$ftable.digits)) |
| 17 |
options$ftable.digits |
| 18 |
else 0, |
| 19 |
...)) |
| 20 |
# We can now adjust the number of digits after the comma |
| 21 |
# for each column e.g. by adding an `ftable.digits` option |
| 22 |
# to an R chunk, as in ```{r,ftable=c(2,2,0)} |
| 23 |
|
| 24 |
knit_print.mtable <-function(x,...) |
| 25 |
knitr::asis_output(format_html(x,...)) |
| 26 |
|
| 27 |
## ----message=FALSE------------------------------------------------------------ |
| 28 |
library(memisc) |
| 29 |
options(digits=3) |
| 30 |
nes1948.por <- unzip(system.file("anes/NES1948.ZIP",package="memisc"), |
| 31 |
"NES1948.POR",exdir=tempfile()) |
| 32 |
|
| 33 |
## ----------------------------------------------------------------------------- |
| 34 |
nes1948 <- spss.portable.file(nes1948.por) |
| 35 |
print(nes1948) |
| 36 |
|
| 37 |
## ----------------------------------------------------------------------------- |
| 38 |
names(nes1948) |
| 39 |
|
| 40 |
## ----------------------------------------------------------------------------- |
| 41 |
description(nes1948) |
| 42 |
|
| 43 |
## ----eval=FALSE--------------------------------------------------------------- |
| 44 |
# codebook(nes1948) |
| 45 |
|
| 46 |
## ----------------------------------------------------------------------------- |
| 47 |
codebook(nes1948[1:5]) |
| 48 |
|
| 49 |
## ----------------------------------------------------------------------------- |
| 50 |
vote.48 <- subset(nes1948, |
| 51 |
select=c( |
| 52 |
V480018, |
| 53 |
V480029, |
| 54 |
V480030, |
| 55 |
V480045, |
| 56 |
V480046, |
| 57 |
V480047, |
| 58 |
V480048, |
| 59 |
V480049, |
| 60 |
V480050 |
| 61 |
)) |
| 62 |
|
| 63 |
## ----------------------------------------------------------------------------- |
| 64 |
str(vote.48) |
| 65 |
|
| 66 |
## ----------------------------------------------------------------------------- |
| 67 |
vote.48 <- rename(vote.48, |
| 68 |
V480018 = "vote", |
| 69 |
V480029 = "occupation.hh", |
| 70 |
V480030 = "unionized.hh", |
| 71 |
V480045 = "gender", |
| 72 |
V480046 = "race", |
| 73 |
V480047 = "age", |
| 74 |
V480048 = "education", |
| 75 |
V480049 = "total.income", |
| 76 |
V480050 = "religious.pref" |
| 77 |
) |
| 78 |
|
| 79 |
## ----eval=FALSE--------------------------------------------------------------- |
| 80 |
# vote.48 <- subset(nes1948, |
| 81 |
# select=c( |
| 82 |
# vote = V480018, |
| 83 |
# occupation.hh = V480029, |
| 84 |
# unionized.hh = V480030, |
| 85 |
# gender = V480045, |
| 86 |
# race = V480046, |
| 87 |
# age = V480047, |
| 88 |
# education = V480048, |
| 89 |
# total.income = V480049, |
| 90 |
# religious.pref = V480050 |
| 91 |
# )) |
| 92 |
|
| 93 |
## ----------------------------------------------------------------------------- |
| 94 |
codebook(vote.48) |
| 95 |
|
| 96 |
## ----------------------------------------------------------------------------- |
| 97 |
vote.48 <- within(vote.48,{ |
| 98 |
vote3 <- recode(vote, |
| 99 |
1 -> "Truman", |
| 100 |
2 -> "Dewey", |
| 101 |
3:4 -> "Other" |
| 102 |
) |
| 103 |
occup4 <- recode(occupation.hh, |
| 104 |
10:20 -> "Upper white collar", |
| 105 |
30 -> "Other white collar", |
| 106 |
40:70 -> "Blue collar", |
| 107 |
80 -> "Farmer" |
| 108 |
) |
| 109 |
relig3 <- recode(religious.pref, |
| 110 |
1 -> "Protestant", |
| 111 |
2 -> "Catholic", |
| 112 |
3:5 -> "Other,none" |
| 113 |
) |
| 114 |
race2 <- recode(race, |
| 115 |
1 -> "White", |
| 116 |
2 -> "Black" |
| 117 |
) |
| 118 |
}) |
| 119 |
|
| 120 |
## ----------------------------------------------------------------------------- |
| 121 |
ftable(xtabs(~vote3+occup4,data=vote.48)) |
| 122 |
|
| 123 |
## ----ftable.digits=c(2,2,2,0)------------------------------------------------- |
| 124 |
gt1 <- genTable(percent(vote3)~occup4,data=vote.48) |
| 125 |
## For knitr-ing, we use ```{r, ftable.digits=c(2,2,2,0)} here. |
| 126 |
ftable(gt1,row.vars=2) |
| 127 |
|
| 128 |
## ----ftable.digits=c(2,2,2,0)------------------------------------------------- |
| 129 |
gt2 <- genTable(percent(vote3)~relig3,data=vote.48) |
| 130 |
ftable(gt2,row.vars=2) |
| 131 |
|
| 132 |
## ----ftable.digits=c(2,2,2,0)------------------------------------------------- |
| 133 |
gt3 <- genTable(percent(vote3)~race2,data=vote.48) |
| 134 |
ftable(gt3,row.vars=2) |
| 135 |
|
| 136 |
## ----ftable.digits=c(2,2,2,0)------------------------------------------------- |
| 137 |
gt4 <- genTable(percent(vote3)~total.income,data=vote.48) |
| 138 |
ftable(gt4,row.vars=2) |
| 139 |
|
| 140 |
## ----ftable.digits=c(2,2,2)--------------------------------------------------- |
| 141 |
## For knitr-ing, we use ```{r, ftable.digits=c(2,2,2)} here. |
| 142 |
inc.tab <- genTable(percent(vote3,ci=TRUE)~total.income,data=vote.48) |
| 143 |
ftable(inc.tab,row.vars=c(3,2)) |
| 144 |
|
| 145 |
## ----ftable.digits=c(2,2,2)--------------------------------------------------- |
| 146 |
occup.tab <- genTable(percent(vote3,ci=TRUE)~occup4,data=vote.48) |
| 147 |
ftable(occup.tab,row.vars=c(3,2)) |
| 148 |
|
| 149 |
## ----------------------------------------------------------------------------- |
| 150 |
vote.48 <- within(vote.48,{ |
| 151 |
contrasts(occup4) <- contr("treatment",base = 3) |
| 152 |
contrasts(total.income) <- contr("treatment",base = 4) |
| 153 |
}) |
| 154 |
|
| 155 |
## ----------------------------------------------------------------------------- |
| 156 |
model1 <- glm((vote3=="Truman")~occup4,data=vote.48, |
| 157 |
family="binomial") |
| 158 |
model2 <- glm((vote3=="Truman")~total.income,data=vote.48, |
| 159 |
family="binomial") |
| 160 |
model3 <- glm((vote3=="Truman")~occup4+total.income,data=vote.48, |
| 161 |
family="binomial") |
| 162 |
model4 <- glm((vote3=="Truman")~relig3,data=vote.48, |
| 163 |
family="binomial") |
| 164 |
model5 <- glm((vote3=="Truman")~occup4+relig3,data=vote.48, |
| 165 |
family="binomial") |
| 166 |
|
| 167 |
## ----------------------------------------------------------------------------- |
| 168 |
mtable(model1,model2,model3,summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N")) |
| 169 |
|
| 170 |
## ----------------------------------------------------------------------------- |
| 171 |
relabel(mtable( |
| 172 |
"Model 1"=model1, |
| 173 |
"Model 2"=model2, |
| 174 |
"Model 3"=model3, |
| 175 |
summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N")), |
| 176 |
UNDER="under", |
| 177 |
"AND OVER"="and over", |
| 178 |
occup4="Occup. class", |
| 179 |
total.income="Income", |
| 180 |
gsub=TRUE |
| 181 |
) |
| 182 |
|
| 183 |
## ----------------------------------------------------------------------------- |
| 184 |
relabel(mtable( |
| 185 |
"Model 1"=model1, |
| 186 |
"Model 4"=model4, |
| 187 |
"Model 5"=model5, |
| 188 |
summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N")), |
| 189 |
occup4="Occup. class", |
| 190 |
relig3="Religion", |
| 191 |
gsub=TRUE |
| 192 |
) |
| 193 |
|
| 194 |
## ----echo=FALSE--------------------------------------------------------------- |
| 195 |
rm(knit_print.codebook, |
| 196 |
knit_print.descriptions, |
| 197 |
knit_print.ftable, |
| 198 |
knit_print.mtable) |
| 199 |
|