Skip to content

Instantly share code, notes, and snippets.

@davidshor
Last active February 1, 2018 08:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidshor/5ea3e6c4e80cdc87243253e47c47bc41 to your computer and use it in GitHub Desktop.
Save davidshor/5ea3e6c4e80cdc87243253e47c47bc41 to your computer and use it in GitHub Desktop.
special election regression
state district vote_17 vote_16 vote_12 exclude
IA SD-45 0.744897959 0.591397849 0.686868687 0
VA SD-22 0.430107527 0.427083333 0.454545455 0
VA HD-85 0.47 0.5 0.5 0
IA HD-89 0.727272727 0.559139785 0.636363636 0
MN HD-32B 0.47 0.344086022 0.43877551 0
DE SD-10 0.585858586 0.568421053 0.595959596 0
CT SD-02 0.75 0.855670103 0.858585859 0
CT SD-32 0.443298969 0.40625 0.434343434 0
CT HD-115 0.64 0.59375 0.696969697 0
KS KS-04 0.469387755 0.35483871 0.367346939 0
CT HD-68 0.22 0.329896907 0.387755102 0
OK HD-28 0.489795918 0.239583333 0.31 0
GA SD-32 0.43 0.425531915 0.316326531 0
NH HD-Carroll-6 0.52 0.463157895 0.434343434 0
NH HD-Hillsborough-44 0.45 0.452631579 0.484848485 0
NY SD-30 0.968421053 0.959183673 0.95959596 0
NY AD-09 0.58 0.381443299 0.43877551 0
MT MT-AL 0.468085106 0.384615385 0.432989691 0
SC HD-84 0.391752577 0.322916667 0.353535354 0
TN HD-95 0.360824742 0.298969072 0.232323232 0
SC SC-05 0.484 0.403 0.441 1
GA GA-06 0.481 0.492 0.381 1
special_elections <- read_csv("~/Downloads/special_elections.csv")
##Exclude SC-5 and GA-6 from the regression
lm_fit<-gam(vote_17~vote_16+vote_12,
data=special_elections[which(special_elections$exclude!=1),] )
##Note 2012 is a better predictor than 2016
summary(lm_fit)
special_elections$forecast<-predict(lm_fit,special_elections)
##View forecasts
special_elections[which(special_elections$exclude==1),]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment