DataCamp Analyzing Election and Polling Data in R
The House of Representatives in 2018
ANALYZING ELECTION AND POLLING DATA IN R
- G. Elliott Morris
The House of Representatives in 2018 G. Elliott Morris Data - - PowerPoint PPT Presentation
DataCamp Analyzing Election and Polling Data in R ANALYZING ELECTION AND POLLING DATA IN R The House of Representatives in 2018 G. Elliott Morris Data Journalist DataCamp Analyzing Election and Polling Data in R Political prediction as a
DataCamp Analyzing Election and Polling Data in R
ANALYZING ELECTION AND POLLING DATA IN R
DataCamp Analyzing Election and Polling Data in R
DataCamp Analyzing Election and Polling Data in R
DataCamp Analyzing Election and Polling Data in R
filter() polls_2018 %>% filter(date > "2018-06-01") mutate() polls_2018 %>% mutate(Dem.Margin = Dem - Rep) pull() polls_2018 %>% pull(Dem.Margin) mean() mean(polls_2018$Dem.Margin)
DataCamp Analyzing Election and Polling Data in R
filter() polls %>% filter(month(date) %in% c(8,9)) group_by() polls %>% group_by(year) summarise() polls %>% group_by(year) %>% summarise(avg = mean(Dem.Margin)
DataCamp Analyzing Election and Polling Data in R
ANALYZING ELECTION AND POLLING DATA IN R
DataCamp Analyzing Election and Polling Data in R
ANALYZING ELECTION AND POLLING DATA IN R
DataCamp Analyzing Election and Polling Data in R
DataCamp Analyzing Election and Polling Data in R
lm(Dem.Vote.Margin ~ Dem.Poll.Margin)
DataCamp Analyzing Election and Polling Data in R
ggplot(generic_ballot,aes(x=Dem.Poll.Margin,y=Dem.Vote.Margin, col=party_in_power) + geom_text(aes(label=ElecYear)) + geom_smooth(method='lm')
DataCamp Analyzing Election and Polling Data in R
model <- lm(Dem.Vote.Margin ~ Dem.Poll.Margin + party_in_power, data=polls_predict) summary(model) Call: lm(formula = Dem.Vote.Margin ~ Dem.Poll.Margin + party_in_power, data = polls_predict) Residuals: Min 1Q Median 3Q Max
Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -2.1168 1.1244 -1.883 0.078079 . Dem.Poll.Margin 0.8856 0.2070 4.278 0.000577 *** party_in_power -2.1348 0.8809 -2.423 0.027601 *
Residual standard error: 3.238 on 16 degrees of freedom Multiple R-squared: 0.7498, Adjusted R-squared: 0.7185 F-statistic: 23.98 on 2 and 16 DF, p-value: 1.535e-05
DataCamp Analyzing Election and Polling Data in R
predict(model, data.frame(Dem.Poll.Margin = 8, party_in_power=-1)) 1 7.102972
DataCamp Analyzing Election and Polling Data in R
DataCamp Analyzing Election and Polling Data in R
sqrt(mean(c(model$fitted.values - data$actual_results)^2)) * 1.96 sqrt(mean(c(model$fitted.values - polls_predict$Dem.Vote.Margin)^2)) *1.96 [1] 5.823251
DataCamp Analyzing Election and Polling Data in R
ANALYZING ELECTION AND POLLING DATA IN R
DataCamp Analyzing Election and Polling Data in R
ANALYZING ELECTION AND POLLING DATA IN R
DataCamp Analyzing Election and Polling Data in R
DataCamp Analyzing Election and Polling Data in R
DataCamp Analyzing Election and Polling Data in R
vote_share: Vote share for the president's party
pres_approve: Presidential approval q2_gdp: annual GDP growth from quarter two two_plus_terms: Term length
lm(vote_share ~ pres_approve + q2_gdp + two_plus_terms, pres_elecs)
DataCamp Analyzing Election and Polling Data in R
ggplot(pres_elecs,aes(x=predict,y=vote_share,label=Year)) + geom_abline() + geom_text()
DataCamp Analyzing Election and Polling Data in R
# calculate the model's root-mean-square error sqrt(mean(c(pres_elecs$predict-pres_elecs$vote_share)^2)) * 1.96 [1] 3.273301
DataCamp Analyzing Election and Polling Data in R
DataCamp Analyzing Election and Polling Data in R
ANALYZING ELECTION AND POLLING DATA IN R
DataCamp Analyzing Election and Polling Data in R
ANALYZING ELECTION AND POLLING DATA IN R
DataCamp Analyzing Election and Polling Data in R
lm
DataCamp Analyzing Election and Polling Data in R
choroplethr
ggplot for showing the relationship between three variables
DataCamp Analyzing Election and Polling Data in R
DataCamp Analyzing Election and Polling Data in R
ANALYZING ELECTION AND POLLING DATA IN R