% \VignetteIndexEntry{Analysing the American National Election Study of 1948 with "memisc"} % \VignetteEngine{knitr::rmarkdown}

knitr::opts_chunk$set(comment=NA, fig.align="center", results="markup")

title: Analysing the American National Election Study of 1948 with 'memisc'

Analysing the American National Election Study of 1948 with memisc


This vignette gives an example for the analysis of a typical social science data set. It is the data file of the American National Election Study of 1948[^1] available from the American National Election Studies website. The data file contains data from to USA-wide surveys conducted October and November 1948 by the Survey Research Centre, University Michigan (principal investigators: Angus Campbell and Robert L. Kahn). The total number of cases in the data set is 662 and the number of variables is 65 (more details about this data set can be found at

[^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.

With 662 cases and 65 variables, the 1948 ANES data set is relatively small as compared to current social science data sets. Such larger data sets can be processed along the same lines as in this vignette. Unlike the 1948 ANES data, their size as well as, in some cases, legal restrictions prohibit the inclusion of such a data set into the package, however.

This vignette starts with a demonstration how a data file can be examined before loading it and how a subset of the data can be loaded into memory. After loading this subset into memory, some descriptive analyses are conducted that showcase the construction of contingency tables and of general tables of descriptive statistics using the genTable function. In addition, a logit analysis is demonstrated and the collection of several logit coefficients into a comprehensive table by the mtable function.

It should be noted that the analyses reported in the following are conducted only for purpose of demonstrating the features of the package and are not to be considered of conclusive scientific evidence of any kind.

This vignette is run with the help of the knitr package. This allows to showcase not only data management facilities provided by memisc. The following code also demonstrates how output created with some of the facilities of memisc can neatly integrated in reports generated with knitr. Before we start, we adjust knitr's output (with which this vignette is formatted) to produce HTML where possible.

knit_print.codebook <-function(x,...) knitr::asis_output(format_html(x,...)) knit_print.descriptions <-function(x,...) knitr::asis_output(format_html(x,...)) knit_print.ftable <-function(x,options,...) knitr::asis_output( format_html(x, digits=if(length(options$ftable.digits)) options$ftable.digits else 0, ...)) # We can now adjust the number of digits after the comma # for each column e.g. by adding an `ftable.digits` option # to an R chunk, as in ```{r} knit_print.mtable <-function(x,...) knitr::asis_output(format_html(x,...))

Reading in a "portable" SPSS data file

We start with importing the data into R. The following code extracts the SPSS portable file NES1948.POR from zip file NES1948.ZIP delivered with the memisc package.

library(memisc) options(digits=3) nes1948.por <- unzip(system.file("anes/NES1948.ZIP",package="memisc"), "NES1948.POR",exdir=tempfile())

Now the portable file is in a temporary directory and the path to the file is contained in the string variable nes1948.por. In the next step, the file is declared as a SPSS/PSPP portable file using the function spss.portable.file, which as first argument takes the path to the file. spss.portable.file reads in the information about the variables contained in the data set and counts the number of cases in the file. That is, standard I/O operations are used on the file, but the data read in are just thrown away without allocating core memory for the data. This counting of cases can, of course, be suppressed if it would take too long.

nes1948 <- spss.portable.file(nes1948.por) print(nes1948)

At this stage, the data are not loaded into the memory yet. But we can see which variables exist inside the data set:


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).

We also can ask for a description ("variable label") for each variable:


or even a code book using


(this is not shown here because the output would have taken more then thirty pages). We can also get a codebook of the first few variabels instead, with


Reading in a subset of the data

After we have decided which variables to use we can read in a subset of the data:

vote.48 <- subset(nes1948, select=c( V480018, V480029, V480030, V480045, V480046, V480047, V480048, V480049, V480050 ))

The subset of the ANES 1948 we read in is now contained in the variable vote.48, which contains an object of class data.set. A data.set is an "embellished" version of a data.frame, a data structure intended to contained labelled vectors. labelled vectors contain the all the special information attached to the variables in the original data set, such as variable labels, value labels, and general missing values. A short summary of this special information shows up after a call to str.


This output shows, for example, that variable V480018 has the description (variable label) "DID R VOTE/FOR WHOM" is considered as having nominal level of measurement, has seven value labels and one defined missing value.

Since the variable names in the ANES data set are not very mnemonic, we rename the variables:

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" )

Since many data sets available from public repositories have such non-mnemonic variable names as in this example, it might be convenient to do the data loading and renaming in one step. Indeed it is possible:

vote.48 <- subset(nes1948, select=c( vote = V480018, occupation.hh = V480029, unionized.hh = V480030, gender = V480045, race = V480046, age = V480047, education = V480048, total.income = V480049, religious.pref = V480050 ))

Before we start with analyses, we take a closer look at the data.


We now have obtained a codebook, which contains information of the class and type of the variables in the data set, the value labels and defined missing values, and counts of the distinct values of the variables.


Some descriptive analyses

We start our analyses with a contingency table, but first we make some preparations: We recode the variables of interest into a smaller number of categories in order to get results that are easier to read and interpret.

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" ) })

Having constructed the unordered factors vote3, occup4, relig3, and race2 we can proceed examining the association the vote, occupational class, relgious denomination, and race. First, we look upon a simple contingency table.


Tables of percentages may seem more informative about the impact of various factors on the vote. So we use the function genTable to obtain such tables of percentages:

gt1 <- genTable(percent(vote3)~occup4,data=vote.48) ## For knitr-ing, we use ```{r} here. ftable(gt1,row.vars=2)

Obviously, voters from farmer and blue collar worker households were especially supportive of President Truman, while voters of upper white collar background largely supported the Republican Candidate Dewey.

gt2 <- genTable(percent(vote3)~relig3,data=vote.48) ftable(gt2,row.vars=2)

This table shows that Catholics and adherents of other denominations were more supportive of Truman than of Dewey.

gt3 <- genTable(percent(vote3)~race2,data=vote.48) ftable(gt3,row.vars=2)

African Americans apparently supported Truman by a large majority. The number of members of this group in the sample is very small, however, so that such an inference would be very shaky.

gt4 <- genTable(percent(vote3)~total.income,data=vote.48) ftable(gt4,row.vars=2)

The table of percentage of vote by income suggests that income had some considerable influence on the choice either of Truman or of Dewey, but the unequal distribution of income categories warrants a more refined analysis that takes into account the uncertainty about the vote percentages. Therefore, the percentages of support for Truman broken down by income shown with confidence intervals:

## For knitr-ing, we use ```{r} here. <- genTable(percent(vote3,ci=TRUE)~total.income,data=vote.48) ftable(,row.vars=c(3,2))

Occupational class is more evenly distributed in the sample, thus it may be possible to obtain more precise estimates of the percentages of support for Truman for occupational classes: <- genTable(percent(vote3,ci=TRUE)~occup4,data=vote.48) ftable(,row.vars=c(3,2))

The upper and lower white-collar and blue-collar classes are quite distinct with regard to the percentages of support for Truman. The point estimates of the percentages are outside the confidence intervals of the respective other occupational classes, the confidence intervals do not even overlap. However, it is not clear whether farmers are distinct from the blue-collar and lower white-collar classes.

Logit modelling of candidate choice

In the following we conduct a logit analysis of the vote for Truman. First, we assign non-standard contrasts the categorical predictors. Here, the function contr is used to assign treatment (dummy) contrasts to occup4 and total.income with baseline category 3 and 4, respectively.

vote.48 <- within(vote.48,{ contrasts(occup4) <- contr("treatment",base = 3) contrasts(total.income) <- contr("treatment",base = 4) })

We now fit some logistic regression models of the impact occupational class, income, and religious denomination on the vote choice supporting Truman. The contrasts of the occupational class and income factors are such that they compare the choices of the members of the blue-collar class with all other classes and the middle income group (\$ 2000-2999) with the other income groups. The religious denomination factor compares Protestants with Catholics and those with other or no denominations.

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")

First, we use mtable to construct a comparative table of the estimates of model1, model2, and model3. We thus can compare the impact of occupational class and income on the choice of candidate Truman.

mtable(model1,model2,model3,summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N"))

mtable returns an object of class "mtable". When formatted it looks close to the requirements of typical social science publications. Yet at least we want to change the technical variable names into non-technical ones, for which we can use relabel:

relabel(mtable( "Model 1"=model1, "Model 2"=model2, "Model 3"=model3, summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N")), UNDER="under", "AND OVER"="and over", occup4="Occup. class", total.income="Income", gsub=TRUE )

The comparison of the pseudo-R-Square values of model 1 and 2 suggests that occupational class has a stronger influence on a preference for Truman than household income. Indeed, if occupational class is taken into account, the effect of income is no longer statistically significant as the column corresponding to model 3 indicates.

Second, we compare the effect of occupational class and religious denomination on the preference for Truman along the same lines as above. We use mtable to collect the estimates of model1, model4, and model5 into a common table.

relabel(mtable( "Model 1"=model1, "Model 4"=model4, "Model 5"=model5, summary.stats=c("Nagelkerke R-sq.","Deviance","AIC","N")), occup4="Occup. class", relig3="Religion", gsub=TRUE )

A comparison of the pseudo-R-squared values suggests that also the effect of religious denomination is weaker than that of occupational class. However, as the third column in the above table indicates the effect of religious denomination remains statistically significant.

rm(knit_print.codebook, knit_print.descriptions, knit_print.ftable, knit_print.mtable)