Frank Hecker

Exploring Howard County election data with R, part 4

6 minute read

In part 3 of this series we constructed a linear model in R to estimate the proportion of voters in the 2010 Howard County general election who are unaffiliated or members of other parties. (See also part 1 and part 2.) For our second prediction we’ll estimate the percentage of those voting who are Democrats. We’ll again make use of the lm() function, but this time we can make use of the fact that the result of lm() can be stored in a variable (which in this case we arbitrarily name lmd):

[sourcecode light=”true”]

lmd <- lm(hgg$PctVotersD ~ hgg$Year)
lmd

Call:
lm(formula = hgg$PctVotersD ~ hgg$Year)

Coefficients:
(Intercept) hgg$Year
766.250 -0.358

[/sourcecode]

Before we go on, note that the slope for the linear model for Democratic voters (-0.358) is almost equal in magnitude, but opposite in sign, to the slope for the linear model for unaffiliated and other voters (0.3522). Put more simply, the models seem to indicate that ongoing increases in the percentage of unaffiliated and other voters in Howard County gubernatorial general elections since 1990 are matched on average by corresponding decreases in the percentage of Democratic voters (presumably leaving the percentage of Republican voters relatively static).

As we did before, we can use the estimated slope and intercept to calculate an estimated value for the percentage of Democratic voters in the 2010 general election, assuming that the above trend continues:

[sourcecode light=”true”]

-0.358 * 2010 + 766.250
[1] 46.67

[/sourcecode]

So 46.7% is our prediction for the percentage of voters who are Democrats, and as noted above 16.3% is our prediction for the percentage of unaffiliated and other voters. Subtracting the sum of these two values from 100 we obtain a prediction of 37.0% for the percentage of voters who are Republican.

Since we stored the linear model in the variable lmd we can do some additional analysis. First, we can compute what the predicted values of the percentage of Democratic voters would be based on the estimated linear relationship (using the predict() function), and compare that to the actual values:

[/sourcecode]
Note that since both hgg$PctVotersD and predict(lmd) contain five values, taking the difference between them produces five values as well.

So in general the actual and predicted values for the percentage of voters who are Democrats differ by about a percentage of the total vote. That likely means that our predicted value of 46.7% for 2010 could be off by at least that amount as well.

If we’d like more statistical goodness we can use the summary() function:

Some of these numbers we’ve seen before, including the slope, intercept, and residuals. Of the additional values, the most interesting for our purpose is the (multiple) R-squared value, which can be used as a measure of how well the linear model explains the observed data. More formally, the R-squared value (also known as the coefficient of determination) measures the proportion of the variability in the observed values that’s explained by the linear model. In this case the R-squared value of 0.7777 means that about three quarters of the variability in the observed data is explained by the linear model.1

This is a good but not great fit to the observed data. For another example let’s return to the linear model for the percentage of unaffiliated and other voters and look at the predicted values, residuals, and R-squared value:

Note that here the residuals are quite small (a fraction of a percent of the total vote) and the R-squared value is quite close to 1.0, indicating that the linear model explains almost all the variability in the observed data.2

Finally, let’s redo the graph from the last post to include trend lines for both the Democratic and independent proportions of the total voting population in gubernatorial elections since 1990:

Note that since we stored the linear models in the variables lmo and lmd we don’t have to manually type in the slope and intercept arguments (the coefficients of the line’s equation) to the abline() function. Instead we can simply use the coef() function to extract those coefficients and plug them in.

Statistics by themselves can take us only so far. We still have the question of why the proportion of unaffiliated and other voters seems to be increasing in such a strict linear manner from election to election. We also don’t know why the increase in the proportion of unaffiliated and other voters seems to be primarily affecting the proportion of Democratic voters and not (on average) the proportion of Republican voters.

I’ve already attempted an answer to the first question in a previous post. Regarding the second question, the graph above suggests that the proportion of Democratic voters may actually be about to grow again, and the proportion of Republican voters to decrease. If that turns out to be the case in the 2010 Howard County general election then our linear model for the Democratic proportion of voters may be wrong.

In the meantime in the next post I’ll look at another proposed approach to estimating the relative proportions of total voters from the two major parties and independents, this time using the data from past primary elections.

The ratio of these two numbers gives the proportion of variability in the observed data not explained by the linear model, which we can then subtract from 1 to give the proportion of variability explained by the model: