Debian Codegraph @

inst/doc/anes48.Rmd

raw
1 ---
2 title: Analysing the American National Election Study of 1948
3 output: rmarkdown::html_vignette
4 vignette: >
5 % \VignetteIndexEntry{Analysing the American National Election Study of 1948}
6 % \VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
8 ---
9
10
11
12 ```{r,echo=FALSE,message=FALSE}
13 knitr::opts_chunk$set(comment=NA,
14 fig.align="center",
15 results="markup")
16 ```
17
18
19 # Introduction
20
21 This vignette gives an example for the analysis of a typical social science data set.
22 It is the data file of the *American National Election Study* of
23 1948[^1] available from the [American National Election Studies website](https://electionstudies.org). The data file contains data
24 from to USA-wide surveys conducted October and November 1948
25 by the Survey Research Centre, University Michigan
26 (principal investigators: Angus Campbell and Robert L. Kahn). The
27 total number of cases in the data set is 662 and the number of variables
28 is 65 (more details about this
29 data set can be found
30 at <https://electionstudies.org/studypages/1948prepost/1948prepost.htm>).
31
32 [^1]: National Election Studies, 1948: *Post-Election Study [dataset].* Ann Arbor, MI: University of Michigan, Center for Political Studies [producer and distributor], 1999. ANES Dataset ID: 1948.T; ICPSR Study Number: 7218. These materials are based on work supported by the National Science Foundation under Grant Nos.: SBR-9707741, SBR-9317631, SES-9209410, SES-9009379, SES-8808361, SES-8341310, SES-8207580, and SOC77-08885. Any opinions, findings and conclusions or recommendations expressed in these materials are those of the author(s) and do not necessarily reflect those of the National Science Foundation.
33
34 With 662 cases and 65 variables, the 1948 ANES data set is relatively
35 small as compared to current social science data sets. Such larger data
36 sets can be processed along the same lines as in this vignette.
37 Unlike the 1948 ANES data,
38 their size as well as, in some cases, legal restrictions prohibit the
39 inclusion of such a data set into the package, however.
40
41 This vignette starts with a demonstration how a data file can be
42 examined before loading it and how a subset of the data can be loaded
43 into memory.
44 After loading this subset into memory, some descriptive analyses are
45 conducted that showcase the construction of contingency tables and of
46 general tables of descriptive statistics using the
47 `genTable` function. In addition, a logit analysis
48 is demonstrated and the collection of several logit coefficients into
49 a comprehensive table by the `mtable` function.
50
51 It should be noted that the analyses reported in the following are conducted
52 only for purpose of demonstrating the features of the package and are
53 not to be considered of conclusive scientific evidence of any kind.
54
55 This vignette is run with the help of the [*knitr* package](https://cran.r-project.org/package=knitr). This
56 allows to showcase not only data management facilities provided
57 by *memisc*. The following code also demonstrates how output
58 created with some of the facilities of *memisc* can neatly
59 integrated in reports generated with *knitr*.
60 Before we start, we adjust *knitr*'s output (with which this
61 vignette is formatted) to produce HTML where possible.
62 ```{r}
63 knit_print.codebook <-function(x,...)
64 knitr::asis_output(format_html(x,...))
65
66 knit_print.descriptions <-function(x,...)
67 knitr::asis_output(format_html(x,...))
68
69 knit_print.ftable <-function(x,options,...)
70 knitr::asis_output(
71 format_html(x,
72 digits=if(length(options$ftable.digits))
73 options$ftable.digits
74 else 0,
75 ...))
76 # We can now adjust the number of digits after the comma
77 # for each column e.g. by adding an `ftable.digits` option
78 # to an R chunk, as in ```{r,ftable=c(2,2,0)}
79
80 knit_print.mtable <-function(x,...)
81 knitr::asis_output(format_html(x,...))
82 ```
83
84 # Reading in a "portable" SPSS data file
85
86 We start with importing the data into R.
87 The following code extracts the SPSS portable file
88 `NES1948.POR` from
89 zip file `NES1948.ZIP` delivered with the *memisc* package.
90 ```{r, message=FALSE}
91 library(memisc)
92 options(digits=3)
93 nes1948.por <- unzip(system.file("anes/NES1948.ZIP",package="memisc"),
94 "NES1948.POR",exdir=tempfile())
95 ```
96 Now the portable file is in a temporary directory and the path
97 to the file is contained in the string variable `nes1948.por`.
98 In the next step, the file is declared as a SPSS/PSPP portable
99 file using the function `spss.portable.file`, which as first argument
100 takes the path to the file. `spss.portable.file`
101 reads in the information about the variables contained in the data
102 set and counts the
103 number of cases in the file. That is, standard
104 I/O operations are used on the file, but the data read in are just
105 thrown away without allocating core memory for the data.
106 This counting of cases can, of course, be suppressed if it would
107 take too long.
108 ```{r}
109 nes1948 <- spss.portable.file(nes1948.por)
110 print(nes1948)
111 ```
112 At this stage, the data are not loaded into the memory yet.
113 But we can see which variables exist inside
114 the data set:
115 ```{r}
116 names(nes1948)
117 ```
118 Note that the variable names are all changed from uppercase to lowercase (SPSS does not distinguish uppercase and lowercase variable names and uppercase looks like shouting). Casefolding could have been suppressed by the call `spsp.portable.file(nes1948.por,tolower=FALSE)`.
119
120 We also can ask for a description ("variable label") for each variable:
121 ```{r}
122 description(nes1948)
123 ```
124 or even a code book using
125 ```{r, eval=FALSE}
126 codebook(nes1948)
127 ```
128 (this is not shown
129 here because the output would have taken more then thirty pages).
130 We can also get a codebook of the first few variabels instead, with
131 ```{r}
132 codebook(nes1948[1:5])
133 ```
134
135
136 ### Reading in a subset of the data
137 After we have decided which variables to use we can read in a subset of
138 the data:
139 ```{r}
140 vote.48 <- subset(nes1948,
141 select=c(
142 V480018,
143 V480029,
144 V480030,
145 V480045,
146 V480046,
147 V480047,
148 V480048,
149 V480049,
150 V480050
151 ))
152 ```
153 The subset of the ANES 1948 we read in is now contained in the
154 variable `vote.48`, which contains an object of class `data.set`.
155 A `data.set` is an "embellished" version of a `data.frame`,
156 a data structure intended to contained `labelled` vectors.
157 `labelled` vectors contain the all the special information
158 attached to the variables in the original data set, such as
159 variable labels, value labels, and general missing values.
160 A short summary of this special information shows up after a
161 call to `str`.
162 ```{r}
163 str(vote.48)
164 ```
165 This output shows, for example, that variable `V480018`
166 has the description (variable label) "DID R VOTE/FOR WHOM" is considered as
167 having nominal level of measurement, has seven value labels
168 and one defined missing value.
169
170 Since the variable names in the ANES data set are not very mnemonic,
171 we rename the variables:
172 ```{r}
173 vote.48 <- rename(vote.48,
174 V480018 = "vote",
175 V480029 = "occupation.hh",
176 V480030 = "unionized.hh",
177 V480045 = "gender",
178 V480046 = "race",
179 V480047 = "age",
180 V480048 = "education",
181 V480049 = "total.income",
182 V480050 = "religious.pref"
183 )
184 ```
185 Since many data sets available from public repositories have
186 such non-mnemonic variable names as in this example, it might
187 be convenient to do the data loading and renaming in one step.
188 Indeed it is possible:
189 ```{r,eval=FALSE}
190 vote.48 <- subset(nes1948,
191 select=c(
192 vote = V480018,
193 occupation.hh = V480029,
194 unionized.hh = V480030,
195 gender = V480045,
196 race = V480046,
197 age = V480047,
198 education = V480048,
199 total.income = V480049,
200 religious.pref = V480050
201 ))
202 ```
203
204
205 Before we start with analyses, we take a closer look at
206 the data.
207 ```{r}
208 codebook(vote.48)
209 ```
210 We now have obtained a *codebook*, which contains information
211 of the class and type of the variables in the data set,
212 the value labels and defined missing values, and counts of
213 the distinct values of the variables.
214
215 # Analysis
216
217 ## Some descriptive analyses
218
219 We start our analyses with a contingency table, but first
220 we make some preparations: We recode the variables of
221 interest into a smaller number of categories in order to
222 get results that are easier to read and interpret.
223 ```{r}
224 vote.48 <- within(vote.48,{
225 vote3 <- recode(vote,
226 1 -> "Truman",
227 2 -> "Dewey",
228 3:4 -> "Other"
229 )
230 occup4 <- recode(occupation.hh,
231 10:20 -> "Upper white collar",
232 30 -> "Other white collar",
233 40:70 -> "Blue collar",
234 80 -> "Farmer"
235 )
236 relig3 <- recode(religious.pref,
237 1 -> "Protestant",
238 2 -> "Catholic",
239 3:5 -> "Other,none"
240 )
241 race2 <- recode(race,
242 1 -> "White",
243 2 -> "Black"
244 )
245 })
246 ```
247
248 Having constructed the unordered factors
249 `vote3`, `occup4`, `relig3`, and `race2` we can
250 proceed examining the association the vote, occupational class,
251 relgious denomination, and race. First, we look upon a simple
252 contingency table.
253
254 ```{r}
255 ftable(xtabs(~vote3+occup4,data=vote.48))
256 ```
257
258 Tables of percentages may seem more informative about the impact
259 of various factors on the vote. So we use the function
260 `genTable` to obtain such tables of percentages:
261 ```{r, ftable.digits=c(2,2,2,0)}
262 gt1 <- genTable(percent(vote3)~occup4,data=vote.48)
263 ## For knitr-ing, we use ```{r, ftable.digits=c(2,2,2,0)} here.
264 ftable(gt1,row.vars=2)
265 ```
266
267 Obviously, voters from farmer and blue collar worker households
268 were especially supportive of President Truman, while voters
269 of upper white collar background largely supported
270 the Republican Candidate Dewey.
271
272
273 ```{r, ftable.digits=c(2,2,2,0)}
274 gt2 <- genTable(percent(vote3)~relig3,data=vote.48)
275 ftable(gt2,row.vars=2)
276 ```
277
278
279 This table shows that Catholics and adherents of other denominations
280 were more supportive of Truman than of Dewey.
281
282
283 ```{r, ftable.digits=c(2,2,2,0)}
284 gt3 <- genTable(percent(vote3)~race2,data=vote.48)
285 ftable(gt3,row.vars=2)
286 ```
287
288
289 African Americans apparently supported Truman by a large majority. The
290 number of members of this group in the sample is very small, however,
291 so that such an inference would be very shaky.
292
293
294 ```{r, ftable.digits=c(2,2,2,0)}
295 gt4 <- genTable(percent(vote3)~total.income,data=vote.48)
296 ftable(gt4,row.vars=2)
297 ```
298 The table of percentage of vote by income suggests that income had
299 some considerable influence on the choice either of Truman or of Dewey,
300 but the unequal distribution of income categories warrants a more
301 refined analysis that takes into account the uncertainty about the
302 vote percentages. Therefore, the percentages of support for Truman
303 broken down by income shown with confidence intervals:
304 ```{r, ftable.digits=c(2,2,2)}
305 ## For knitr-ing, we use ```{r, ftable.digits=c(2,2,2)} here.
306 inc.tab <- genTable(percent(vote3,ci=TRUE)~total.income,data=vote.48)
307 ftable(inc.tab,row.vars=c(3,2))
308 ```
309
310 Occupational class is more evenly distributed in the sample, thus it may be possible
311 to obtain more precise estimates of the percentages of support for Truman for occupational
312 classes:
313 ```{r, ftable.digits=c(2,2,2)}
314 occup.tab <- genTable(percent(vote3,ci=TRUE)~occup4,data=vote.48)
315 ftable(occup.tab,row.vars=c(3,2))
316 ```
317 The upper and lower white-collar and blue-collar classes are quite distinct
318 with regard to the percentages of support for Truman. The point estimates
319 of the percentages are outside the confidence intervals of the respective
320 other occupational classes, the confidence intervals do not even overlap.
321 However, it is not clear whether farmers are distinct from the blue-collar
322 and lower white-collar classes.
323
324 ### Logit modelling of candidate choice
325
326 In the following we conduct a logit analysis of the vote for Truman.
327 First, we assign non-standard contrasts the categorical predictors.
328 Here, the function `contr` is used to assign treatment (dummy)
329 contrasts to `occup4` and `total.income` with
330 baseline category 3 and 4, respectively.
331 ```{r}
332 vote.48 <- within(vote.48,{
333 contrasts(occup4) <- contr("treatment",base = 3)
334 contrasts(total.income) <- contr("treatment",base = 4)
335 })
336 ```
337 We now fit some logistic regression models of the
338 impact occupational class, income, and religious denomination
339 on the vote choice supporting Truman. The contrasts of
340 the occupational class and income factors are such that they
341 compare the choices of the members of the blue-collar class
342 with all other classes and the middle income group (\$ 2000-2999)
343 with the other income groups. The religious denomination factor
344 compares Protestants with Catholics and those with other or
345 no denominations.
346 ```{r}
347 model1 <- glm((vote3=="Truman")~occup4,data=vote.48,
348 family="binomial")
349 model2 <- glm((vote3=="Truman")~total.income,data=vote.48,
350 family="binomial")
351 model3 <- glm((vote3=="Truman")~occup4+total.income,data=vote.48,
352 family="binomial")
353 model4 <- glm((vote3=="Truman")~relig3,data=vote.48,
354 family="binomial")
355 model5 <- glm((vote3=="Truman")~occup4+relig3,data=vote.48,
356 family="binomial")
357 ```
358 First, we use `mtable` to construct a comparative table
359 of the estimates of `model1`,
360 `model2`, and `model3`. We thus can compare
361 the impact of occupational class and income on the choice of
362 candidate Truman.
363 ```{r}
364 mtable(model1,model2,model3,summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N"))
365 ```
366 `mtable` returns an object of class `"mtable"`. When formatted
367 it looks close to the requirements of typical social science
368 publications. Yet at least we want to change the technical variable
369 names into non-technical ones, for which we can use `relabel`:
370
371 ```{r}
372 relabel(mtable(
373 "Model 1"=model1,
374 "Model 2"=model2,
375 "Model 3"=model3,
376 summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N")),
377 UNDER="under",
378 "AND OVER"="and over",
379 occup4="Occup. class",
380 total.income="Income",
381 gsub=TRUE
382 )
383 ```
384
385
386 The comparison of the pseudo-R-Square values of model 1 and 2 suggests that
387 occupational class has a stronger influence on a preference for Truman than
388 household income. Indeed, if occupational class is taken into account, the
389 effect of income is no longer statistically significant as the column
390 corresponding to model 3 indicates.
391
392 Second, we compare the effect of occupational class and religious denomination
393 on the preference for Truman along the same lines as above. We use
394 `mtable` to collect the estimates of `model1`, `model4`,
395 and `model5` into a common table.
396
397 ```{r}
398 relabel(mtable(
399 "Model 1"=model1,
400 "Model 4"=model4,
401 "Model 5"=model5,
402 summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N")),
403 occup4="Occup. class",
404 relig3="Religion",
405 gsub=TRUE
406 )
407 ```
408
409
410 A comparison of the pseudo-R-squared values suggests that also the effect of
411 religious denomination is weaker than that of occupational class. However,
412 as the third column in the above table indicates the effect of religious
413 denomination remains statistically significant.
414
415 ```{r,echo=FALSE}
416 rm(knit_print.codebook,
417 knit_print.descriptions,
418 knit_print.ftable,
419 knit_print.mtable)
420 ```