# Predict results of Elections

I have information on the votes in my town and in the country.

I want to predict the results in the country's elections knowing the results in my town. What methods I can use?

I have thought of linear models, but I don't know exactly how to do it:

summary(lm(Country~MyTown))


The results in R are:

Call:
lm(formula = Country ~ MyTown)

Residuals:
1       2       3       4       5       6
-0.8968  3.5258 -2.2629 -1.3784 -0.9361  1.9484

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  -7.3710     2.7238  -2.706 0.053749 .
MyTown        1.4423     0.1516   9.516 0.000681 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.496 on 4 degrees of freedom
Multiple R-squared:  0.9577,    Adjusted R-squared:  0.9471
F-statistic: 90.56 on 1 and 4 DF,  p-value: 0.0006807


And I would like to get the estimate for all the parties. Is a linear model a good idea? Are there more methods for it?

• From which data do you want to predict which information? From results of all parties in your town all results of all parties in the country? (this leaves you will $n$ features and $n$ target variables). Do you assume that the relation between all features and their target variables is the same? If no, modelling your problem this way might be a bad idea at all. BTW: even if you might be able to model things this way, be aware that your model might not be able to work the same way on future elections. – geekoverdose Jun 27 '16 at 16:44
• @ geekoverdose, Yes, exactly, I want to predict from the results in my town the results of the country. It is only for fun, I asume, that the results will be different for the next years – user4563174 Jun 27 '16 at 20:56
• Why would you expect a linear relationship? Election results must add up to 100 %. You need a model that ensures this. – Roland Jun 28 '16 at 7:32

As @Roland mentioned you should first check if your expectation of a linear relationship is applicable with your problem:

# use semi-linear dummy data
d <- mtcars[,4:3]
names(d) <- c('town', 'country')
# visualize relation
plot(d)


If the relation turns out to be (semi) linear you can fit a linear model to it. Be aware that depending on what you want to do with that model, the fitting and evaluation process would need to be adapted (e.g. if you want to predict the votes in the country for some parties from the relation you derived from other parties before, using a partitioning approach like repeated CV or LOOCV would be useful). This example is very similar to what you already did, but additionally uses LOOCV to obtain a realistic RMSE estimate for your model:

library(caret)
model <- train(x = data.frame(d$town), y = d$country, method = 'lm', metric = 'RMSE', trControl = trainControl(method = 'LOOCV'))
print(model)

Linear Regression

32 samples
1 predictors

No pre-processing
Resampling:

Summary of sample sizes: 31, 31, 31, 31, 31, 31, ...

Resampling results

RMSE  Rsquared
84.6  0.532


Using the model (which in the end is trained on all data provided to caret::train) you can also visualize the relation between predicted and observed values on your training and/or test data:

plot(predict(object = model, newdata = data.frame(d$town)), d$country)
abline(0,1)


Looking at a problem this way will give you a better understanding if the model actually is and does what you would expect and want it to be.