Data Analysis and Statistical Inference project on Coursera

Earlier this month I completed a course on Data Analysis and Statistical Inference offered by Duke University through Coursera. As part of the course we had to complete a research project using statistical inference and modelling techniques. My project is detailed below. All code examples are in R.

In the US, is there a relationship between someone being contacted by a political party and who they voted for in the 2012 US Presidential election?

In the run-up to a Presidential election all of the political parties contesting the ballot will attempt to contact as many voters as possible to try to encourage them to vote for their party’s candidate. The purpose of the research question is to determine the effectiveness of this strategy. Does the time and resources political parties invest in contacting voters result in more votes for their candidate?

The data used to answer this research question

In order to answer this question I’ve used the data collected from the American National Election Studies (ANES) 2012 Time Series study (

How this data was collected

The data were collected using face-to-face interviews and surveys conducted via the internet.

The face-to-face sample consisted of a cross-section of US eligible voters using an address-based, stratified, multi-stage cluster sample in 125 census tracts. It includes a nationally representative “main sample” and two “oversamples”: one of blacks and one of Hispanics (this is to correct for any bias that occurred in the main sample). Two interviews were conducted in each case: one during the two months prior to the 2012 US elections and one in the months after the election.

The internet sample consisted of a representative US sample separate from the face-to-face group. They were given the same questions as the face-to-face sample but this was split across two pre-election interviews and two post-election interviews.

The units of observation (cases) are all the respondents to the Time Series questionnaire whether via interview or survey.

Variables used to answer the question

The two variables I am studying are:

  1. Which party contacted the respondent about the 2012 campaign (a categorical variable)
  2. For whom did the respondent vote for President in 2012 (a categorical variable)

Type of study

This is an observational study because it is merely asking voters their views on various political issues i.e. we are observing what happens when these questions are asked. The study does use cluster sampling to gather the data but if this was an experiment it would also control and block for other factors that may influence responses. However, it does not do this so it cannot be called an experiment.

The population of interest and generalizability

The population of interest is the entire US population that is eligible to vote and the findings can be generalized to that population. This is because the samples used are representative of the US population as they account for the multitude of different demographic groups in the country. For example, in the face-to-face sample the 48 contiguous states of the US are stratified into nine regions which correspond to the Census Divisions (these are groupings of states in a similar geographical area). Then within each region a number of census tracts are randomly selected based on the region’s proportion of the US population. This means the study’s sample is representative of the US population.

Some sources of bias include:

  • Lack of representation of blacks and Hispanics. This is overcome by using “oversamples” which specifically target participation from these groups so that there is no bias in the main sample.
  • The sampling method randomly selects from residential addresses based on the United States Postal Service address list. However, this list does not cover all residential housing units and so may exclude certain types of voters. In census tracts where there was a high proportion of housing units of this type interviewers were sent to these tracts to make a list of all housing units so that they could be included in the sample.
  • The internet sample might have included voters who do not have access to the internet. In these cases the voter was supplied with a computer and internet access so that they could still participate in the study.
  • Alaska and Hawaii were excluded from the sample due to lack of resources. The researchers claim that this may introduce some bias but not enough to affect the representativeness of the sample.

Exploratory data analysis

I have only shown results for those respondents who said they did not belong to a political party or were independents. If a respondent belongs to a political party that will very likely affect who they vote for (and possibly who they’re contacted by) so it may bias the results.

In addition, I’ve excluded any respondents where they were contacted by both Democrats and Republicans since this isn’t relevant to the research question.

Here is a table showing how many respondents were contacted by a political party compared to which candidate they voted for (note that Barack Obama was the Democrat’s candidate and Mitt Romney the Republican’s).

indepVoters <- anes$prevote_regpty == 'None Or 'Independent''
excludeBoth <- anes$mobilpo_wparty != 'Both {Vol}'
candidateVotedFor <- anes$presvote2012_x[indepVoters & excludeBoth]
partyContactedBy <- droplevels(anes$mobilpo_wparty[indepVoters & excludeBoth])
addmargins(table(partyContactedBy, candidateVotedFor))
##                  candidateVotedFor
## partyContactedBy  Barack Obama Mitt Romney Other Sum
##   Democrats                 10           5     1  16
##   Republicans                5           9     1  15
##   Other {Specify}            1           0     0   1
##   Sum                       16          14     2  32

We can see that when a respondent was contacted by a political party they were generally more likely to vote for that party’s candidate. Sixteen respondents were contacted by only Democrats, 10 of which voted for the Democrat’s candidate Barack Obama (62.5%). Fifteen respondents were contacted by only Republicans, nine of which voted for the Republican’s candidate Mitt Romney (60%). However, this is not a strong correlation and we do not have many results so we cannot make any firm conclusions.

The image below visualizes this table:

barplot(table(partyContactedBy, candidateVotedFor), legend.text = T, beside=T, col=c('blue', 'red', 'black'), main='Respondents who voted for a candidate based on party contact', xlab = 'Presidential candidates', ylab='Number of votes', names.arg=c('Democrat', 'Republican', 'Other'))

This confirms that there seems to be a link between whether a respondent was contacted by a political party and their likelihood of voting for that party’s candidate.

Statistical inference

In order to fully answer this research question I will conduct a simulation. Since I am comparing two categorical variables, both of which have more than two levels, the chi-squared independence test is the most appropriate method here.


  • Null hypothesis - there is no relationship between contact by a political party and voting for that party’s candidate
  • Alternative hypothesis - there is a relationship between contact by a political party and voting for that party’s candidate


One of the conditions of the test is that all sampled observations must be independent. This means there must be random sampling, the sample size must be less than 10% of the population and each case should only contribute to one cell in the table. As we’ve already seen the study uses random sampling, the total number of respondents to the study was 5914, which is certainly less than 10% of the eligible US voting population, and each case only contributes to one cell. The independence criteria might have been affected by respondents who were members of a political party but I have filtered the results to remove this potential source of bias.

Another condition of the test is that each scenario (cell in the table) must have at least five expected cases. The results do not meet this criteria so I cannot use the theoretical version of the chi-squared independence test.


I will test the hypothesis that contact by a political party and voting for that party’s candidate are associated at the 5% significance level. In order to run the simulation I will use R and more specifically the inference function provided by Duke University which performs 10000 simulations.

This method works as follows:

  1. Create a set of 32 cases (the total number of cases in the results)
  2. Mark 16 as “contacted by Democrats”, 15 as “contacted by Republicans” and 1 as “contacted by Other”
  3. Split them into three equal sized groups representing “voted Democrat”, “voted Republican” and “voted Other”
  4. Calculate the difference between the proportions of those contacted by Democrats and who voted Democrat, those contacted by Republicans and who voted Republican etc.
  5. Repeat steps 3-4 10000 times

Here are the results of the simulation:

inference(partyContactedBy, candidateVotedFor, est = 'proportion', type = 'ht', method = 'simulation', alternative = 'greater')
## Response variable: categorical, Explanatory variable: categorical
## Chi-square test of independence
## Summary statistics:
##                  x
## y                 Barack Obama Mitt Romney Other Sum
##   Democrats                 10           5     1  16
##   Republicans                5           9     1  15
##   Other {Specify}            1           0     0   1
##   Sum                       16          14     2  32

## H_0: Response and explanatory variable are independent.
## H_A: Response and explanatory variable are dependent.
##  Pearson's Chi-squared test with simulated p-value (based on 10000
##  replicates)
## data:  y_table
## X-squared = 3.8143, df = NA, p-value = 0.3649

Interpretation of the results

In the simulation the p-value comes out well above the significance level of 5%. This means I fail to reject the null hypothesis and it’s likely that the relationship we saw in the exploratory data analysis, that contact by a political party made it more likely that someone would vote for that party’s candidate, was simply due to chance.

No other methods are applicable for studying the relationship between these two variables so there is nothing to compare this result to.


This study has found that there is no relationship between someone being contacted by a political party and who they voted for in the 2012 US Presidential elections. Even though 62.5% of respondents contacted by Democrats voted for the Democrat’s candidate and 60% of respondents contacted by Republicans voted for the Republican’s candidate, these results were likely due to chance.

There were only 32 respondents to the study who were not registered with a political party, had been contacted by only one political party and voted in the Presidential election. This is quite a small number so future research could ask a large cross-section of independent voters, who were only contacted by one political party, who they voted for in the Presidential election.


The American National Election Studies (ANES; The ANES 2012 Time Series Study [dataset]. Stanford University and the University of Michigan [producers].

head(data.frame(anes$mobilpo_wparty, anes$presvote2012_x), 50)
##    anes.mobilpo_wparty anes.presvote2012_x
## 1                 <NA&gt;                <NA&gt;
## 2                 <NA&gt;        Barack Obama
## 3            Democrats        Barack Obama
## 4                 <NA&gt;        Barack Obama
## 5            Democrats        Barack Obama
## 6                 <NA&gt;                <NA&gt;
## 7                 <NA&gt;        Barack Obama
## 8            Democrats                <NA&gt;
## 9                 <NA&gt;        Barack Obama
## 10         Republicans         Mitt Romney
## 11         Republicans         Mitt Romney
## 12                <NA&gt;                <NA&gt;
## 13         Republicans        Barack Obama
## 14                <NA&gt;                <NA&gt;
## 15          Both {Vol}         Mitt Romney
## 16          Both {Vol}         Mitt Romney
## 17                <NA&gt;         Mitt Romney
## 18                <NA&gt;        Barack Obama
## 19                <NA&gt;                <NA&gt;
## 20                <NA&gt;                <NA&gt;
## 21                <NA&gt;         Mitt Romney
## 22     Other {Specify}                <NA&gt;
## 23                <NA&gt;                <NA&gt;
## 24                <NA&gt;                <NA&gt;
## 25          Both {Vol}         Mitt Romney
## 26                <NA&gt;                <NA&gt;
## 27                <NA&gt;         Mitt Romney
## 28                <NA&gt;         Mitt Romney
## 29                <NA&gt;        Barack Obama
## 30                <NA&gt;        Barack Obama
## 31           Democrats        Barack Obama
## 32                <NA&gt;                <NA&gt;
## 33                <NA&gt;        Barack Obama
## 34           Democrats        Barack Obama
## 35                <NA&gt;                <NA&gt;
## 36                <NA&gt;               Other
## 37                <NA&gt;        Barack Obama
## 38                <NA&gt;                <NA&gt;
## 39                <NA&gt;        Barack Obama
## 40                <NA&gt;                <NA&gt;
## 41         Republicans         Mitt Romney
## 42           Democrats        Barack Obama
## 43                <NA&gt;        Barack Obama
## 44         Republicans         Mitt Romney
## 45                <NA&gt;         Mitt Romney
## 46         Republicans         Mitt Romney
## 47                <NA&gt;         Mitt Romney
## 48                <NA&gt;         Mitt Romney
## 49                <NA&gt;                <NA&gt;
## 50                <NA&gt;         Mitt Romney