├── .gitignore ├── model ├── louisiana │ └── la.RData ├── data │ ├── senateData.RData │ └── misc │ │ ├── politician-slugs.csv │ │ └── races.csv ├── optimizing │ ├── error-spline.RData │ └── splines-new.RData ├── combine-data.R ├── senate-model-2014.R └── Functions.R ├── fundamentals ├── data │ ├── fundydata.rdata │ ├── biography │ │ └── experience.RData │ ├── incumbent-approval │ │ └── incumbent-approval.csv │ ├── results-and-biography │ │ ├── senate-master-results-bio.csv │ │ └── older-results │ │ │ └── oldresults2.csv │ └── pvi │ │ └── pvi.csv ├── fundamentals-source.R └── FunctionsFundamentals.R ├── output └── README.md ├── data-publisher └── public │ └── _big_assets │ └── README.md ├── master-public.R ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /model/louisiana/la.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheUpshot/leo-senate-model/HEAD/model/louisiana/la.RData -------------------------------------------------------------------------------- /model/data/senateData.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheUpshot/leo-senate-model/HEAD/model/data/senateData.RData -------------------------------------------------------------------------------- /fundamentals/data/fundydata.rdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheUpshot/leo-senate-model/HEAD/fundamentals/data/fundydata.rdata -------------------------------------------------------------------------------- /model/optimizing/error-spline.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheUpshot/leo-senate-model/HEAD/model/optimizing/error-spline.RData -------------------------------------------------------------------------------- /model/optimizing/splines-new.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheUpshot/leo-senate-model/HEAD/model/optimizing/splines-new.RData -------------------------------------------------------------------------------- /output/README.md: -------------------------------------------------------------------------------- 1 | Dated .RData files will be placed here. This directory is required for 2 | the master-public.R script to run. 3 | -------------------------------------------------------------------------------- /model/data/misc/politician-slugs.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheUpshot/leo-senate-model/HEAD/model/data/misc/politician-slugs.csv -------------------------------------------------------------------------------- /fundamentals/data/biography/experience.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheUpshot/leo-senate-model/HEAD/fundamentals/data/biography/experience.RData -------------------------------------------------------------------------------- /fundamentals/data/incumbent-approval/incumbent-approval.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheUpshot/leo-senate-model/HEAD/fundamentals/data/incumbent-approval/incumbent-approval.csv -------------------------------------------------------------------------------- /data-publisher/public/_big_assets/README.md: -------------------------------------------------------------------------------- 1 | Prediction output from the model realizations will be placed here. This 2 | directory is required for the master-public.R script to run. 3 | -------------------------------------------------------------------------------- /fundamentals/data/results-and-biography/senate-master-results-bio.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheUpshot/leo-senate-model/HEAD/fundamentals/data/results-and-biography/senate-master-results-bio.csv -------------------------------------------------------------------------------- /master-public.R: -------------------------------------------------------------------------------- 1 | workingDir <- getwd() 2 | dataDir <- paste(workingDir, "data-publisher/", sep = "/") 3 | modelDir <- paste(workingDir, "model", sep = "/") 4 | fundyDir <- paste(workingDir, "fundamentals", sep = "/") 5 | 6 | ### run the model 7 | setwd(modelDir) 8 | n.days <- 30 # number of days to sim. set to "all" to run all days. 9 | just.today <- T # if T, overrides n.days 10 | n.sims <- 50000 11 | source("senate-model-2014.R") 12 | 13 | if (just.today) source("combine-data.R") 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 The New York Times Company 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this library except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /model/combine-data.R: -------------------------------------------------------------------------------- 1 | #### 2 | wd <- paste0(dataDir, "public/_big_assets/") 3 | today <- trim(format(today(), format = "%e-%b-%y")) 4 | 5 | # update topline number 6 | filename <- paste0(wd, "senate-likelihood.tsv") 7 | if (file.exists(filename)) { 8 | df <- read.table(filename, header = T, as.is = T) 9 | df <- df[df$date != today, ] 10 | df.new <- read.table(paste0(wd, "senate-likelihood-new.tsv"), header = T, 11 | as.is = T) 12 | df.new$date[nrow(df.new)] <- today 13 | df <- rbind(df, df.new) 14 | write.table(df, file = filename, quote = F, sep='\t', row.names = F, 15 | col.names = c("date", "dem")) 16 | } else { 17 | file.copy(paste0(wd, "senate-likelihood-new.tsv"), to = filename) 18 | } 19 | 20 | # update state numbers 21 | filename <- paste0(wd, "senate-likelihood-all.tsv") 22 | if (file.exists(filename)) { 23 | df <- read.table(filename, header = T, as.is = T) 24 | df <- df[df$date != today, ] 25 | df.new <- read.table(paste0(wd, "senate-likelihood-all-new.tsv"), header = T, 26 | as.is = T) 27 | df.new$date <- today 28 | df <- rbind(df, df.new) 29 | write.table(df, file = filename, quote = F, sep='\t', row.names = F, 30 | col.names = c("office", "date", "dem")) 31 | } else { 32 | file.copy(paste0(wd, "senate-likelihood-all-new.tsv"), to = filename) 33 | } 34 | -------------------------------------------------------------------------------- /fundamentals/fundamentals-source.R: -------------------------------------------------------------------------------- 1 | library(gdata) 2 | library(lubridate) 3 | library(nlme) 4 | library(stringr) 5 | library(plotrix) 6 | library(plyr) 7 | 8 | ############## 9 | ### load stuff made by FundamentalsUpdateInputs.r 10 | ############## 11 | 12 | # load("data/fundydata.rdata") 13 | # killResults = 2014 14 | # daysOut = electionDay("2014")-Sys.Date() 15 | # intervalType = "prediction" 16 | 17 | ############## 18 | ### restrict ourselves to the data that we know. 19 | ############## 20 | 21 | fitModel <- function(daysOut, killResults = 2014, intervalType = "prediction", q1FECmissing = F) { 22 | 23 | dropQ1 <- F 24 | today <- (electionDay(2014) - Sys.Date()) 25 | if(daysOut %in% 203:(today-1) & q1FECmissing) dropQ1 <- T 26 | 27 | ## make data that is known as of a specific date 28 | fec2 <- fecAsOf(fec, daysOut, dropQ1) 29 | cookRatings2 <- cookAsOf(cookRatings, daysOut) 30 | incApproval2 <- thingAsOf(incApproval, daysOut, "net", "relevant.office") 31 | presApproval2 <- thingAsOf(presApproval, daysOut, "pctapp", "cycle") 32 | genBallot2 <- thingAsOf(genBallot, daysOut, "net", "cycle") 33 | partyFav2 <- thingAsOf(partyFav, daysOut, "net", "cycle") 34 | 35 | ## lots of messy reassembling with known data 36 | reduced$demMarginAll[reduced$year >= killResults] <- NA 37 | ready2model <- prepData(fec2, cookRatings2, incApproval2, presApproval2, genBallot2, partyFav2, reduced) 38 | 39 | ## no year-end FEC report for 2013 40 | noFECdem <- c("chad-taylor", "travis-childers", "david-domina") 41 | noFECrep <- c("ed-gillespie", "scott-brown") 42 | noYearEndReport <- ready2model$year == 2014 & (ready2model$slug.Democrat1 %in% noFECdem | ready2model$slug.Republican1 %in% noFECrep) & ready2model$demMoney%in%c(50,-50) 43 | ready2model$demMoney[noYearEndReport] <- 0 44 | 45 | ## fit a model 46 | form <- "demMarginAll ~ offyear + pvi + genBallot + incApproval2 + demMoney + netExp + norep + nodem" 47 | form <- as.formula(form) 48 | 49 | fitdata <- subset(ready2model, sigThirdParty == 0) 50 | m0 <- lm(form, data = fitdata) 51 | 52 | ## inflated variance for races that have a history of sen/pres vote being different, particularly the open ones 53 | ready2model$xx <- interaction(ready2model$presSenMostlyOpposite, ready2model$open2) 54 | m2 <- gls(form, 55 | data = ready2model, 56 | na.action = "na.omit", 57 | weights = varIdent(form = ~1 | xx)) 58 | var.inflate <- coef(m2$model, unconstrained = F) 59 | 60 | ### predictions for 2014 61 | nd <- subset(ready2model, year == killResults) 62 | #write.csv(nd[,c("office", "slug.Democrat1", "offyear", "pvi", "genBallot", "incApproval2", "demMoney", "netExp", "norep", "nodem")], file = paste0("~/Desktop/", daysOut, ".csv")) 63 | 64 | temp <- data.frame(suppressWarnings(predict(m0, newdata = nd, interval = intervalType))) ## don't care about rank deficiency when don't have 2 candidates. 65 | temp$office <- nd$office 66 | temp$matchup <- nd$matchup 67 | temp$democrat <- nd$slug.Democrat1 68 | temp$republican <- nd$slug.Republican1 69 | temp$predyear <- killResults 70 | temp$df <- summary(m0)$df[2] 71 | 72 | # where we are inflating variance: history of diff sen/pres * openness 73 | incAndPresDiff <- unique(subset(nd, presSenMostlyOpposite == 1 & open2 == 0)$office) 74 | openAndPresDiff <- unique(subset(nd, presSenMostlyOpposite == 1)$office) 75 | openAndPresDiff <- openAndPresDiff[!(openAndPresDiff %in% incAndPresDiff)] 76 | 77 | t.star <- qt(.975, temp$df) 78 | temp$se <- (temp$upr - temp$fit) / t.star 79 | 80 | pos <- temp$office %in% incAndPresDiff 81 | temp$se[pos] <- sqrt(var.inflate[2])*temp$se[pos] 82 | 83 | pos <- temp$office %in% openAndPresDiff 84 | temp$se[pos] <- sqrt(var.inflate[3])*temp$se[pos] 85 | 86 | list(model = temp, 87 | var.inflate = var.inflate[2:3], 88 | var.inflate.races = list(incAndPresDiff, openAndPresDiff)) 89 | } 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | leo-senate-model 2 | ================ 3 | 4 | Leo combines polls with other information to predict how many Senate races Democrats and Republicans will win in 2014. 5 | 6 | Leo uses: 7 | 8 | Historical results from the [Open Elections](http://openelections.github.io/) project, CQ Press [Voting and Elections](http://library.cqpress.com/elections/) Collection, [Dave Leip’s Atlas of U.S. Presidential Elections](http://uselectionatlas.org/), and the [Federal Election Commission](http://www.fec.gov/pubrec/electionresults.shtml). 9 | 10 | Fund-raising data from the [Federal Election Commission](http://www.fec.gov/finance/disclosure/candcmte_info.shtml). Biographical data from [Project Vote Smart](http://votesmart.org/) and candidate websites. 11 | 12 | Polls aggregated by [Pollster](http://elections.huffingtonpost.com/pollster/api), the [Roper Center for Public Opinion Research](http://www.ropercenter.uconn.edu/CFIDE/cf/action/home/index.cfm), the [U.S. Officials’ Job Approval Ratings](http://www.unc.edu/~beyle/jars.html) project, [Polling Report](http://www.pollingreport.com/), Gallup, [Talking Points Memo](http://polltracker.talkingpointsmemo.com/), [The Argo Journal](http://www.argojournal.com/"), [Real Clear Politics](http://www.realclearpolitics.com/epolls/other/generic_congressional_vote-2170.html) and [FiveThirtyEight](http://fivethirtyeight.com/). 13 | 14 | Brief instructions 15 | ------------------ 16 | 17 | * Please make sure the following R packages have been installed: 18 | `gam`, `gtools`, `lubridate`, `maps`, `RJSONIO`, `gdata`, 19 | `plotrix`, `zoo` 20 | 21 | * Change directory to the top-level working directory of this Git 22 | repository. 23 | 24 | * Run: `Rscript master-public.R` 25 | 26 | * Prediction output can then be found in the 27 | `data-publisher/public/_big_assets/` subdirectory. 28 | 29 | 30 | ### Sample output data in `/data-publisher/public/_big_assets` 31 | 32 | 33 | ##### histogram.tsv 34 | 35 | | dem | chance | 36 | |-----|--------| 37 | | 44 | 0 | 38 | | 45 | 0 | 39 | | 46 | 0 | 40 | | 47 | 0.05 | 41 | | 48 | 0.25 | 42 | | 49 | 0.15 | 43 | | 50 | 0.3 | 44 | | 51 | 0.15 | 45 | 46 | ##### matchups.tsv 47 | 48 | | statePostal | secondary | probability | mean | scale | office | dem | rep | 49 | |-------------|-----------|-------------|------|-------|--------|-----|-----| 50 | |AK||0.0675992536643285|13.3371042222312|6.37706218633251|ak-senate-class-ii-2014|mark-begich|joe-miller 51 | |AK||0.672480368777626|-0.369330147923164|6.40862522137136|ak-senate-class-ii-2014|mark-begich|daniel-s-sullivan 52 | |AK||0.259920377558046|-1.42864965262853|6.43282051331378|ak-senate-class-ii-2014|mark-begich|mead-treadwell 53 | |AL||1|-92.2586242817344|13.0668255800746|al-senate-class-ii-2014|NA|jeff-sessions 54 | |AR||1|-0.588349828909989|5.43497477566511|ar-senate-class-ii-2014|mark-pryor|thomas-cotton-1 55 | |CO||1|2.75130137738909|5.32999951709124|co-senate-class-ii-2014|mark-udall|cory-gardner 56 | |DE||0.0485586168626867|22.1226995091985|12.2918575405219|de-senate-class-ii-2014|christopher-a-coons|tom-kovach 57 | |DE||0.951441383137313|24.462299974418|12.2976359697371|de-senate-class-ii-2014|christopher-a-coons|christine-o'donnell 58 | 59 | ##### nyt-ratings-today.json 60 | 61 | ```json 62 | { 63 | "date": "2014-04-22T13:19:49-0400", 64 | "data": [ 65 | { 66 | "dem-pct": 49.428, 67 | "state": "AK", 68 | "office": "ak-senate-class-ii-2014", 69 | "fips": "2", 70 | "nyt-rating": 4 71 | }, 72 | { 73 | "dem-pct": 4.1438E-10, 74 | "state": "AL", 75 | "office": "al-senate-class-ii-2014", 76 | "fips": "1", 77 | "nyt-rating": 7 78 | }, 79 | { 80 | "dem-pct": 45.693, 81 | "state": "AR", 82 | "office": "ar-senate-class-ii-2014", 83 | "fips": "5", 84 | "nyt-rating": 4 85 | } 86 | ] 87 | } 88 | ``` 89 | 90 | 91 | ##### parameters.json 92 | 93 | ```json 94 | { 95 | "continuingSeats": { 96 | "dem": 34, 97 | "rep": 30 98 | }, 99 | "nationalErrorScale": 0.40797, 100 | "upsetProb": 40, 101 | "houseEffect": 3.5, 102 | "mt": 85, 103 | "mtNum": "17", 104 | "mtDenom": "20", 105 | "aan": "an" 106 | } 107 | ``` 108 | 109 | 110 | ##### senate-likelihood-all.tsv 111 | 112 | | office | date | dem | 113 | |--------|------|-----| 114 | |ak-senate-class-ii-2014|1-Jan-14||57.0869004591178| 115 | |ak-senate-class-ii-2014|25-Feb-14|51.4921956208518| 116 | |ak-senate-class-ii-2014|22-Apr-14|49.4276015976854| 117 | |al-senate-class-ii-2014|1-Jan-14||2.8079525308588e-09| 118 | |al-senate-class-ii-2014|25-Feb-14|9.22057393777991e-10| 119 | |al-senate-class-ii-2014|22-Apr-14|4.14383203099536e-10| 120 | 121 | ##### senate-likelihood.tsv 122 | | date | dem | 123 | |-----------|-----| 124 | | 1-Jan-14 | 50 | 125 | | 25-Feb-14 | 35 | 126 | | 22-Apr-14 | 55 | 127 | 128 | 129 | ##### slug-lookup.tsv 130 | 131 | | slug | name | | 132 | |-------------------|----------|-----------| 133 | | mark-begich | Mark | Begich | 134 | | william-bryk | William | Bryk | 135 | | john-jaramillo | John | Jaramillo | 136 | | joe-miller | Joe | Miller | 137 | | sarah-palin | Sarah | Palin | 138 | | daniel-s-sullivan | Dan | Sullivan | 139 | | kathleen-tonn | Kathleen | Tonn | 140 | | mead-treadwell | Mead | Treadwell | 141 | 142 | 143 | -------------------------------------------------------------------------------- /fundamentals/data/results-and-biography/older-results/oldresults2.csv: -------------------------------------------------------------------------------- 1 | class,year,postal,office,offyear,demPctMajor,RaceNotes ii,1990,AL,al-senate-class-ii-1990,1,60.57, ii,1990,AK,ak-senate-class-ii-1990,1,32.71, ii,1990,AR,ar-senate-class-ii-1990,1,100, ii,1990,CO,co-senate-class-ii-1990,1,42.8, ii,1990,DE,de-senate-class-ii-1990,1,63.63, ii,1990,GA,ga-senate-class-ii-1990,1,100, ii,1990,ID,id-senate-class-ii-1990,1,38.71, ii,1990,IL,il-senate-class-ii-1990,1,65.07, ii,1990,IA,ia-senate-class-ii-1990,1,54.53, ii,1990,KS,ks-senate-class-ii-1990,1,26.4, ii,1990,KY,ky-senate-class-ii-1990,1,47.81, ii,1990,LA,la-senate-class-ii-1990,1,55.35,"Results are from the October 6th Primary. Since one candidate received the majority of the vote, no general (run-off) election was required." ii,1990,ME,me-senate-class-ii-1990,1,38.65, ii,1990,MA,ma-senate-class-ii-1990,1,57.1, ii,1990,MI,mi-senate-class-ii-1990,1,58.23, ii,1990,MN,mn-senate-class-ii-1990,1,51.34, ii,1990,MS,ms-senate-class-ii-1990,1,0,Wayne county did not report votes for the unopposed candidate Thad Cochran; therefore the certified results do not include any returns for Wayne county. ii,1990,MT,mt-senate-class-ii-1990,1,69.87, ii,1990,NE,ne-senate-class-ii-1990,1,59.01, ii,1990,NH,nh-senate-class-ii-1990,1,32.48, ii,1990,NJ,nj-senate-class-ii-1990,1,51.55, ii,1990,NM,nm-senate-class-ii-1990,1,27.05, ii,1990,NC,nc-senate-class-ii-1990,1,47.44, ii,1990,OK,ok-senate-class-ii-1990,1,83.18, ii,1990,OR,or-senate-class-ii-1990,1,46.25, ii,1990,RI,ri-senate-class-ii-1990,1,61.83, ii,1990,SC,sc-senate-class-ii-1990,1,33.62, ii,1990,SD,sd-senate-class-ii-1990,1,46.25, ii,1990,TN,tn-senate-class-ii-1990,1,69.43, ii,1990,TX,tx-senate-class-ii-1990,1,38.31, ii,1990,VA,va-senate-class-ii-1990,1,0, ii,1990,WV,wv-senate-class-ii-1990,1,68.32, ii,1990,WY,wy-senate-class-ii-1990,1,36.06, ii,1990,NA,NA-senate-class-ii-1990,1,NA, ii,1990,NA,NA-senate-class-ii-1990,1,NA, ii,1990,NA,NA-senate-class-ii-1990,1,NA, i,1988,AZ,az-senate-class-i-1988,0,58.01, i,1988,CA,ca-senate-class-i-1988,0,45.46, i,1988,CT,ct-senate-class-i-1988,0,50.37, i,1988,DE,de-senate-class-i-1988,0,37.94, i,1988,FL,fl-senate-class-i-1988,0,49.58, i,1988,HI,hi-senate-class-i-1988,0,78.73, i,1988,IN,in-senate-class-i-1988,0,31.86, i,1988,ME,me-senate-class-i-1988,0,81.2, i,1988,MD,md-senate-class-i-1988,0,61.8, i,1988,MA,ma-senate-class-i-1988,0,65.69, i,1988,MI,mi-senate-class-i-1988,0,61.09, i,1988,MN,mn-senate-class-i-1988,0,0,"The other vote was: Humphrey, Hubert H. III (Democrat Farmer-Labor) 856,694" i,1988,MS,ms-senate-class-i-1988,0,46.09, i,1988,MO,mo-senate-class-i-1988,0,31.93, i,1988,MT,mt-senate-class-i-1988,0,48.13, i,1988,NE,ne-senate-class-i-1988,0,57.65, i,1988,NV,nv-senate-class-i-1988,0,52.11, i,1988,NJ,nj-senate-class-i-1988,0,54.24, i,1988,NM,nm-senate-class-i-1988,0,63.31, i,1988,NY,ny-senate-class-i-1988,0,68.34, i,1988,ND,nd-senate-class-i-1988,0,60.35, i,1988,OH,oh-senate-class-i-1988,0,56.98, i,1988,PA,pa-senate-class-i-1988,0,32.81, i,1988,RI,ri-senate-class-i-1988,0,45.41, i,1988,TN,tn-senate-class-i-1988,0,65.34, i,1988,TX,tx-senate-class-i-1988,0,59.67, i,1988,UT,ut-senate-class-i-1988,0,32.1, i,1988,VT,vt-senate-class-i-1988,0,30.45, i,1988,VA,va-senate-class-i-1988,0,71.29, i,1988,WA,wa-senate-class-i-1988,0,48.91, i,1988,WV,wv-senate-class-i-1988,0,64.77, i,1988,WI,wi-senate-class-i-1988,0,52.27, i,1988,WY,wy-senate-class-i-1988,0,49.63, i,1988,NA,NA-senate-class-i-1988,0,NA, i,1988,NA,NA-senate-class-i-1988,0,NA, i,1988,NA,NA-senate-class-i-1988,0,NA, iii,1986,AL,al-senate-class-iii-1986,1,50.28,NA iii,1986,AK,ak-senate-class-iii-1986,1,44.94,NA iii,1986,AZ,az-senate-class-iii-1986,1,39.52,NA iii,1986,AR,ar-senate-class-iii-1986,1,62.28,NA iii,1986,CA,ca-senate-class-iii-1986,1,50.73,NA iii,1986,CO,co-senate-class-iii-1986,1,50.79,NA iii,1986,CT,ct-senate-class-iii-1986,1,65.02,NA iii,1986,FL,fl-senate-class-iii-1986,1,54.74,NA iii,1986,GA,ga-senate-class-iii-1986,1,50.92,NA iii,1986,HI,hi-senate-class-iii-1986,1,73.57,NA iii,1986,ID,id-senate-class-iii-1986,1,48.44,NA iii,1986,IL,il-senate-class-iii-1986,1,65.87,NA iii,1986,IN,in-senate-class-iii-1986,1,38.87,NA iii,1986,IA,ia-senate-class-iii-1986,1,33.71,NA iii,1986,KS,ks-senate-class-iii-1986,1,29.95,NA iii,1986,KY,ky-senate-class-iii-1986,1,74.4,NA iii,1986,LA,la-senate-class-iii-1986,1,52.82,NA iii,1986,MD,md-senate-class-iii-1986,1,60.69,NA iii,1986,MO,mo-senate-class-iii-1986,1,47.36,NA iii,1986,NV,nv-senate-class-iii-1986,1,52.9,NA iii,1986,NH,nh-senate-class-iii-1986,1,33.96,NA iii,1986,NY,ny-senate-class-iii-1986,1,42.02,NA iii,1986,NC,nc-senate-class-iii-1986,1,51.76,NA iii,1986,ND,nd-senate-class-iii-1986,1,50.37,NA iii,1986,OH,oh-senate-class-iii-1986,1,62.45,NA iii,1986,OK,ok-senate-class-iii-1986,1,44.79,NA iii,1986,OR,or-senate-class-iii-1986,1,36.41,NA iii,1986,PA,pa-senate-class-iii-1986,1,43.17,NA iii,1986,SC,sc-senate-class-iii-1986,1,63.91,NA iii,1986,SD,sd-senate-class-iii-1986,1,51.6,NA iii,1986,UT,ut-senate-class-iii-1986,1,26.86,NA iii,1986,VT,vt-senate-class-iii-1986,1,64.67,NA iii,1986,WA,wa-senate-class-iii-1986,1,51,NA iii,1986,WI,wi-senate-class-iii-1986,1,48.23,NA iii,1986,NA,NA-senate-class-iii-1986,1,NA,NA iii,1986,NA,NA-senate-class-iii-1986,1,NA,NA iii,1986,NA,NA-senate-class-iii-1986,1,NA,NA i,1984,AL,al-senate-class-i-1984,0,63.32, i,1984,AK,ak-senate-class-i-1984,0,28.58, i,1984,AR,ar-senate-class-i-1984,0,57.35, i,1984,CO,co-senate-class-i-1984,0,35.02, i,1984,DE,de-senate-class-i-1984,0,60.11, i,1984,GA,ga-senate-class-i-1984,0,79.94, i,1984,ID,id-senate-class-i-1984,0,26.48, i,1984,IL,il-senate-class-i-1984,0,50.95, i,1984,IA,ia-senate-class-i-1984,0,55.95, i,1984,KS,ks-senate-class-i-1984,0,21.84, i,1984,KY,ky-senate-class-i-1984,0,49.79, i,1984,LA,la-senate-class-i-1984,0,90.64,"Results are from the Sept. 29th Primary. Since the winner received a majority of the votes in the primary, no General Election was required." i,1984,ME,me-senate-class-i-1984,0,26.07, i,1984,MA,ma-senate-class-i-1984,0,55.06, i,1984,MI,mi-senate-class-i-1984,0,52.33, i,1984,MN,mn-senate-class-i-1984,0,41.55, i,1984,MS,ms-senate-class-i-1984,0,39.06, i,1984,MT,mt-senate-class-i-1984,0,58.3, i,1984,NE,ne-senate-class-i-1984,0,51.96, i,1984,NH,nh-senate-class-i-1984,0,41.08, i,1984,NJ,nj-senate-class-i-1984,0,64.78, i,1984,NM,nm-senate-class-i-1984,0,28.1, i,1984,NC,nc-senate-class-i-1984,0,48.06, i,1984,OK,ok-senate-class-i-1984,0,76.35, i,1984,OR,or-senate-class-i-1984,0,33.45, i,1984,RI,ri-senate-class-i-1984,0,72.55, i,1984,SC,sc-senate-class-i-1984,0,32.25, i,1984,SD,sd-senate-class-i-1984,0,25.51, i,1984,TN,tn-senate-class-i-1984,0,64.24, i,1984,TX,tx-senate-class-i-1984,0,41.41, i,1984,VA,va-senate-class-i-1984,0,29.95, i,1984,WV,wv-senate-class-i-1984,0,52.06, i,1984,WY,wy-senate-class-i-1984,0,21.68, i,1984,NA,NA-senate-class-i-1984,0,NA, i,1984,NA,NA-senate-class-i-1984,0,NA, i,1984,NA,NA-senate-class-i-1984,0,NA, -------------------------------------------------------------------------------- /model/senate-model-2014.R: -------------------------------------------------------------------------------- 1 | 2 | source("Functions.R") 3 | load("data/senateData.RData") 4 | 5 | source("../fundamentals/FunctionsFundamentals.R") 6 | source("../fundamentals/fundamentals-source.R") 7 | load("../fundamentals/data/fundydata.rdata") 8 | 9 | library(nnet) 10 | library(zoo) 11 | 12 | new.races <- races[sapply(races, function(x) x$cycle == 2014)] 13 | race.names <- names(new.races) 14 | new.races <- new.races[order(race.names)] 15 | races <- races[sapply(races, function(x) x$cycle != 2014)] 16 | old.polls <- pollPrep(races, max.margin=20) # only needed for pollster ratings / lv adjustment 17 | 18 | day.start <- as.numeric(electionDay(2014) - as.Date("2014-01-01")) 19 | day.end <- as.numeric(electionDay(2014) - today()) 20 | 21 | if (just.today) { 22 | day.start <- day.end 23 | } 24 | 25 | if (n.days == "all") { 26 | day.seq <- day.start:day.end 27 | } else { 28 | day.seq <- unique(round(seq(day.start, day.end, length = n.days))) 29 | } 30 | 31 | ##### 32 | # louisiana - handle possiblility Landrieu gets more than 50% in jungle primary 33 | load("louisiana/la.RData") 34 | 35 | # fec 36 | d <- max(day.seq) 37 | fec.new <- subset(read.csv("../fundamentals/data/2014-fec-update/fec.csv", as.is = T), cycle == 2014) 38 | fec.start <- subset(fec.new, daysOut >= d) 39 | fec.start <- aggregate(list(money = fec.start$money), 40 | list(fecid = fec.start$cand_id), sum, na.rm = T) 41 | 42 | # initialize 43 | polls <- data.frame() 44 | race.names <- names(new.races) 45 | for (race.name in race.names) { 46 | x <- new.races[[match(race.name, race.names)]] 47 | 48 | fec.ids <- x$fecid 49 | parties <- x$party 50 | 51 | d.pos <- which(parties == "DEM") 52 | r.pos <- which(parties == "REP") 53 | 54 | d.names <- x$slug[d.pos] 55 | r.names <- x$slug[r.pos] 56 | 57 | x$serious[x$day_in < d] <- 0 58 | x$serious[x$day_out < d] <- 1 59 | 60 | if (!is.null(x$polls)) { 61 | race.polls <- subset(x$polls, add_date >= d) 62 | if (nrow(race.polls) == 0) { 63 | race.polls <- NULL 64 | } 65 | } else { 66 | race.polls <- NULL 67 | } 68 | 69 | money <- fec.start$money[match(fec.ids, fec.start$fecid)] 70 | money[is.na(money)] <- 0 71 | 72 | d.probs <- probGet(d.names, d.pos, "D", money, days = d, race.polls) 73 | r.probs <- probGet(r.names, r.pos, "R", money, days = d, race.polls) 74 | 75 | probs <- outer(r.probs, d.probs) 76 | 77 | if (sum(probs) == 0) { 78 | probs <- probs + 1 79 | } 80 | 81 | rownames(probs) <- r.names 82 | colnames(probs) <- d.names 83 | 84 | new.races[[match(race.name, names(new.races))]]$probs <- probs 85 | 86 | polls <- imputePolls(polls, new.races, race.name) 87 | } 88 | colnames(polls)[match("cycle", colnames(polls))] <- "year" 89 | new.polls <- pollPrep2014(polls, lv.adj) 90 | 91 | ##### save matchup probabilities by day 92 | prob_list <- setNames(vector("list", length(race.names)), race.names) 93 | for (race.name in race.names) { 94 | cat(race.name, "\n") 95 | ind <- match(race.name, names(new.races)) 96 | x <- new.races[[ind]] 97 | 98 | fec.ids <- x$fecid 99 | parties <- x$party 100 | 101 | d.pos <- which(parties == "DEM") 102 | r.pos <- which(parties == "REP") 103 | 104 | d.names <- x$slug[d.pos] 105 | r.names <- x$slug[r.pos] 106 | 107 | for (i in seq_along(day.seq)) { 108 | x <- new.races[[ind]] 109 | 110 | d <- day.seq[i] 111 | 112 | x$serious[x$day_in < d] <- 0 113 | x$serious[x$day_out < d] <- 1 114 | 115 | if (!is.null(x$polls)) { 116 | race.polls <- subset(x$polls, add_date >= d) 117 | if (nrow(race.polls) == 0) { 118 | race.polls <- NULL 119 | } 120 | } else { 121 | race.polls <- NULL 122 | } 123 | 124 | fec.temp <- subset(fec.new, daysOut >= d) 125 | fec.temp <- aggregate(list(money = fec.temp$money), 126 | list(fecid = fec.temp$cand_id), sum, na.rm = T) 127 | 128 | money <- fec.temp$money[match(fec.ids, fec.temp$fecid)] 129 | money[is.na(money)] <- 0 130 | 131 | d.probs <- probGet(d.names, d.pos, "D", money, days = d, race.polls) 132 | r.probs <- probGet(r.names, r.pos, "R", money, days = d, race.polls) 133 | 134 | probs <- outer(r.probs, d.probs) 135 | 136 | if (sum(probs) == 0) { 137 | probs <- probs + 1 138 | } 139 | 140 | rownames(probs) <- r.names 141 | colnames(probs) <- d.names 142 | 143 | prob_list[[match(race.name, names(prob_list))]][[i]] <- probs 144 | } 145 | } 146 | probs <- unlist(prob_list) 147 | 148 | ##### fundamentals 149 | fm_list <- lapply(day.seq, fitModel, q1FECmissing = T) 150 | 151 | ##### polling averages 152 | m00 <- fm_list[[1]]$model 153 | d.pos <- as.numeric(substr(m00$matchup, 1, 1)) 154 | r.pos <- as.numeric(substr(m00$matchup, 3, 3)) 155 | old.race <- m00[1, ]$office 156 | # save.image("../output/modelData.RData") 157 | # load("../output/modelData.RData") 158 | 159 | results <- data.frame() 160 | h.eff_list <- list() 161 | poll.df_list <- list() 162 | index <- data.frame() 163 | for (i in 1:nrow(m00)) { 164 | cat(i, "\n") 165 | 166 | key.race <- m00[i, ]$office 167 | 168 | if (key.race != old.race) { 169 | x <- new.races[[match(old.race, names(new.races))]] 170 | polls <- imputePollsNew(polls, new.races, old.race, x$probs, delete=T) 171 | old.race <- key.race 172 | } 173 | 174 | key.dem <- m00[i, ]$dem 175 | key.rep <- m00[i, ]$rep 176 | 177 | x <- new.races[[match(key.race, names(new.races))]] 178 | 179 | kd.pos <- match(key.dem, x$slug) 180 | kr.pos <- match(key.rep, x$slug) 181 | 182 | if (x$probs[cbind(r.pos[i], d.pos[i])] == 0 & 183 | all(x$day_in[c(kd.pos, kr.pos)] > 307)) { 184 | next 185 | } 186 | 187 | index <- rbind(index, 188 | data.frame(key.race = key.race, 189 | key.dem = key.dem, 190 | key.rep = key.rep)) 191 | probs <- 0*x$probs 192 | probs[cbind(r.pos[i], d.pos[i])] <- 1 193 | 194 | polls <- imputePollsNew(polls, new.races, key.race, probs, delete=T) 195 | new.polls <- pollPrep2014(polls, lv.adj) 196 | 197 | p <- prob_list[[match(key.race, names(prob_list))]] 198 | 199 | h.eff <- NULL 200 | poll.df <- NULL 201 | for (ii in seq_along(day.seq)) { 202 | temp.probs <- cbind(p[[ii]]) 203 | if (temp.probs[cbind(r.pos[i], d.pos[i])] != 0) { 204 | d <- day.seq[ii] 205 | m0 <- fm_list[[ii]]$model[i, ] 206 | forecast.obj <- pollPredict(2014, d, new.polls, new.races, 207 | race.names = m0$office, 208 | fund.mean = m0$fit, 209 | fund.se = m0$se, 210 | prior.alpha = m0$df / 2) 211 | res <- forecast.obj$forecast 212 | res$d <- d 213 | forecast.obj$h.eff$d <- d 214 | if (!is.null(forecast.obj$poll.df)) { 215 | forecast.obj$poll.df$d <- d 216 | } 217 | res$key.dem <- key.dem 218 | res$key.rep <- key.rep 219 | results <- rbind(results, res) 220 | h.eff <- rbind(h.eff, forecast.obj$h.eff) 221 | poll.df <- rbind(poll.df, forecast.obj$poll.df) 222 | } 223 | } 224 | h.eff_list[[length(h.eff_list) + 1]] <- h.eff 225 | poll.df_list[[length(poll.df_list) + 1]] <- if (is.null(poll.df)) { 226 | NA 227 | } else { 228 | poll.df 229 | } 230 | } 231 | index$index <- 1:nrow(index) 232 | 233 | ##### adjust louisiana ##### 234 | pos <- which(results$office == "la-senate-class-ii-2014") 235 | p <- (results$prob50 + (1 - results$prob50)*results$prob)[pos] 236 | results$mean[pos] <- qt(p, 2*results$alpha[pos])*results$scale[pos] 237 | results$prob[pos] <- p 238 | 239 | # insert matchup probs into dataframe 240 | results$matchupProb <- NA 241 | day.seq <- rev(sort(unique(results$d))) 242 | for (i in seq_along(day.seq)) { 243 | d <- day.seq[i] 244 | pos <- which(results$d == d) 245 | p <- unlist(sapply(prob_list, function(x) x[[i]])) 246 | p <- p[p != 0] 247 | results$matchupProb[pos] <- p 248 | } 249 | 250 | filename <- format(Sys.time(), "../output/%m%d%H%M.RData") 251 | save(h.eff_list, poll.df_list, prob_list, results, file = filename) 252 | 253 | ###################### -- 254 | ##### OUTPUT ##### 255 | ###################### -- 256 | 257 | temp <- subset(results, d == day.end) 258 | df <- data.frame(statePostal = toupper(substr(temp$office, 1, 2)), 259 | secondary = "", 260 | probability = temp$matchupProb, 261 | mean = temp$mean, 262 | scale = temp$scale, 263 | office = temp$office, 264 | dem = temp$key.dem, 265 | rep = temp$key.rep, 266 | stringsAsFactors = F) 267 | df$secondary[grep("(ok|sc).+i{3}", temp$office)] <- "-2" 268 | filename <- paste0(dataDir, "public/_big_assets/matchups.tsv") 269 | write.table(df, quote = F, sep = "\t", file = filename, row.names = F) 270 | 271 | matching <- NULL 272 | for (i in seq_along(new.races)) { 273 | x <- new.races[[i]] 274 | matching <- rbind(matching, cbind(x$slug, str_trim(x$candidates))) 275 | } 276 | # middle initials, etc. 277 | matching[, 2] <- candidateRename(matching[, 2]) 278 | filename <- paste0(dataDir, "public/_big_assets/slug-lookup.tsv") 279 | write.table(matching, quote = F, sep = "\t", file = filename, row.names = F, 280 | col.names = c("slug", "name")) 281 | 282 | results$tau[is.na(results$tau)] <- 0 283 | results$estN11 <- getEstimate(results, "normal") 284 | results$estP11 <- getEstimate(results, "polls") 285 | results$estF11 <- getEstimate(results, "fundys") 286 | results$estN10 <- getEstimate(results, "normal", T, F) 287 | results$estP10 <- getEstimate(results, "polls", T, F) 288 | results$estF10 <- getEstimate(results, "fundys", T, F) 289 | results$estN01 <- getEstimate(results, "normal", F) 290 | results$estP01 <- getEstimate(results, "polls", F) 291 | results$estF01 <- getEstimate(results, "fundys", F) 292 | results$estN00 <- getEstimate(results, "normal", F, F) 293 | results$estP00 <- getEstimate(results, "polls", F, F) 294 | results$estF00 <- getEstimate(results, "fundys", F, F) 295 | 296 | ## adjust louisiana 297 | pos <- which(results$office == "la-senate-class-ii-2014") 298 | results$estN11[pos] <- qt(results$prob[pos], 2*results$alpha[pos])*results$scale[pos] 299 | 300 | 301 | ##### SIMULATE ##### 302 | day.seq <- rev(sort(unique(results$d))) 303 | breakpoints <- c(-1e-8, 5, 15, 35) 304 | load("optimizing/error-spline.RData") 305 | 306 | for (snowflakes in c(F)) { 307 | 308 | cat("\n\n##### SIMULATING THINGS #####\n") 309 | sim_list_fund <- sim_list <- list() 310 | 311 | for (i in seq_along(day.seq)) { 312 | d <- day.seq[i] 313 | cat(d, "\n") 314 | keep <- results[results$d == d, ] 315 | # outputs some parameters too 316 | sim_list[[i]] <- electionSimNew(keep, n.sims = n.sims, 317 | s = error.spline$y[match(round(d), error.spline$x)], 318 | breaks = breakpoints) 319 | } 320 | 321 | outputGen(results, sim_list, sim_list_fund, breaks = breakpoints, 322 | best = F, snowflakes = snowflakes) 323 | 324 | } 325 | if (just.today) { 326 | source("combine-data.R") 327 | filename <- format(Sys.time(), "../output/%m%d%H%M-sims.RData") 328 | save(sim_list, file = filename) 329 | } 330 | 331 | # la tipping-point prob 332 | pos <- match("la-senate-class-ii-2014", race.names) 333 | sims <- sim_list[[length(sim_list)]] 334 | prob <- (sum(34 + colSums(sims > 0) == 50 & sims[pos, ] > 0) + 335 | sum(34 + colSums(sims < 0) == 51 & sims[pos, ] < 0)) / n.sims 336 | print(paste("## prob that control of senate determined by result in LA: ", prob)) 337 | 338 | # current party 339 | currentParty <- sapply(new.races, function(x) { 340 | x$party[match(1, x$inc)] 341 | }) 342 | currentParty["ga-senate-class-ii-2014"] <- "REP" 343 | currentParty["ia-senate-class-ii-2014"] <- "DEM" 344 | currentParty["mi-senate-class-ii-2014"] <- "DEM" 345 | currentParty["ne-senate-class-ii-2014"] <- "REP" 346 | currentParty["ok-senate-class-iii-2014"] <- "REP" 347 | currentParty["sd-senate-class-ii-2014"] <- "DEM" 348 | currentParty["wv-senate-class-ii-2014"] <- "DEM" 349 | 350 | # prob that REPS hold every currently-held seat 351 | w <- apply(sims[currentParty == "REP", ] < 0, 2, all) 352 | mean(w) 353 | 354 | # prob that all of our forecasts are right: 355 | res <- ddply(results, .(office, d), summarize, 356 | mean = weighted.mean(mean, matchupProb), 357 | probs = weighted.mean(prob, matchupProb)) 358 | res <- arrange(res, office, desc(d)) 359 | res <- subset(res, d == min(day.seq)) 360 | dem.call <- res$probs > .5 361 | w <- apply(sims, 2, function(x) all(!xor((x > 0), dem.call))) 362 | mean(w) 363 | 364 | 365 | ##### 366 | 367 | # some plots if you want 368 | # par(mfcol = c(4, 4), las = 1) 369 | # race.name <- "nc-senate-class-ii-2014" 370 | # day <- min(results$d) 371 | # sims <- sapply(sim_list, function(x) rowMeans(x > 0)) 372 | # for (i in seq_along(race.names)) { 373 | # par(mar = c(2, 4, 4, 2)) 374 | # race.name <- race.names[i] 375 | # temp <- subset(results, office == race.name & d == day) 376 | # pos <- which.is.max(temp$matchupProb) 377 | # dem <- temp$key.dem[pos] 378 | # rep <- temp$key.rep[pos] 379 | # temp <- subset(results, office == race.name & key.dem == dem & key.rep == rep) 380 | # if (nrow(temp) == 0) next 381 | # plot.new() 382 | # plot.window(range(day.seq), c(-30, 30)) 383 | # fit <- temp$mean 384 | # upper <- fit + qt(.975, df=2*temp$alpha)*temp$scale 385 | # lower <- fit + qt(.025, df=2*temp$alpha)*temp$scale 386 | # polygon(c(day.seq, rev(day.seq)), c(rev(lower), upper), border=NA, 387 | # col=adjustcolor("light gray", .5)) 388 | # upper <- fit + qt(.75, df=2*temp$alpha)*temp$scale 389 | # lower <- fit + qt(.25, df=2*temp$alpha)*temp$scale 390 | # polygon(c(day.seq, rev(day.seq)), c(rev(lower), upper), border=NA, 391 | # col=adjustcolor("light gray", 1)) 392 | # points(rev(day.seq), fit, type="l") 393 | # race.polls <- polls[polls$office == race.name, ] 394 | # for (j in seq_along(unique(race.polls$pollster))) { 395 | # q <- subset(race.polls, pollster == unique(race.polls$pollster)[j]) 396 | # x <- min(day.seq) + max(day.seq) - q$mid_date 397 | # points(x, q$margin, pch=j) 398 | # } 399 | # points(rev(day.seq), temp$prior.mean, type="l", lty=2) 400 | # points(rev(day.seq), temp$pollAvg, type="l", col="dark blue") 401 | # axis(2) 402 | # title(race.name) 403 | # title(paste(dem, rep, sep = " | "), line = .2, font.main = 1) 404 | # ### 405 | # par(mar = c(3, 4, 2, 2)) 406 | # plot.new() 407 | # plot.window(xlim = range(day.seq), ylim = c(0, 1)) 408 | # points(rev(day.seq), sims[i ,], type = "l", lwd = 2, col = "blue") 409 | # points(rev(day.seq), 1 - sims[i ,], type = "l", lwd = 2, col = "red") 410 | # axis(2) 411 | # } 412 | # 413 | # 414 | # 415 | -------------------------------------------------------------------------------- /fundamentals/FunctionsFundamentals.R: -------------------------------------------------------------------------------- 1 | ### replace $ and ,. Turn () into negatives 2 | cleanDollars <- function(x){ 3 | x <- gsub("\\(", "-", x) 4 | x <- as.numeric(gsub("\\$|,|\\)", "", x)) 5 | x 6 | } 7 | 8 | 9 | ### compute totals as of a certain date. 10 | fecAsOf <- function(fec, daysOut, dropQ1 = F){ 11 | 12 | known <- fec[fec$daysOut > daysOut, ] 13 | 14 | ### don't have Q1 data yet. 15 | if(dropQ1) print("hi buddy. just fyi, i'm not using any Q1 fec data, because i think you don't have it yet in 2014") 16 | if(dropQ1) known <- subset(known, daysOut > 203) 17 | 18 | #TODO will generate unfair comparisons when one candidate files earlier than the other. be careful around filing deadlines 19 | totals <- aggregate(list(money = known$money), list(fecid = known$cand_id, year = known$cycle), sum, na.rm = T) 20 | totals 21 | } 22 | 23 | 24 | 25 | cleanUpResults <- function(results){ 26 | 27 | ### clean up some things 28 | results$candidate <- str_trim(results$candidate) 29 | results$votesmartid[results$votesmartid=="#N/A"] <- NA 30 | results$fecid[results$fecid=="#N/A"] <- NA 31 | 32 | ### add ranks to results 33 | results <- results[order(results$office),] 34 | results$rank <- unlist(by(-1*results$vote, results$office, rank, ties.method = "average", na.last = "keep")) 35 | 36 | ### simplify party codes 37 | results$party2 <- results$party 38 | demids <- c("D", "DEM", "Democrat ", "D/WF/IDP Combined Parties", "Democratic-Farmer-Labor", "DFL", "D*", "DNL", "Democratic-Nonpartisan League") 39 | repids <- c("R", "REP", "R/CRV Combined Parties", "Republican ", "Independent-Republican") 40 | results$party2[results$party2 %in% demids] <- "Democrat" 41 | results$party2[results$party2 %in% repids] <- "Republican" 42 | results$party2[!results$party2%in%c("Democrat", "Republican")] <- "other" 43 | 44 | ### add rank by party 45 | results <- results[order(results$office, results$party2),] 46 | results$partyrank <- unlist(by(-1*results$vote, paste0(results$office, results$party2), rank, ties.method = "random", na.last = "keep")) 47 | results$party2 <- paste0(results$party2, results$partyrank) 48 | 49 | ### add a total vote 50 | results <- merge(results, aggregate(list(tvote = results$vote), list(office = results$office), sum, na.rm = T), by = "office", all = T) 51 | results$pctvote <- 100*results$vote/results$tvote 52 | results <- results[order(results$vote, decreasing = T), ] 53 | 54 | } 55 | 56 | 57 | 58 | ## latest cook rating 59 | cookAsOf <- function(data, daysOut){ 60 | known <- data[data$days_til_election > daysOut,] 61 | names(known)[1] <- "office" 62 | known <- known[order(known$days_til_election),] 63 | known <- known[!duplicated(known$office), ] 64 | known <- known[,c("office", "cook_rating")] 65 | known 66 | } 67 | 68 | 69 | 70 | thingAsOf <- function(data, daysOut, thing, collapseBy){ 71 | known <- data[data$daysOut >= daysOut,] 72 | known <- plyr:::arrange(known, as.numeric(daysOut)) 73 | weightingSchemes(known, thing, collapseBy) 74 | } 75 | 76 | 77 | 78 | cleanUpPresApproval <- function(data, var1 = "A0", var2 = "B0", signed = T) { 79 | 80 | data$enddate <- as.Date(data$enddate, format = "%Y-%m-%d") 81 | 82 | if(is.null(data$knownDate)) data$knownDate <- NA 83 | 84 | data$knownDate <- as.Date(data$knownDate, format = "%Y-%m-%d") 85 | if(all(is.na(data$knownDate))) data$knownDate <- data$enddate 86 | 87 | data <- subset(data, !is.na(enddate)) 88 | data$cycle <- as.numeric(format(data$enddate, "%Y")) 89 | data$cycle <- data$cycle + data$cycle%%2 ## broken for days past election day 90 | 91 | ## two versions of approval ratings 92 | data$net <- data[,var1] - data[,var2] 93 | data$pctapp <- 100*data[,var1]/(data[,var1] + data[,var2]) 94 | 95 | ## sign the approval ratings 96 | if(signed){ 97 | reps <- which(data$presparty=="R") 98 | data$net[reps] <- -1*data$net[reps] 99 | data$pctapp[reps] <- 100 - data$pctapp[reps] 100 | } 101 | 102 | data$daysOut <- electionDay(data$cycle) - data$knownDate 103 | bad <- data$daysOut < 0 104 | data$cycle[bad] <- data$cycle[bad] + 2 105 | data$daysOut <- electionDay(data$cycle) - data$knownDate 106 | data 107 | } 108 | 109 | 110 | 111 | cleanUpPartyFav <- function(data){ 112 | ## weirdness so we can use same function as other ones 113 | data$enddate <- as.character(format(as.Date(data$EndDate, format = "%m/%d/%Y"), format = "%Y-%m-%d")) 114 | data$A0 <- data$Favorable.dem - data$Unfavorable.dem 115 | data$B0 <- data$Favorable.rep - data$Unfavorable.rep 116 | cleanUpPresApproval(data, signed = F) 117 | } 118 | 119 | 120 | 121 | cleanUpPVI <- function(pvi){ 122 | 123 | pvi2 <- pvi[ ,grep("pvi|postal", names(pvi))] 124 | pvi2 <- reshape(pvi2, direction = "long", varying = list(1:16)) 125 | pvi2$year <- 1952 + 4*(16 - pvi2$time) 126 | names(pvi2)[3] <- "pvi" 127 | pvi2 <- pvi2[,c("postal", "year", "pvi")] 128 | 129 | pvi3 <- pvi2 130 | pvi3$year <- pvi3$year + 2 131 | 132 | x <- rbind(pvi2, pvi3) 133 | rownames(x) <- NULL 134 | na.exclude(x) 135 | } 136 | 137 | 138 | 139 | realraces <- function(reduced, results){ 140 | these <- c("vote.Democrat1", "vote.Republican1") 141 | reduced[,these][is.na(reduced[,these])] <- 0 142 | reduced$demPctMajor <- 100*reduced$vote.Democrat1/(reduced$vote.Democrat1 + reduced$vote.Republican1) 143 | reduced$demMarginAll <- 100*(reduced$vote.Democrat1 - reduced$vote.Republican1)/reduced$tvote 144 | 145 | reduced$realrace <- as.numeric(reduced$demPctMajor > 10 & reduced$demPctMajor < 90) 146 | reduced$mostlyARepAndDem <- 1 147 | reduced$mostlyARepAndDem [reduced$office %in% subset(results, rank < 3 & party2 == "other1")$office ] <- 0 148 | 149 | ## is it really a standard 2-party deal? 150 | reduced$sigThirdParty <- 0 151 | reduced$sigThirdParty [reduced$office %in% subset(results, party2 == "other1" & pctvote > 20)$office ] <- 1 152 | reduced$sigThirdParty10 <- 0 153 | reduced$sigThirdParty10[reduced$office %in% subset(results, party2 == "other1" & pctvote > 10)$office ] <- 1 154 | problematicRecodes <- subset(results, xx%in%c("2012MEKing", "2010FLCrist", "2010AKMurkowski", "2006CTLieberman"))$office 155 | reduced$sigThirdParty10[reduced$office%in%problematicRecodes] <- 1 156 | 157 | reduced$fakeParty <- 0 158 | reduced$fakeParty[reduced$office%in%problematicRecodes] <- 1 159 | 160 | #reduced$mostlyARepAndDem[ (reduced$vote.Democrat1 + reduced$vote.Republican1) / reduced$tvote < .80 ] <- 0 161 | reduced 162 | } 163 | 164 | 165 | 166 | weightingSchemes <- function(known, var, id, lastX = 4){ 167 | known$thing <- known[,var] 168 | 169 | zz <- ddply(known, id, function(X) data.frame( 170 | x1 = mean(X$thing, na.rm = T), ### mean of everything this election cycle 171 | x2 = weighted.mean(X$thing, .9^(as.numeric(X$daysOut)/30), na.rm = T), ## time weighting. crazy slow decay. 172 | x2b = weighted.mean(X$thing, .5^(as.numeric(X$daysOut)/14), na.rm = T), ## half-life of two weeks. 173 | x2c = weighted.mean(X$thing, .5^(as.numeric(X$daysOut)/30), na.rm = T), 174 | x3 = X$thing[!is.na(X$thing)][1], ## 1 most recent 175 | x4 = mean(X$thing[order(X$daysOut) < lastX], na.rm = T) ## 3 most recent 176 | )) 177 | zz 178 | } 179 | 180 | 181 | 182 | addLags <- function(oldreults, reduced){ 183 | oldresults <- oldresults[,c("year", "class", "postal", "offyear", "office", "demPctMajor")] 184 | 185 | for(i in names(reduced)){ 186 | if(!i%in%names(oldresults)){ 187 | oldresults[,i] <- NA 188 | } 189 | } 190 | 191 | oldresults <- oldresults[,names(reduced)] 192 | oldresults <- subset(oldresults, !is.na(postal)) 193 | reduced <- rbind(reduced, oldresults) 194 | 195 | #### make lags. 196 | reduced$lastResultsThisSeat <- NA 197 | reduced$lastResultsThisOffOn <- NA 198 | reduced$lastResultsThisState <- NA 199 | reduced <- reduced[order(reduced$year, decreasing = T),] 200 | 201 | ### terrible; to improve, handle multi races per state. 202 | ### may want to only do competitive raes? 203 | for(i in 1:nrow(reduced)){ 204 | reduced$lastResultsThisSeat[i] <- subset(reduced, postal == reduced$postal[i] & class == reduced$class[i] & year < reduced$year[i] & demPctMajor!=0)$demPctMajor[1] 205 | reduced$lastResultsThisOffOn[i] <- subset(reduced, postal == reduced$postal[i] & offyear == reduced$offyear[i] & year < reduced$year[i] & demPctMajor!=0)$demPctMajor[1] 206 | reduced$lastResultsThisState[i] <- subset(reduced, postal == reduced$postal[i] & year < reduced$year[i] & demPctMajor!=0)$demPctMajor[1] 207 | } 208 | 209 | reduced <- subset(reduced, year > 1990) 210 | reduced 211 | } 212 | 213 | 214 | 215 | prepareTheData <- function(results, reduced, presApproval, genBallot, partyFav, pvi, fec, incApproval, lastsen, cookRatings, oldresults){ 216 | 217 | results <- cleanUpResults(results) 218 | 219 | ############## 220 | ### fec clean up 221 | ############## 222 | 223 | ## moved to fec-updates.r 224 | 225 | ############## 226 | ### bio collapse. 227 | ############## 228 | 229 | temp <- names(results) 230 | bio <- results[ ,grep("city.council", temp):grep("us.senate", temp)] 231 | bio <- data.frame(nothing = 1, bio) 232 | results$experience <- apply(bio, 1, function(x) max(which(x == 1))) - 1 233 | 234 | ############## 235 | ### 2014, all possible combos 236 | ############## 237 | 238 | finishedRaces <- subset(results, year < 2014) 239 | finishedRaces$matchup <- 1 240 | 241 | results2014 <- subset(results, year == 2014) 242 | these <- unique(results2014$office) 243 | 244 | ## order by decreasing money, so lower matchup numbers come first 245 | ## temp <- aggregate(list(money = fec$money), list(fecid = fec$cand_id), sum) 246 | ## results2014 <- results2014[order(temp$money[match(results2014$fecid, temp$fecid)], decreasing = T),] 247 | 248 | combos2014 <- NULL 249 | for(i in these){ 250 | 251 | temp <- subset(results2014, office == i) 252 | dems <- temp[grep("Democrat", temp$party2),]$slug 253 | reps <- temp[grep("Republican", temp$party2),]$slug 254 | 255 | for(j in 1:length(dems)){ 256 | for(k in 1:length(reps)){ 257 | 258 | thisdem <- subset(temp, slug == dems[j]) 259 | if(nrow(thisdem)>0) 260 | thisdem$party2 <- "Democrat1" 261 | 262 | thisrep <- subset(temp, slug == reps[k]) 263 | if(nrow(thisrep)>0) 264 | thisrep$party2 <- "Republican1" 265 | 266 | newmatchup <- rbind(thisdem, thisrep) 267 | newmatchup$matchup <- paste(j, k, sep = "-") 268 | combos2014 <- rbind(combos2014, newmatchup) 269 | } 270 | } 271 | } 272 | 273 | combos2014 <- combos2014[-c(grep("0-|-0", combos2014$matchup)),] 274 | results <- rbind(finishedRaces, combos2014) 275 | 276 | ############## 277 | ### drop to a single row per race, keeping data for only the top Democrat and the top Republican 278 | ############## 279 | reduced <- subset(results, party2 %in% c("Democrat1", "Republican1")) 280 | reduced <- reshape(reduced, direction = "wide", idvar = c("office", "year", "state", "postal", "class", "tvote", "matchup"), timevar = "party2", drop = c("partyrank", "party")) 281 | reduced$serious.Republican1[is.na(reduced$candidate.Republican1) & reduced$year == 2014] <- 1 282 | reduced$serious.Democrat1[is.na(reduced$candidate.Democrat1) & reduced$year == 2014] <- 1 283 | 284 | ### some dummys (whether our race is real and offyear), and lagged results by seat 285 | reduced <- realraces(reduced, results) 286 | reduced$lastparty <- lastsen$lastparty[match(reduced$office, lastsen$nextoffice)] 287 | reduced$offyear <- as.numeric(reduced$year%%4 != 0) 288 | reduced <- addLags(oldresults, reduced) 289 | 290 | reduced$win <- "" 291 | reduced$win[reduced$win.Democrat1 == 1] <- "D" 292 | reduced$win[reduced$win.Republican1 == 1] <- "R" 293 | reduced$win[reduced$win.other1 == 1] <- "O" 294 | 295 | ############## 296 | ### identify states where the senator (sitting or newly elected) usually (since 1992) doesn't match the presidential vote. 297 | ############## 298 | temp <- cbind(postal = pvi$postal, pvi[,grep("diff", names(pvi))] ) 299 | temp2 <- reshape(temp, idvar = "postal", direction = "long", varying = list(2:ncol(temp))) 300 | temp2$year <- as.numeric(gsub("diff", "", names(temp)[-c(1)][temp2$time])) 301 | names(temp2)[3] <- "diff" 302 | temp2$presparty <- ifelse(temp2$diff > 0, "D", "R") 303 | tempsen <- lastsen[,c("Party", "term", "state")] 304 | names(tempsen) <- c("party", "year", "postal") 305 | temp3 <- merge(tempsen, temp2, by = c("year", "postal")) 306 | xx <- strsplit(as.character(temp3$party), split = ";") 307 | temp3$party <- sapply(xx, function(x) paste(unique(x), collapse = ";")) 308 | temp3 <- subset(temp3, temp3$year > 1992) 309 | diffsen <- aggregate(list(diffsen = temp3$party!=temp3$presparty), list(postal = temp3$postal), mean) 310 | diffsen <- diffsen[order(diffsen$diffsen),] 311 | reduced$presSenMostlyOpposite <- 0 312 | reduced$presSenMostlyOpposite [reduced$postal %in% diffsen$postal[diffsen$diffsen > .5]] <- 1 313 | 314 | ############## 315 | ### median senate vote 316 | ############## 317 | medMargin2 <- with(reduced, aggregate(list(x = reduced$demMarginAll), list(postal = reduced$postal), median, na.rm = T)) 318 | reduced$medMargin2 <- medMargin2$x[match(reduced$postal, medMargin2$postal)] 319 | 320 | ############## 321 | ### does sitting president party matter (esp in offyear?) - prob not enough data to actaully investigate this 322 | ############## 323 | oldpres <- data.frame(year = seq(1990, 2014, by = 2), sittingPres = "D", stringsAsFactors = F) 324 | oldpres$sittingPres[oldpres$year %in% c(1990, 1992, 2002, 2004, 2006, 2008)] <- "R" 325 | reduced <- merge(reduced, oldpres, by = "year") 326 | 327 | ############## 328 | ### compute margins, name dates consistently, etc. for generic poll data 329 | ############## 330 | presApproval <- cleanUpPresApproval(presApproval) 331 | genBallot <- cleanUpPresApproval(genBallot, var1 = "Democrats", var2 = "Republicans", signed = F) 332 | partyFav <- cleanUpPartyFav(partyFav) 333 | pvi <- cleanUpPVI(pvi) 334 | 335 | ############## 336 | ### add some dates to things 337 | ############## 338 | incApproval$DATEOUT <- as.Date(incApproval$knownDate, format = "%Y-%m-%d") ### switched from incApproval$DATEOUT to incApproval$knownDate 339 | incApproval$daysOut <- electionDay(as.numeric(sapply(strsplit(incApproval$relevant.office, split = "-"), function(x) x[5]))) - incApproval$DATEOUT 340 | 341 | save(results, reduced, presApproval, genBallot, partyFav, pvi, fec, incApproval, lastsen, cookRatings, oldresults, file = "fundydata.rdata") 342 | 343 | } 344 | 345 | 346 | 347 | prepData <- function(fec2, cookRatings2, incApproval2, presApproval2, genBallot2, partyFav2, reduced){ 348 | 349 | ############## 350 | ### put it all together 351 | ############## 352 | 353 | makeid <- function(data, var, xx = "year") paste(data[,xx], data[,var]) 354 | getMoney <- function(xx) fec2$money[match(makeid(reduced, xx), makeid(fec2, "fecid"))]; 355 | 356 | reduced$money.Democrat1 <- getMoney("fecid.Democrat1") 357 | reduced$money.Republican1 <- getMoney("fecid.Republican1") 358 | reduced$cook <- cookRatings2$cook_rating[ match(reduced$office, cookRatings2$office) ] 359 | reduced$incApproval <- incApproval2$x2[ match(reduced$office, incApproval2$relevant.office) ] 360 | reduced$presApproval <- presApproval2$x2b[ match(reduced$year, presApproval2$cycle) ] 361 | reduced$genBallot <- genBallot2$x2c[ match(reduced$year, genBallot2$cycle) ] 362 | reduced$partyFav <- partyFav2$x2b[ match(reduced$year, partyFav2$cycle) ] 363 | reduced$pvi <- pvi$pvi [ match(makeid(reduced, "postal"), makeid(pvi, "postal")) ] 364 | 365 | reduced$incumbent <- "open" 366 | reduced$incumbent[ which(reduced$incumbent.Democrat1 == 1) ] <- "D" 367 | reduced$incumbent[ which(reduced$incumbent.Democrat1 == "a") ] <- "Da" 368 | reduced$incumbent[ which(reduced$incumbent.Republican1 == 1) ] <- "R" 369 | reduced$incumbent[ which(reduced$incumbent.Republican1 == "a") ] <- "Ra" 370 | reduced$open <- reduced$incumbent == "open" 371 | reduced$open2 <- !reduced$incumbent %in% c("D", "R") 372 | 373 | ### calculate senate percents on a rolling basis 374 | lastsen2 <- NULL 375 | for(year in seq(1992, 2014, by = 2)){ 376 | temp <- subset(lastsen, term < year & term >= year - 40) 377 | demsplits = ddply(temp, "state", function(X) { 378 | gg <- strsplit(as.character(X$Party), split = ";") 379 | l <- sapply(gg, length) 380 | weight <- 1/rep(l, times = l) 381 | party = unlist(gg) 382 | weighted.mean(party !="R" , weight)} ) 383 | demsplits$year <- year 384 | lastsen2 <- rbind(lastsen2, demsplits) 385 | } 386 | names(lastsen2)[2] <- "pctDemLast20" 387 | reduced$pctDemLast20 <- lastsen2$pctDemLast20[ match( paste(reduced$year, reduced$postal), paste(lastsen2$year, lastsen2$state) )] 388 | 389 | ## bad filling in missing ratings 390 | reduced$temp <- ifelse(reduced$incumbent %in% c("D", "Da"), reduced$demPctMajor, 100 - reduced$demPctMajor) 391 | missingratings <- which(is.na(reduced$incApproval) & reduced$incumbent != "open") 392 | temp <- reduced[missingratings,] 393 | reduced$incApproval[missingratings] <- predict( lm(incApproval ~ pvi + temp, data = subset(reduced, realrace == 1)), newdata = temp ) 394 | reduced$incApproval[is.na(reduced$incApproval)] <- 0 395 | 396 | reduced$cook <- reduced$cook - 4 397 | reduced$cook[is.na(reduced$cook)] <- 0 398 | reduced$cook <- as.factor(reduced$cook) 399 | reduced$cook <- relevel(reduced$cook, ref = "0") 400 | 401 | fixme <- c("money.Democrat1", "money.Republican1", "experience.Democrat1", "experience.Republican1") 402 | reduced[,fixme][is.na(reduced[,fixme])] <- 0 403 | 404 | reduced$demMoney <- 100 * with(reduced, money.Democrat1 / (money.Democrat1 + money.Republican1) ) 405 | reduced$demMoney[is.na(reduced$demMoney)] <- 50 ## no one has reported money. 406 | reduced$demMoney <- reduced$demMoney - 50 407 | 408 | reduced$netExp <- reduced$experience.Democrat1 - reduced$experience.Republican1 409 | reduced$netExp2 <- floor(reduced$experience.Democrat1/3) - floor(reduced$experience.Republican1/3) 410 | 411 | reduced$incApproval2 <- reduced$incApproval 412 | reps <- reduced$incumbent%in%c("R", "Ra") 413 | reduced$incApproval2[reps] <- -1 * reduced$incApproval2[reps] 414 | 415 | reduced$incumbentSimple <- substr(reduced$incumbent, 1, 1) 416 | reduced$incumbentSimple <- relevel(as.factor(reduced$incumbentSimple), ref = 3) 417 | 418 | reduced$notARace <- with(reduced, as.character(cook)%in%c("-3", "3") | is.na(candidate.Democrat1) | is.na(candidate.Republican1)) 419 | 420 | reduced$norep <- is.na(reduced$candidate.Republican1) 421 | reduced$nodem <- is.na(reduced$candidate.Democrat1) 422 | reduced 423 | } 424 | -------------------------------------------------------------------------------- /fundamentals/data/pvi/pvi.csv: -------------------------------------------------------------------------------- 1 | "state","dem2012","rep2012","dem2008","rep2008","dem2004","rep2004","dem2000","rep2000","dem1996","rep1996","dem1992","rep1992","dem1988","rep1988","dem1984","rep1984","dem1980","rep1980","dem1976","rep1976","dem1972","rep1972","dem1968","rep1968","dem1964","rep1964","dem1960","rep1960","X.3","dem1956","rep1956","dem1952","rep1952","diff2012","diff2008","diff2004","diff2000","diff1996","diff1992","diff1988","diff1984","diff1980","diff1976","diff1972","diff1968","diff1964","diff1960","diff1956","diff1952","pvi2012","pvi2008","pvi2004","pvi2000","pvi1996","pvi1992","pvi1988","pvi1984","pvi1980","pvi1976","pvi1972","pvi1968","pvi1964","pvi1960","pvi1956","pvi1952","postal" 2 | "Alabama",38.36,60.55,38.74,60.32,36.84,62.46,41.59,56.47,43.16,50.12,40.88,47.65,39.86,59.17,38.28,60.54,47.45,48.75,55.73,42.61,25.54,72.43,18.72,13.99,0,69.45,56.39,42.16,"0.00%",56.54,39.4,64.55,35.02,-22.19,-21.58,-25.62,-14.88,-6.96,-6.77,-19.31,-22.26,-1.3,13.12,-46.89,4.73,-69.45,14.23,17.14,29.53,-13.1814448977675,-14.5838680899121,-11.6578778723794,-7.8521265803338,-8.4611090399946,-7.27869561946964,-5.85136845376317,-2.09648726614033,4.63164777680907,5.6207851559123,-12.1424256536419,7.63652013100044,-61.3444533762058,7.13406037590392,16.6838238901662,20.2804770794791,"AL" 3 | "Alaska",40.81,54.8,37.89,59.42,35.52,61.07,27.67,58.62,33.27,50.8,30.29,39.46,36.27,59.59,29.87,66.65,26.41,54.35,35.65,57.9,34.62,58.13,42.65,45.28,65.91,34.09,49.06,50.94,"0.00%",NA,NA,NA,NA,-13.99,-21.53,-25.55,-30.95,-17.53,-9.17,-23.32,-36.78,-27.94,-22.25,-23.51,-2.63,31.82,-1.88,NA,NA,-9.2803569899964,-14.7540631345121,-11.9835825905812,-18.1986469666618,-15.1562413941689,-10.0286096878433,-8.2653694922951,-9.88663102046037,-11.990843957124,-12.9419853758014,-0.885484959711486,-1.08919248961441,4.56554662379421,-1.0256250629596,NA,NA,"AK" 4 | "Arizona",44.45,53.48,44.91,53.39,44.37,54.83,44.67,50.95,46.52,44.29,36.52,38.47,38.74,59.95,32.54,66.42,28.24,60.61,39.8,56.37,30.38,61.64,35.02,54.78,49.45,50.45,44.36,55.52,"0.00%",38.9,60.99,41.65,58.35,-9.02999999999999,-8.48,-10.46,-6.28,2.23,-1.95,-21.21,-33.88,-32.37,-16.57,-31.26,-19.76,-1,-11.16,-22.09,-16.7,-6.57461269986253,-8.00480618984191,-4.02975317693059,-3.54876689927935,-3.5025674371947,-4.75530634178185,-6.84756719816099,-7.9516125054844,-12.9087710888771,-9.66490171954883,-5.19706846054425,-10.5959118708189,-11.8449538767063,-5.67232910781343,-3.30600523874027,-2.89828660436138,"AZ" 5 | "Arkansas",36.88,60.57,38.86,58.72,44.55,54.31,45.86,51.31,53.74,36.8,53.21,35.48,42.19,56.37,38.29,60.47,47.52,48.13,64.94,34.93,30.71,68.82,30.33,31.01,56.06,43.41,50.19,43.06,"0.00%",52.46,45.82,55.9,43.76,-23.69,-19.86,-9.76000000000001,-5.45,16.94,17.73,-14.18,-22.18,-0.609999999999999,30.01,-38.11,-0.680000000000003,12.65,7.13,6.64,12.14,-14.1191279311848,-13.8677452666711,-3.69384927568218,-3.06929855160791,4.62457544277907,6.54035692333633,-3.29538527898077,-2.06282762797498,4.98845256905557,13.9745828598585,-7.35661192491154,-0.147972276763914,-4.98575226029144,3.73743123730849,11.1292610185302,11.5424218042278,"AR" 6 | "California",60.24,37.12,60.94,36.91,54.31,44.36,53.45,41.65,51.1,38.21,46.01,32.61,47.56,51.13,41.27,57.51,35.91,52.69,47.57,49.35,41.54,55,44.74,47.82,59.11,40.79,49.55,50.1,"0.00%",44.27,55.39,42.27,56.83,23.12,24.03,9.95,11.8,12.89,13.4,-3.57,-16.24,-16.78,-1.78,-13.46,-3.08,18.32,-0.550000000000004,-11.12,-14.56,9.90928265208214,8.58751882857283,6.28448363230972,5.93906072896609,2.48603134811144,5.06687159365321,2.08950849339845,0.946127472775843,-4.16220250688321,-1.96823215169946,4.81716584157084,-1.25747035187637,-2.17528420703662,-0.361590943541645,2.17218914755105,-1.89440163967923,"CA" 7 | "Colorado",51.49,46.13,53.66,44.71,47.02,51.69,42.39,50.75,44.43,45.8,40.13,35.87,45.28,53.06,35.12,63.44,31.07,55.07,42.58,54.05,34.59,62.61,41.32,50.46,61.27,38.19,44.91,54.63,"0.00%",39.81,59.49,38.96,60.27,5.36,8.95,-4.66999999999999,-8.36,-1.37,4.26000000000001,-7.78,-28.32,-24,-11.47,-28.02,-9.14,23.08,-9.72000000000001,-19.68,-21.31,0.781162395732871,0.857671525504189,-1.1230909029511,-4.75280279093893,-5.48957678841491,-0.652501406386952,-0.0574616394212191,-5.20046813651465,-8.62348685631489,-6.98495886291573,-2.62521075918571,-4.5729830214012,0.258200957194576,-4.96808437579866,-2.15820791848557,-5.2859667414167,"CO" 8 | "Connecticut",58.06,40.72,60.59,38.22,54.31,43.95,55.91,38.44,52.83,34.69,42.21,35.78,46.87,51.98,38.83,60.73,38.52,48.16,46.9,52.06,40.13,58.57,49.48,44.32,67.81,32.09,53.73,46.27,"0.00%",36.26,63.72,43.91,55.7,17.34,22.37,10.36,17.47,18.14,6.43,-5.11,-21.9,-9.63999999999999,-5.16,-18.44,5.16,35.72,7.45999999999999,-27.46,-11.79,6.812903706514,7.62822484488332,6.51415231081422,8.99314654608771,5.63293974003433,0.667190389457317,1.31347805356428,-1.83197794851864,-0.253359519365742,-3.65706301704908,2.44693078458704,3.15684834971384,6.53342450167209,3.6443749370404,-5.98158890888304,-0.466367118365996,"CT" 9 | "Delaware",58.61,39.98,61.91,36.93,53.35,45.75,54.96,41.9,51.82,36.58,43.52,35.33,43.48,55.88,39.93,59.78,44.87,47.21,51.98,46.57,39.18,59.6,41.61,45.12,60.95,38.78,50.63,49,"0.00%",44.62,55.09,47.88,51.75,18.63,24.98,7.6,13.06,15.24,8.19,-12.4,-19.85,-2.34,5.41,-20.42,-3.51,22.17,1.63,-10.47,-3.87,7.48404322646857,8.94510474032544,5.07693483778247,6.4767539707866,3.88950372127299,1.73827221441204,-2.34173320440478,-0.787451231646374,4.03668922138135,1.69485056251426,1.45226906254058,-1.61720597224261,-0.229442847779038,0.732401635825908,2.50093198602906,3.50952730711108,"DE" 10 | "District of Columbia",90.91,7.28,92.46,6.53,89.18,9.34,85.16,8.95,85.19,9.34,84.64,9.1,82.65,14.3,85.38,13.73,74.89,13.41,81.63,16.51,78.1,21.56,81.82,18.18,85.5,14.5,NA,NA,NA,NA,NA,NA,NA,83.63,85.93,79.84,76.21,75.85,75.54,68.35,71.65,61.48,65.12,56.54,63.64,71,NA,NA,NA,40.6216263608024,39.7118944397211,41.7621156756358,40.2249172355643,35.3891329897712,36.8371648597692,39.1483313157963,45.3131206609254,40.1204604853273,32.1271449158216,40.1548154038427,32.2263153006733,24.1555466237942,NA,NA,NA,"DC" 11 | "Florida",50.01,49.13,50.91,48.1,47.09,52.1,48.84,48.85,48.02,42.32,39,40.89,38.51,60.87,34.66,65.32,38.5,55.52,51.93,46.64,27.8,71.91,30.93,40.53,51.15,48.85,48.49,51.51,"0.00%",42.73,57.27,44.97,54.99,0.879999999999995,2.81,-5.01,-0.00999999999999801,5.7,-1.89,-22.36,-30.66,-17.02,5.29,-44.11,-9.6,2.3,-3.02,-14.54,-10.02,-1.5203598494375,-2.27243105751714,-1.28303195275672,-0.27005329607438,-1.57565705395822,-4.63800944046012,-7.35154605697315,-6.1666516329542,-3.74394223566672,1.63342319118543,-10.330776034286,-6.3107291997465,-10.1944533762058,-1.5956250629596,0.481157640426816,0.439708593717852,"FL" 12 | "Georgia",45.48,53.3,46.9,52.1,41.37,57.97,42.98,54.67,45.84,47.01,43.47,42.88,39.5,59.75,39.79,60.17,55.76,40.95,66.74,32.96,24.65,75.04,26.75,30.4,45.87,54.12,62.54,37.43,"0.00%",66.48,32.65,69.66,30.34,-7.82,-5.2,-16.6,-11.69,-1.16999999999999,0.589999999999996,-20.25,-20.38,14.81,33.78,-50.39,-3.65,-8.25,25.11,33.83,39.32,-5.92246782618493,-6.31774226473119,-7.11271970764623,-6.25059814737235,-5.360454246256,-3.11350009593073,-6.3033089516557,-1.02766265068395,12.9642358710557,15.8908734358018,-13.4849778891404,-2.78703553047278,-15.4698659174599,12.4731425673295,24.8146096731112,25.1117133956386,"GA" 13 | "Hawaii",70.55,27.84,71.85,26.58,54.01,45.26,55.79,37.46,56.93,31.64,48.09,36.7,54.27,44.75,43.82,55.1,44.8,42.9,50.59,48.06,37.52,62.48,59.83,38.7,78.76,21.24,50.03,49.97,"0.00%",NA,NA,NA,NA,42.71,45.27,8.75,18.33,25.29,11.39,9.52,-11.28,1.9,2.53,-24.96,21.13,57.52,0.0600000000000023,NA,NA,19.7402648341535,19.3045581548871,5.64959660063921,9.56348316562793,9.54643739390048,3.26146095263007,8.70531205817007,3.46483794842347,6.39056176491348,0.232362169616018,-0.691630512272129,11.1289378521805,17.4155466237942,-0.0556250629596078,NA,NA,"HI" 14 | "Idaho",32.62,64.52,35.91,61.21,30.26,68.38,27.64,67.17,33.65,52.18,28.42,42.03,36.01,62.08,26.39,72.36,25.19,66.46,37.12,59.88,26.04,64.24,30.66,56.79,50.92,49.08,46.22,53.78,"0.00%",38.78,61.17,34.42,65.42,-31.9,-25.3,-38.12,-39.53,-18.53,-13.61,-26.07,-45.97,-41.27,-22.76,-38.2,-26.13,1.84,-7.56,-22.39,-31,-18.3837772506174,-16.7166031969529,-18.0803657008037,-21.1118921369739,-15.5249997458036,-13.1144658455188,-9.39061400975147,-14.1095343867201,-17.2076792752839,-12.781907794487,-9.36803281621542,-14.5336503940094,-10.4244533762058,-3.8656250629596,-3.44944265972326,-10.0731263479511,"ID" 15 | "Illinois",57.6,40.73,61.85,36.74,54.82,44.48,54.6,42.58,54.32,36.81,48.58,34.34,48.6,50.69,43.3,56.17,41.72,49.65,48.13,50.1,40.51,59.03,44.15,47.08,59.47,40.53,49.98,49.8,"0.00%",40.29,59.52,44.94,54.84,16.87,25.11,10.34,12.02,17.51,14.24,-2.09,-12.87,-7.93,-1.97,-18.52,-2.93,18.94,0.18,-19.23,-9.90000000000001,6.61408021593421,9.04307761885976,6.44886935823492,5.91946501738639,4.87674883329784,5.13145649850553,2.8457298282154,2.69712775809039,0.967824711103538,-2.05269768272532,2.48557664063123,-1.19951611443135,-1.87445337620579,0.00457337360082466,-1.88214563579801,0.490799384814811,"IL" 16 | "Indiana",43.93,54.13,49.87,48.84,39.26,59.94,41.01,56.65,41.55,47.13,36.79,42.91,39.69,59.84,37.68,61.67,37.65,56.01,45.7,53.32,33.34,66.11,37.99,50.29,55.98,43.56,44.6,55.03,"0.00%",39.7,59.9,40.99,58.11,-10.2,1.02999999999999,-20.68,-15.64,-5.58000000000001,-6.12,-20.15,-23.99,-18.36,-7.62,-32.77,-12.3,12.42,-10.43,-20.2,-17.12,-7.16507408387899,-3.16974931732583,-9.18096285434995,-8.27230758183042,-7.87654921806658,-7.29453072686506,-6.22437372434932,-2.90706262406031,-4.49408590049412,-4.89765656543196,-4.68724639965272,-6.56015502103034,-5.10575536535588,-5.319992221446,-2.38940460856916,-3.18602626127358,"IN" 17 | "Iowa",51.99,46.18,53.93,44.39,49.23,49.9,48.54,48.22,50.26,39.92,43.29,37.27,54.71,44.5,45.89,53.27,38.6,51.31,48.46,49.47,40.48,57.61,40.82,53.01,61.88,37.92,43.22,56.71,"0.00%",40.65,59.06,35.59,63.75,5.81,9.54,-0.670000000000002,0.32,10.34,6.02,10.21,-7.38,-12.71,-1.01,-17.13,-12.19,23.96,-13.49,-18.41,-28.16,0.994975816447707,1.16002565038417,0.904484163739683,-0.0995774791558213,1.00257270648005,0.281212595599145,9.04385302341334,5.44515640836363,-1.76085583791677,-1.56562349295035,3.05659254818255,-6.08947495830568,0.659554639826276,-6.83534987032476,-1.48061449877688,-8.72183200399898,"IA" 18 | "Kansas",37.99,59.71,41.57,56.5,36.62,62,37.24,58.04,36.08,54.29,33.74,38.88,42.56,55.79,32.6,66.27,33.29,57.85,44.94,52.49,29.5,67.66,34.72,54.84,54.09,45.06,39.1,60.45,"0.00%",34.21,65.44,30.5,68.77,-21.72,-14.93,-25.38,-20.8,-18.21,-5.14,-13.23,-33.67,-24.56,-7.55,-38.16,-20.12,9.03,-21.35,-31.23,-38.27,-13.0798368583673,-11.3033894987724,-11.6251482580828,-11.1801323781173,-14.8056519910149,-6.99410296605588,-2.82777626432993,-7.86099474957995,-8.16645315493242,-4.92452565071157,-7.84934150444998,-10.8263778659189,-6.79074687091078,-10.8088797088662,-7.91868681516776,-13.8239993070913,"KS" 19 | "Kentucky",37.8,60.49,41.15,57.37,39.69,59.55,41.37,56.5,45.84,44.88,44.55,41.34,43.88,55.52,39.37,60.04,47.61,49.07,52.75,45.57,34.77,63.37,37.65,43.79,64.01,35.65,46.41,53.59,"0.00%",45.21,54.3,49.91,49.84,-22.69,-16.22,-19.86,-15.13,0.960000000000001,3.20999999999999,-11.64,-20.67,-1.46,7.18,-28.6,-6.14,28.36,-7.18000000000001,-9.09,0.0699999999999932,-13.5065512798883,-11.9233107387528,-8.76362170678979,-7.99457642592414,-4.20130525188891,-1.58646375725189,-1.95692840135135,-1.22992341617111,4.55225518603873,2.6013935233223,-2.78265150269398,-3.36333106474907,2.88392310382632,-3.67562506295961,3.18377747762912,5.48680111493687,"KY" 20 | "Louisiana",40.58,57.78,39.93,58.56,42.22,56.72,44.88,52.55,52.01,39.94,45.58,40.97,44.06,54.27,38.18,60.77,45.75,51.2,51.73,45.95,28.35,65.32,28.21,23.47,43.19,56.81,50.42,28.59,"20.99%",39.51,53.28,52.92,47.08,-17.2,-18.63,-14.5,-7.66999999999999,12.07,4.61,-10.21,-22.59,-5.45,5.77999999999999,-36.97,4.74,-13.62,21.83,-13.77,5.84,-10.7075682967407,-13.1492926144052,-6.08524909495195,-4.20109435878707,1.8329438655576,-0.791932523173722,-1.29349903025035,-2.24844100750419,2.49659627352651,1.90869142704004,-7.94580367336959,4.99222861336683,-18.1544533762058,13.7290819361544,0.331177039068914,8.37171339563863,"LA" 21 | "Maine",56.27,40.98,57.71,40.38,53.57,44.58,49.09,43.97,51.62,30.76,38.77,30.39,43.88,55.34,38.78,60.83,42.25,45.61,48.07,48.91,38.48,61.46,55.3,43.07,68.84,31.16,42.95,57.05,"0.00%",29.13,70.87,33.77,66.05,15.29,17.33,8.99,5.12,20.86,8.38,-11.46,-22.05,-3.36,-0.839999999999996,-22.98,12.23,37.68,-14.1,-41.74,-32.28,5.89700584515035,5.14224449243162,5.82214915327497,2.48597832427619,7.93043422872165,2.60328228360728,-1.87684297040242,-1.90175086643406,3.39519051371852,-1.4830280169582,0.291471348844535,6.62264141635899,7.49554662379421,-7.1356250629596,-13.1188423595732,-10.7173909922596,"ME" 22 | "Maryland",61.97,35.9,61.92,36.47,55.91,42.93,56.57,40.18,54.25,38.27,49.8,35.62,48.2,51.11,47.02,52.51,47.14,44.18,53.04,46.96,37.36,61.26,43.59,41.94,65.47,34.53,53.61,46.39,"0.00%",39.96,60.04,43.83,55.36,26.07,25.45,12.98,16.39,15.98,14.18,-2.91,-5.48999999999999,2.96,6.08,-23.9,1.65000000000001,30.94,7.22,-20.08,-11.53,11.3545113814541,9.24174528276327,7.80859178592889,8.20534917279103,3.90556481996169,4.84503091070877,2.43309312950532,6.40845255697854,6.9279980035141,1.99005096839959,-0.328848115192437,1.37088913441588,4.12554662379421,3.5243749370404,-2.28884235957318,-0.36036443478783,"MD" 23 | "Massachusetts",60.65,37.51,61.8,35.99,61.94,36.78,59.8,32.5,61.47,28.09,47.54,29.03,53.23,45.38,48.43,51.22,41.75,41.9,56.11,40.44,54.2,45.23,63.01,32.89,76.19,23.44,60.22,39.55,"0.00%",40.37,59.32,45.46,54.22,23.14,25.81,25.16,27.3,33.38,18.51,7.84999999999999,-2.79,-0.149999999999999,15.67,8.97000000000001,30.12,52.75,20.67,-18.95,-8.76,9.82270189147732,9.50516623534267,13.9855360738667,14.5237973294311,13.9051458045398,8.6318462493529,7.87852892224747,7.76651533159777,5.21766415780452,7.06501730708422,16.29908054073,16.1101734862833,15.1284965384785,10.2732002352262,-1.75330619747067,1.05765240045403,"MA" 24 | "Michigan",54.21,44.71,57.33,40.89,51.23,47.81,51.28,46.14,51.69,38.48,43.77,36.38,45.67,53.57,40.24,59.23,42.5,48.99,46.44,51.83,41.81,56.2,48.18,41.46,66.7,33.1,50.85,48.84,"0.02%",44.15,55.63,43.97,55.44,9.5,16.44,3.41999999999999,5.14,13.21,7.39,-7.9,-18.99,-6.49,-5.39,-14.39,6.72,33.6,2.01,-11.48,-11.47,2.83768341483092,4.67748798523333,2.9689993635874,2.37312693465433,2.59464689728494,1.15497306581976,-0.0820475158772871,-0.379176655300573,1.76048773273395,-3.79239331775081,4.44728184360992,4.15464194056623,5.48921395846355,0.92250012512346,1.99850179757254,-0.317323924550489,"MI" 25 | "Minnesota",52.65,44.96,54.06,43.82,51.09,47.61,47.91,45.5,51.1,34.96,43.48,31.85,52.91,45.9,49.72,49.54,46.5,42.56,54.9,42.02,46.07,51.58,54,41.46,63.76,36,50.58,49.16,"0.00%",46.08,53.68,44.11,55.33,7.69,10.24,3.48,2.41,16.14,11.63,7.01,0.18,3.94,12.88,-5.51,12.54,27.76,1.42,-7.6,-11.22,1.97496890521652,1.5394153349683,3.00534217555494,1.02507671110604,4.6467729315367,4.26423512829902,7.44541420402282,9.25708594551053,7.51931536804728,5.59470635428486,8.96706892449182,6.97451140375314,2.56893876493295,0.626225749151887,3.94201569976925,-0.189879524715358,"MN" 26 | "Mississippi",43.79,55.29,43,56.18,39.75,59.44,40.7,57.62,44.08,49.21,40.77,49.68,39.07,59.89,37.46,61.85,48.09,49.42,49.56,47.68,19.63,78.2,23.02,13.52,12.86,87.14,36.34,24.67,"38.99%",58.23,24.46,60.44,39.56,-11.5,-13.18,-19.69,-16.92,-5.13,-8.91,-20.82,-24.39,-1.33,1.88,-58.57,9.5,-74.28,11.67,33.77,20.88,-7.76756787316095,-10.3359644136248,-8.68297146278798,-8.86949161497574,-7.47989661601999,-8.38050611966267,-6.62119939513947,-3.1133151575834,4.62534211723708,-0.0832686531553217,-18.1462109068341,13.4057679552984,-48.4844533762058,9.47838083771242,28.1707972582766,15.8917133956386,"MS" 27 | "Missouri",44.38,53.76,49.24,49.37,46.1,53.3,47.08,50.42,47.54,41.24,44.07,33.92,47.85,51.83,39.98,60.02,44.35,51.16,51.1,47.47,37.71,62.29,43.74,44.87,64.05,35.95,50.26,49.74,"0.00%",50.11,49.89,49.14,50.71,-9.38,-0.129999999999995,-7.2,-3.34,6.3,10.15,-3.98,-20.04,-6.81,3.63,-24.58,-1.13,28.1,0.519999999999996,0.219999999999999,-1.57,-6.7430639779815,-3.75739587414446,-2.37930613986952,-1.97775557775558,-1.18230936287725,3.05211153319369,1.90181394033924,-0.853585019631531,1.74225173224603,0.791382002182694,-0.501630512272133,-0.231310249490307,2.70554662379421,0.174374937040389,7.86115764042682,4.66553412673527,"MO" 28 | "Montana",41.7,55.35,47.11,49.49,38.56,59.07,33.36,58.44,41.23,44.11,37.63,35.12,46.2,52.07,38.18,60.47,32.43,56.82,45.4,52.84,37.85,57.93,41.59,50.6,58.95,40.57,48.6,51.1,"0.00%",42.87,57.13,40.07,59.39,-13.65,-2.38,-20.51,-25.08,-2.88,2.51000000000001,-5.87,-22.29,-24.39,-7.44,-20.08,-9.01,18.38,-2.5,-14.26,-19.32,-8.99663417026587,-4.92336369643958,-9.26151921757781,-13.9250657838893,-6.41777395535082,-1.7300470746814,0.911533003078069,-2.13110149200862,-8.35654209373375,-4.83659398274048,1.30601408994128,-4.48033184109909,-2.11012861736335,-1.33938634681116,0.621157640426817,-4.26073381932217,"MT" 29 | "Nebraska",38.03,59.8,41.6,56.53,32.68,65.9,33.25,62.25,34.95,53.65,29.4,46.58,39.2,60.15,28.81,70.55,26.04,65.53,38.46,59.19,29.5,70.5,31.81,59.82,52.61,47.39,37.93,62.07,"0.00%",34.49,65.51,30.85,69.15,-21.77,-14.93,-33.22,-29,-18.7,-17.18,-20.95,-41.74,-39.49,-20.73,-41,-28.01,5.22,-24.14,-31.02,-38.3,-13.0906205052655,-11.2987353197077,-15.6068352422583,-15.4481811382335,-15.2834531850526,-14.7607397239497,-6.64533058091084,-11.8380133610164,-16.2554154357974,-11.6643883557171,-8.71163051227213,-14.877980235723,-8.73445337620579,-12.1556250629596,-7.75884235957318,-13.6982866043614,"NE" 30 | "Nevada",52.36,45.68,55.15,42.65,47.88,50.47,45.98,49.52,43.93,42.91,37.36,34.73,37.92,58.86,31.97,65.85,26.89,62.54,45.81,50.17,36.32,63.68,39.29,47.46,58.58,41.42,51.16,48.84,"0.00%",42.03,57.97,38.55,61.45,6.68,12.5,-2.59,-3.54000000000001,1.02,2.63,-20.94,-33.88,-35.65,-4.36,-27.36,-8.17,17.16,2.31999999999999,-15.94,-22.9,1.44259607168817,2.6991134085662,-0.0743017362234422,-2.11833820629633,-4.14311881645697,-1.63102423238661,-6.92014851558916,-8.1511069987769,-14.6244667745084,-3.32125555379253,-1.89163051227213,-4.30261841690592,-2.76445337620579,1.07437493704039,-0.218842359573179,-5.99828660436138,"NV" 31 | "New Hampshire",51.98,46.4,54.13,44.52,50.24,48.87,46.8,48.07,49.32,39.37,38.91,37.69,36.33,62.49,30.95,68.66,28.35,57.74,43.47,54.75,34.86,63.98,43.93,52.1,63.64,36.36,46.58,53.42,"0.00%",33.84,66.11,39.08,60.92,5.58,9.61,1.37,-1.27,9.95,1.22,-26.16,-37.71,-29.39,-11.28,-29.12,-8.17,27.28,-6.84,-32.27,-21.84,0.871765590558082,1.17927555666575,1.93357548851444,-0.934272052391594,0.8790203098889,-2.65878833781473,-9.33798462332191,-9.76240742702035,-11.7620225807363,-6.79216039384842,-2.94250869924097,-3.84756369547371,2.29554662379421,-3.50562506295961,-8.39191389534106,-5.46828660436138,"NH" 32 | "New Jersey",58.25,40.5,57.14,41.61,52.92,46.24,56.13,40.29,53.72,35.86,42.95,40.58,42.6,56.24,39.2,60.09,38.56,51.97,47.92,50.08,36.77,61.57,43.97,46.1,65.61,33.86,49.96,49.16,"0.00%",34.23,64.68,41.99,56.81,17.75,15.53,6.68,15.84,17.86,2.37,-13.64,-20.89,-13.41,-2.16,-24.8,-2.13,31.75,0.800000000000004,-30.45,-14.82,7.02316509802204,4.17181150077194,4.61071790922538,7.94912840737358,5.23833724200676,-2.03648100401024,-3.00183808608867,-1.35327481719422,-2.09906117139684,-2.15198984792693,-0.820945135009571,-0.776098377576923,4.61513242855947,0.317926188049267,-7.64162367592138,-2.04828660436138,"NJ" 33 | "New Mexico",52.99,42.84,56.91,41.78,49.05,49.84,47.91,47.85,49.18,41.86,45.9,37.34,46.9,51.86,39.23,59.7,36.78,54.97,48.28,50.75,36.56,61.05,39.75,51.85,59.22,40.24,50.15,49.41,"0.00%",41.78,57.81,44.28,55.39,10.15,15.13,-0.790000000000006,0.0599999999999952,7.32,8.56,-4.96,-20.47,-18.19,-2.47,-24.49,-12.1,18.98,0.740000000000002,-16.03,-11.11,3.33165970278759,3.97393732373632,0.842990528196308,-0.233606744133064,-0.710194884680126,1.68662578448787,1.38706427076071,-1.17928399870765,-4.60548308702479,-2.29704587094202,-0.756451739605396,-6.19848819277645,-1.80292914535922,0.286010131897763,-0.296839146399169,-0.12167879860237,"NM" 34 | "New York",63.33,35.19,62.88,36.03,58.37,40.08,60.21,35.23,59.47,30.61,49.73,33.88,51.62,47.52,45.83,53.84,43.99,46.66,51.95,47.52,41.21,58.54,49.76,44.3,68.56,31.31,52.53,47.27,"0.00%",38.78,61.19,43.55,55.45,28.14,26.85,18.29,24.98,28.86,15.85,4.09999999999999,-8.01000000000001,-2.66999999999999,4.43,-17.33,5.46,37.25,5.26,-22.41,-11.9,12.3171875158823,9.88146546313896,10.5314034196716,12.8218210121814,11.2886883575541,6.02339829082882,5.96598531658263,5.14815472151425,3.83462626550184,1.17685301926921,3.10165269574792,3.30871802234035,7.3047906410166,2.54964547812255,-3.4572048683258,-0.558387614462391,"NY" 35 | "North Carolina",48.35,50.39,49.7,49.38,43.58,56.02,43.2,56.03,44.04,48.73,42.65,43.44,41.71,57.97,37.89,61.9,47.18,49.3,55.27,44.22,28.89,69.46,29.24,39.51,56.15,43.85,52.11,47.89,"0.00%",50.66,49.34,53.91,46.09,-2.04,0.32,-12.44,-12.83,-4.69,-0.789999999999999,-16.26,-24.01,-2.12,11.05,-40.57,-10.27,12.3,4.22,1.31999999999999,7.81999999999999,-2.99719267575028,-3.52999397032162,-5.00255567725447,-6.72971386166992,-7.25816259892628,-3.91395514818715,-4.25789713510217,-2.8638485730938,4.20865015231891,4.50337291030331,-8.83694825502759,-7.06277560841759,-5.19445337620579,2.0243749370404,8.41115764042681,9.36171339563862,"NC" 36 | "North Dakota",38.7,58.32,44.5,53.15,35.5,62.86,33.05,60.66,40.13,46.94,32.18,44.22,42.97,56.03,33.8,64.84,26.26,64.23,45.8,51.66,35.79,62.07,38.23,55.94,57.97,41.88,44.52,55.42,"0.00%",38.09,61.72,28.39,70.97,-19.62,-8.65,-27.36,-27.61,-6.81,-12.04,-13.06,-31.04,-37.97,-5.86,-26.28,-17.71,16.09,-10.9,-23.63,-42.58,-12.0754939283043,-8.12056309981009,-12.6656684781939,-14.9965538889667,-8.64105238716837,-11.3347141371668,-2.6977572126027,-6.56756717697136,-15.6728953562234,-4.05631061584009,-1.63897569927397,-8.99689166545176,-3.28736774776313,-5.53889702613751,-4.08633359291653,-15.9754202597559,"ND" 37 | "Ohio",50.58,47.6,51.38,46.8,48.71,50.81,46.46,49.97,47.38,41.02,40.18,38.35,44.15,55,40.14,58.9,40.91,51.51,48.92,48.65,38.07,59.63,42.95,45.23,62.94,37.06,46.72,53.28,"0.00%",38.89,61.11,43.24,56.76,2.98,4.58000000000001,-2.1,-3.51,6.36,1.83,-10.85,-18.76,-10.6,0.270000000000003,-21.56,-2.27999999999999,25.88,-6.56,-22.22,-13.52,-0.446555977450291,-1.35902903753151,0.187359933742565,-2.08490810237155,-1.13312071311614,-2.28997317379733,-1.57330543308284,-0.304505859696147,-0.427366008670849,-0.911586830104039,0.754592619764716,-0.88649486036092,1.59554662379421,-3.3656250629596,-3.35884235957318,-1.30828660436138,"OH" 38 | "Oklahoma",33.23,66.77,34.35,65.65,34.43,65.57,38.43,60.31,40.45,48.26,34.02,42.65,41.28,57.93,30.67,68.61,34.97,60.5,48.75,49.96,24,73.7,31.99,47.68,55.75,44.25,40.98,59.02,"0.00%",44.87,55.13,45.41,54.59,-33.54,-31.3,-31.14,-21.88,-7.81,-8.63,-16.65,-37.94,-25.53,-1.21,-49.7,-15.69,11.5,-18.04,-10.26,-9.18000000000001,-18.7341766741299,-19.3414796384686,-14.3275757575758,-11.344538062707,-9.13238977377492,-9.08314915854418,-4.49308881712693,-9.94115955629551,-8.06336891160869,-1.66285552537004,-13.6466356299794,-9.44055303119564,-5.59445337620579,-9.10562506295961,2.62115764042682,0.861713395638619,"OK" 39 | "Oregon",54.24,42.15,56.75,40.4,51.35,47.19,46.96,46.52,47.15,39.06,42.48,32.53,51.28,46.61,43.74,55.91,38.67,48.33,47.62,47.78,42.33,52.45,43.78,49.83,63.72,35.96,47.32,52.56,"0.00%",44.75,55.25,38.93,60.54,12.09,16.35,4.16,0.439999999999998,8.09,9.95,4.67,-12.17,-9.66,-0.160000000000004,-10.12,-6.05,27.76,-5.24,-10.5,-21.61,4.30722077373817,4.72334280105794,3.35324218437675,-0.0295906062273255,-0.0383746941085783,3.17731602146478,6.28353285633677,3.06004267730776,-0.244400685446289,-1.13380647394842,6.44969044151558,-2.82517706125381,2.58010521127414,-2.7087728402924,2.50115764042682,-5.41085823399845,"OR" 40 | "Pennsylvania",52.08,46.68,54.47,44.15,50.92,48.42,50.6,46.43,49.17,39.97,45.15,36.13,48.39,50.7,45.99,53.34,42.48,49.59,50.4,47.73,39.13,59.11,47.59,44.02,64.92,34.7,51.06,48.74,"0.00%",43.3,56.49,46.85,52.74,5.4,10.32,2.5,4.17,9.2,9.02,-2.31,-7.35,-7.11000000000001,2.67,-19.98,3.57,30.22,2.32,-13.19,-5.89,0.769723690390189,1.54072478254137,2.50072905418184,1.88388488765692,0.430016027401847,2.09358748710662,2.73259535943924,5.46662639685895,1.44613088161475,0.310491200744445,1.61939554635979,2.35479254114926,3.82318364447279,1.07669958633899,1.14227899527199,2.49458918638067,"PA" 41 | "Rhode Island",62.7,35.24,62.86,35.06,59.42,38.67,60.99,31.91,59.71,26.82,47.04,29.02,55.64,43.93,48.02,51.66,47.67,37.2,55.36,44.08,46.81,53,64.03,31.78,80.87,19.13,63.63,36.37,"0.00%",41.74,58.26,49.05,50.89,27.46,27.8,20.75,29.08,32.89,18.02,11.71,-3.63999999999999,10.47,11.28,-6.19,32.25,61.74,27.26,-16.52,-1.84,12.0546103383268,10.5037817994399,11.8194453454928,15.3863028252694,14.2745635937939,8.39077813746347,9.77848760983074,7.34057228373926,11.475580787232,4.62181283485172,8.68747779350885,17.2365000413058,19.5255466237942,13.5443749370404,-0.508842359573181,4.53116106423979,"RI" 42 | "South Carolina",44.09,54.56,44.9,53.87,40.9,57.98,40.91,56.83,43.85,49.89,39.88,48.02,37.58,61.5,35.57,63.55,48.04,49.57,56.17,43.13,27.92,70.58,29.61,38.09,41.1,58.89,51.24,48.76,"0.00%",45.37,25.18,50.72,49.28,-10.47,-8.97,-17.08,-15.92,-6.04,-8.14,-23.92,-27.98,-1.53,13.04,-42.66,-8.48,-17.79,2.48,20.19,1.44,-7.27081630920335,-8.23233212404111,-7.39430714916152,-8.40899072280288,-7.9520827598672,-8.0853946463127,-8.17285131062776,-4.94779002366704,4.52359227740022,5.51601270052446,-9.86645284729751,-5.85660936697808,-20.2403429651647,1.15437493704039,22.060158349144,6.17171339563862,"SC" 43 | "South Dakota",39.87,57.89,44.75,53.16,38.44,59.91,37.56,60.3,43.03,46.49,37.14,40.66,46.51,52.85,36.53,63,31.69,60.53,48.91,50.39,45.52,54.15,41.96,53.27,55.61,44.39,41.79,58.21,"0.00%",41.61,58.39,30.73,69.27,-18.02,-8.41,-21.47,-22.74,-3.46,-3.52,-6.34,-26.47,-28.84,-1.48,-8.63,-11.31,11.22,-16.42,-16.78,-38.54,-11.1806251193017,-7.98624013280009,-9.67267489331546,-11.8835739367928,-6.66293482477854,-5.71734378224948,0.707783703807779,-4.13108326136769,-10.3291979094758,-1.79516554720967,7.45908284179629,-5.53193945098057,-5.73445337620578,-8.2956250629596,-0.638842359573183,-13.8182866043614,"SD" 44 | "Tennessee",39.08,59.48,41.79,56.85,42.53,56.8,47.28,51.15,48,45.59,47.08,42.43,41.55,57.89,41.57,57.84,48.41,48.7,55.94,42.94,29.75,67.7,28.13,37.85,55.5,44.49,45.77,52.92,"0.00%",48.6,49.21,49.71,49.99,-20.4,-15.06,-14.27,-3.87,2.41,4.65,-16.34,-16.27,-0.290000000000006,13,-37.95,-9.72,11.01,-7.15,-0.609999999999999,-0.280000000000001,-12.3132026481558,-11.3252995898068,-5.94070270814457,-2.23079913076866,-3.44287506189552,-0.857657842892134,-4.31780727070586,0.983133620344329,5.15800824292856,5.52367556386885,-7.68315437066105,-6.95955314431,-5.83890282115028,-3.70807921231617,7.43932858409311,5.31129213184725,"TN" 45 | "Texas",41.38,57.17,43.63,55.39,38.22,61.09,37.98,59.3,43.83,48.76,37.08,40.56,43.35,55.95,36.11,63.61,41.42,55.28,51.14,47.97,33.24,66.2,41.14,39.87,63.32,36.49,50.52,48.52,"0.00%",43.98,55.26,46.69,53.13,-15.79,-11.76,-22.87,-21.32,-4.93,-3.48,-12.6,-27.5,-13.86,3.17,-32.96,1.27,26.83,2,-11.28,-6.44,-9.97533852090814,-9.62967394264954,-10.2720254605261,-11.2229942754614,-7.39268032467667,-5.69624581377327,-2.44620849277604,-4.62219312231906,-1.85917085982136,0.549284143659412,-4.78443823552233,1.19016914587763,2.09608364413285,0.924067990352184,2.06796537924181,2.22590694402572,"TX" 46 | "Total",51.06,47.2,52.87,45.6,48.27,50.73,48.38,47.87,49.23,40.72,43.01,37.45,45.65,53.37,40.56,58.77,41.01,50.75,50.08,48.02,37.52,60.67,42.72,43.42,61.05,38.47,49.72,49.55,"0.42%",41.97,57.37,44.33,55.18,3.86,7.27,-2.45999999999999,0.510000000000005,8.51,5.56,-7.72,-18.21,-9.74,2.06,-23.15,-0.700000000000003,22.58,0.170000000000002,-15.4,-10.85,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,NA 47 | "Utah",24.75,72.79,34.22,62.25,26,71.54,26.34,66.83,33.3,54.37,24.65,43.36,32.05,66.22,24.68,74.5,20.57,72.77,33.65,62.44,26.39,67.64,37.07,56.49,54.86,45.14,45.17,54.81,"0.00%",35.44,64.56,41.07,58.93,-48.04,-28.03,-45.54,-40.49,-21.07,-18.71,-34.17,-49.82,-52.2,-28.79,-41.25,-19.42,9.72,-9.64,-29.12,-17.86,-26.5899712199572,-18.219312125252,-22.1018447754146,-21.9940324138671,-16.7470591401773,-17.2104630838492,-13.4875715049101,-15.9495358161631,-22.6549649554861,-16.0306962477519,-10.1461195051467,-9.97205152275549,-6.48445337620579,-4.90658925579817,-6.80884235957318,-3.47828660436137,"UT" 48 | "Vermont",66.57,30.97,67.46,30.45,58.94,38.8,50.63,40.7,53.35,31.09,46.11,30.42,47.58,51.1,40.81,57.92,38.41,44.37,43.14,54.34,36.47,62.66,43.53,52.75,66.3,33.69,41.35,58.65,"0.00%",27.81,72.16,28.23,71.45,35.6,37.01,20.14,9.93,22.26,15.69,-3.52,-17.11,-5.96,-11.2,-26.19,-9.22,32.61,-17.3,-44.35,-43.22,16.2847468444266,15.2085305749928,11.5452685231691,5.17139472812307,8.45055111147859,6.79574902172173,2.11465961886561,0.501368895085375,1.70742009418563,-6.79471718917119,-1.42155687159827,-4.38180268852484,4.96217728686053,-8.7356250629596,-14.4304968559221,-16.2276606011511,"VT" 49 | "Virginia",51.16,47.28,52.63,46.33,45.48,53.68,44.44,52.47,45.15,47.1,40.59,44.97,39.23,59.74,37.09,62.29,40.31,53.03,47.96,49.29,30.12,67.84,32.49,43.36,53.54,46.18,46.97,52.44,"0.00%",38.36,55.37,43.36,56.32,3.88,6.3,-8.2,-8.03,-1.95,-4.38,-20.51,-25.2,-12.72,-1.33,-37.72,-10.87,7.36,-5.47,-17.01,-12.96,0.00656692603266951,-0.508375353909152,-2.89230750424781,-4.40795436118933,-5.78731635009513,-6.01474027846194,-6.46352339213063,-3.51219238529866,-1.50647556187137,-1.73375365884976,-7.46438673930357,-6.75914284039457,-7.6541204439956,-2.83685733335494,-1.32277813253808,-1.04908917257968,"VA" 50 | "Washington",56.16,41.29,57.34,40.26,52.82,45.64,50.13,44.56,49.84,37.3,43.41,31.97,50.05,48.46,42.86,55.82,37.32,49.66,46.11,50,38.64,56.92,47.23,45.12,61.97,37.37,48.27,50.68,"0.00%",45.44,53.91,44.69,54.33,14.87,17.08,7.18,5.57,12.54,11.44,1.59,-12.96,-12.34,-3.89,-18.28,2.11,24.6,-2.41,-8.47,-9.64,5.66537694310976,5.05852036153144,4.88857496352925,2.67624140565317,2.46491209828529,4.13308670158529,4.70522705090334,2.59973480201419,-1.78626127963758,-3.07367184920523,2.2236980770958,1.54870837051631,1.037265971489,-1.30341182395,3.48845004103074,0.584009901395038,"WA" 51 | "West Virginia",35.53,62.27,42.51,55.6,43.2,56.06,45.59,51.92,51.51,36.76,48.41,35.39,52.2,47.46,44.6,55.11,49.81,45.3,58.07,41.93,36.39,63.61,49.6,40.78,67.94,32.06,52.73,47.27,"0.00%",45.92,54.08,51.92,48.08,-26.74,-13.09,-12.86,-6.33,14.75,13.02,4.74,-10.51,4.51000000000001,16.14,-27.22,8.82,35.88,5.45999999999999,-8.16,3.84,-15.6349333203466,-10.3625631161976,-5.23551248939119,-3.51075600637697,3.62464123385139,4.31336343471341,6.27628787402518,3.89613115728152,7.67826236532251,7.02005096839959,-1.82163051227213,5.28571339759743,6.59554662379421,2.64437493704039,3.67115764042682,7.37171339563862,"WV" 52 | "Wisconsin",52.83,45.89,56.22,42.31,49.7,49.32,47.83,47.61,48.81,38.48,41.13,36.78,51.41,47.8,45.02,54.19,43.18,47.9,49.5,47.83,43.72,53.4,44.27,47.89,62.09,37.74,48.05,51.77,"0.00%",37.84,61.58,38.71,60.95,6.94,13.91,0.380000000000003,0.219999999999999,10.33,4.35,3.61,-9.16999999999999,-4.72,1.67,-9.68,-3.62,24.35,-3.72000000000001,-23.74,-22.24,1.55081522214242,3.36728418980708,1.43430467062056,-0.149679406930037,1.18665230126511,-0.663450274514132,5.71757543042877,4.54490505193383,2.71619477439954,-0.192042938925996,6.80484395230777,-1.55766039377112,0.851279369461844,-1.94897910022668,-4.18808999586366,-5.70622359011293,"WI" 53 | "Wyoming",27.82,68.64,32.54,64.78,29.07,68.86,27.7,67.76,36.84,49.81,34.1,39.7,38.01,60.53,28.24,70.51,27.97,62.64,39.81,59.3,30.47,69.01,35.51,55.76,56.56,43.44,44.99,55.01,"0.00%",39.92,60.08,37.09,62.71,-40.82,-32.24,-39.79,-40.06,-12.97,-5.6,-22.52,-42.27,-34.67,-19.49,-38.54,-20.25,13.12,-10.02,-20.16,-25.62,-23.1232063237255,-20.2553925032446,-19.0731072596691,-21.2475455824293,-12.2145373447517,-7.24917092571373,-7.52862936009754,-12.2361166652012,-13.8241189931614,-10.8824583646647,-7.58235829675143,-10.6871436672241,-4.78445337620579,-5.0956250629596,-2.32884235957318,-7.38395794704675,"WY" 54 | -------------------------------------------------------------------------------- /model/Functions.R: -------------------------------------------------------------------------------- 1 | # Set locale 2 | Sys.setlocale(locale="C") 3 | 4 | library(gam) 5 | library(gtools) 6 | library(lubridate) 7 | library(maps) 8 | library(plyr) 9 | library(RJSONIO) 10 | 11 | data(state.fips) 12 | state.fips$abb <- as.character(state.fips$abb) 13 | state.fips <- rbind(state.fips, tail(state.fips, 2)) 14 | state.fips[64, ]$fips <- 2 15 | state.fips[64, ]$abb <- "AK" 16 | state.fips[65, ]$fips <- 15 17 | state.fips[65, ]$abb <- "HI" 18 | 19 | cap <- function(x) { 20 | # capitalize first character 21 | s <- strsplit(x, " ") 22 | paste(toupper(substring(s, 1, 1)), substring(s, 2), sep="") 23 | } 24 | 25 | 26 | candidateRename <- function(cand) { 27 | cand[cand == "Dick Durbin"] <- "Richard Durbin" 28 | cand[cand == "Mary Landrieu"] <- "Mary L. Landrieu" 29 | cand[cand == "Ed Markey"] <- "Edward J. Markey" 30 | cand[cand == "Jim Inhofe"] <- "James M. Inhofe" 31 | cand[cand == "Mike Enzi"] <- "Michael B. Enzi" 32 | cand[cand == "Daniel S Sullivan"] <- "Dan Sullivan" 33 | cand[cand == "Campbell Cavasso"] <- "Cam Cavasso" 34 | cand[cand == "Matthew Whitaker"] <- "Matt Whitaker" 35 | cand[cand == "TW Shannon"] <- "T.W. Shannon" 36 | cand[cand == "Christine ODonnell"] <- "Christine O'Donnell (potential)" 37 | cand[cand == "Tom Kovach"] <- "Tom Kovach (potential)" 38 | cand 39 | } 40 | 41 | 42 | demProb <- function(sims, dem.seats = 34) { 43 | mean(dem.seats + colSums(sims > 0) >= 50) 44 | } 45 | 46 | 47 | electionDay <- function(year) { 48 | # fed numeric or character year, return election day as Date 49 | year <- as.numeric(year) 50 | pos <- match("Monday", weekdays(as.Date(paste0(year, "-11-0", 1:7)))) 51 | as.Date(paste0(year, "-11-0", pos + 1)) 52 | } 53 | 54 | 55 | electionSimNew <- function(keep, 56 | breaks=c(-1e-8, 5, 15, 35), 57 | s=.840, # share of error that is nat'l (vs local), by e-day 58 | n.sims=10000, # number of simulations 59 | best=F, 60 | snowflakes=F, 61 | fund=F 62 | ) { 63 | race.names <- unique(keep$office) 64 | n <- length(race.names) 65 | if (!best) { 66 | c <- cumsum(keep$matchupProb) 67 | t <- 0:(n - 1) + runif(n*n.sims) 68 | samp <- sapply(t, function(y) match(T, y < c)) 69 | new <- subset(keep[samp, ], select=c(mean, scale, alpha, prior.mean, prior.var, prior.alpha)) 70 | } else { 71 | new <- keep 72 | } 73 | 74 | mu <- new$mean 75 | scale <- new$scale 76 | df <- 2*new$alpha 77 | if (fund) { 78 | mu <- new$prior.mean 79 | scale <- sqrt(new$prior.var) 80 | df <- 2*new$prior.alpha 81 | } 82 | 83 | days <- keep$d[1] 84 | 85 | # flatten covariance. make posdef. 86 | # k <- min(1, days / 300) 87 | # s <- (1 - k)*s 88 | 89 | sims <- mu + (matrix(rt(n*n.sims, df), ncol=n.sims))*sqrt(1 - s^2)*scale + 90 | rep(rnorm(n.sims, sd=1), each=n)*s*scale 91 | 92 | if (days == min(day.seq) & !snowflakes & !best) { 93 | probs <- rowMeans(sims > 0) 94 | rating <- 8 - cut(100*probs, breaks=c(breaks, rev(100 - breaks)), labels=F) 95 | upsets <- colSums(sims[rating <= 2, ] < 0) + colSums(sims[rating >= 6, ] > 0) 96 | # round down to mult of 5 97 | k <- 5*floor(100*mean(upsets > 0) / 5) 98 | h <- mean(sapply(h.eff_list, function(x) { 99 | temp <- subset(x, d == days) 100 | temp$mu[match("Public Policy Polling", temp$pollsters)] - 101 | temp$mu[match("Harper Polling", temp$pollsters)] 102 | }), na.rm = T) 103 | # 104 | mt <- mean(sims[match("mt-senate-class-ii-2014", race.names), ] < 0) 105 | t <- (1:20)*mt 106 | pos <- which.min((t - round(t))^2) 107 | mtNum <- round(pos*mt) 108 | mtDenom <- pos 109 | mt <- round(100*mt) 110 | aan <- ifelse(mt == 8 | mt == 11 | floor(mt / 10) == 8, "an", "a") 111 | 112 | filename <- paste0(dataDir, "public/_big_assets/parameters.json") 113 | 114 | write(toJSON(list( 115 | continuingSeats = list( 116 | dem = 34, rep = 30), 117 | nationalErrorScale = s, 118 | upsetProb = k, 119 | houseEffect = round(2*h) / 2, 120 | mt = mt, 121 | mtNum = textify(mtNum), 122 | mtDenom = textify(mtDenom), 123 | aan = aan 124 | )), file=filename) 125 | } 126 | 127 | return(sims) 128 | 129 | } 130 | 131 | getEstimate <- function(results, 132 | type, # choices: normal, polls, fundys 133 | house.effects = T, 134 | time.weights = T) { 135 | colname <- paste0("pollAvg", paste(as.numeric(c(house.effects, time.weights)), collapse="")) 136 | poll.avg <- get(colname, envir = as.environment(results)) 137 | with(results, 138 | switch(type, 139 | "normal" = (prior.mean*(kappa - tau) + poll.avg*tau) / kappa, 140 | "polls" = poll.avg, 141 | "fundys" = prior.mean 142 | )) 143 | } 144 | 145 | 146 | ### push data 147 | gitPush <- function(message = "auto update", 148 | file = ".", 149 | silent = T) { 150 | system(paste("git add", file), ignore.stdout = silent, ignore.stderr = silent) 151 | system(paste("git commit -m '", message, file, "'"), 152 | ignore.stdout = silent, ignore.stderr = silent) 153 | system("git push", ignore.stdout = silent, ignore.stderr = silent) 154 | } 155 | 156 | 157 | hardcodeGuys <- function(results, var = "party"){ 158 | # re-code parties where necessary 159 | results[results$xx == "2000GAMiller", var] <- "DEM" #harmless 160 | results[results$xx == "2000GAMattingly", var] <- "REP" #harmless 161 | results[results$xx == "2006CTLieberman", var] <- "REP" # problematic, but lieberman runs to the right of Ned Lamont. means ignoring Alan Schlesinger 162 | results[results$xx == "2006VTSanders", var] <- "DEM" #harmless 163 | results[results$xx == "2008MSMusgrove", var] <- "DEM" #harmless 164 | results[results$xx == "2010AKMurkowski", var] <- "DEM" # problematic, but Murkowski to the left of Joe Miller. means ignoring Scott Adams. 165 | results[results$xx == "2010FLCrist", var] <- "DEM" # problematic, but Crist to the left of Marco Rubio + would caucus with dems. means ignoring Kendrick B. Meek. 166 | results[results$xx == "2012CTMurphy", var] <- "DEM" #harmless 167 | results[results$xx == "2012CTMcMahon", var] <- "REP" #harmless 168 | results[results$xx == "2012MEKing", var] <- "DEM" # mostly harmless, means ignoring Cynthia Dill 169 | results[results$xx == "2012MNKlobuchar", var] <- "DEM" #harmless 170 | results[results$xx == "2012NYGillibrand", var] <- "DEM" #harmless 171 | results[results$xx == "2012NYLong", var] <- "REP" #harmless 172 | results[results$xx == "2012NDHeitkamp", var] <- "DEM" #harmless 173 | results[results$xx == "2012VTSanders", var] <- "DEM" #harmless 174 | results 175 | } 176 | 177 | 178 | 179 | histoMatic <- function(sims) { 180 | tab <- table(colSums(sims > 0)) 181 | h <- rep(0, nrow(sims) + 1) 182 | seats <- 34 + seq_along(h) - 1 183 | h[as.numeric(names(tab)) + 1] <- tab 184 | h <- setNames(h, seats) 185 | return (h / ncol(sims)) 186 | } 187 | 188 | 189 | houseEffects <- function( 190 | #### Estimate house effects #### 191 | p, # polls 192 | ratings = NULL, # pollster ratings (from pollsterRatings function) 193 | prior = NULL, # prior houseEffects object, optional 194 | kappa0 = 4, # effective sample size of the prior distribution on mu 195 | lambda.m = 1, # mean and variance of prior marginal distribution of 196 | lambda.v = 1/2, # precision, lambda 197 | predictive = T, # whether the model is intended to be predictive... 198 | # ie, whether to not include election results in calc 199 | election.ss = 50000, # default sample size given to election result 200 | alpha0 = NULL, 201 | beta0 = NULL 202 | ){ 203 | # add a poll for each race giving result 204 | x <- p[1, ] 205 | if (predictive == F) { 206 | for (race in as.character(unique(p$office))) { 207 | ind <- match(race, names(races)) 208 | x$sample_size <- election.ss # choose strength here 209 | x$mid_date <- 0 210 | x$margin <- x$obsMargin <- -diff(races[[ind]]$result) 211 | x$sample_varnum <- 1 212 | x$pollster <- "Election" 213 | x$office <- race 214 | p <- rbind(p, x) 215 | } 216 | } 217 | 218 | effective.ss <- matrix(0, nrow(p), nrow(p)) 219 | # matrix gives effective sample size for poll in COLUMN for calculating 220 | # current mean for poll in ROW 221 | for (j in 1:nrow(p)) { 222 | w <- timeWeight(abs(p$mid_date - p$mid_date[j]), p$mid_date[j]) * 223 | as.numeric(p$office == p$office[j]) # only include if same race 224 | effective.ss[j, ] <- w*p$sample_size 225 | } 226 | 227 | pollsters <- unique(p$pollster) 228 | mu <- rep(0, length(pollsters)) 229 | kappa <- rep(kappa0, length(pollsters)) 230 | # choose parameters 231 | # based on other measures of house effects, house effects of legitimate 232 | # pollsters seem to all be in range of +/- 3 points. so use this as prior? 233 | if (is.null(alpha0)) { 234 | alpha0 <- lambda.m^2 / lambda.v 235 | } 236 | if(is.null(beta0)) { 237 | beta0 <- lambda.m / lambda.v 238 | } 239 | alpha <- rep(alpha0, length(pollsters)) 240 | beta <- rep(beta0, length(pollsters)) 241 | h.eff <- data.frame(pollsters, mu, kappa, alpha, beta, stringsAsFactors=F) 242 | if (!is.null(prior)) { 243 | # incorporate prior info if given 244 | pos <- match(h.eff$pollsters, prior$pollsters) 245 | h.eff[!is.na(pos), ] <- prior[pos[!is.na(pos)], ] 246 | } 247 | poll.pos <- match(p$pollster, h.eff$pollster) 248 | tab <- table(poll.pos) 249 | h.eff$number <- as.numeric(tab[order(as.numeric(names(tab)))]) 250 | tau <- 1 / (10000*p$sample_varnum/t(effective.ss)) 251 | 252 | # est gives poll result after correcting for prior house effect estimate 253 | p$est <- p$margin - h.eff$mu[poll.pos] 254 | # xbar gives best estimate of state of race at time of poll 255 | p$xbar <- apply(tau, 2, function(w) weighted.mean(p$est, w)) 256 | p$tau <- colSums(tau) # precision of estimate 257 | 258 | num <- length(setdiff(h.eff$pollster, "Election")) 259 | for (i in 1:num) { 260 | m <- h.eff$mu[i] 261 | k <- h.eff$kappa[i] 262 | n <- p$tau[poll.pos == i] 263 | if (sum(n) == 0) next # if no weights, nothing updates 264 | x <- p$margin[poll.pos == i] - p$xbar[poll.pos == i] 265 | h.eff$mu[i] <- weighted.mean(c(m, x), c(k, n)) 266 | h.eff$alpha[i] <- h.eff$alpha[i] + sum(n)/2 267 | mu.star <- weighted.mean(x, n) # weighted mean of observations 268 | c <- h.eff$beta[i] + sum((x - mu.star)^2) / 2 269 | h.eff$beta[i] <- c + k*sum(n)*(mu.star - m)^2 / (2*(k + sum(n))) 270 | h.eff$kappa[i] <- k + sum(n) 271 | } 272 | return(h.eff) 273 | } 274 | 275 | imputePolls <- function(polls, races, race.name, delete=F, a0=20) { 276 | # combines multiple candidate combos from single poll into one D-R number. 277 | # also use to specify what polls say if candidate x wins 278 | 279 | # polls: polls 280 | # races: races 281 | # race.name: eg, "ak-senate-class-ii-2014" 282 | 283 | # add'l arguments for specifying primary winner 284 | # party: "DEM" or "REP" 285 | # candidate: must match candidate name in races object 286 | # a0: dispersion param for Dirichlet dist. passed to probGet fxn 287 | 288 | # if candidate specified, delete polls first 289 | x <- races[[match(race.name, names(races))]] 290 | 291 | if (delete) { 292 | polls <- polls[!(polls$office == race.name), ] 293 | } 294 | 295 | race.polls <- x$polls 296 | if (is.null(race.polls)) { 297 | return(polls) 298 | } 299 | p <- race.polls[race.polls$branch == "sen_g", ] 300 | if (is.null(p)) { 301 | return(polls) 302 | } 303 | if (nrow(p) == 0) { 304 | return(polls) 305 | } 306 | p$id <- substr(p$poll_id, 1, 5) 307 | # if it's general election polling, just make composite ... impute values 308 | reduced <- p[, -(1:match("type", names(p)))] # just numbers 309 | temp <- reduced[apply(reduced, 1, function(x) sum(!is.na(x)) > 0), ] 310 | # split into Dem table and Rep table 311 | id <- temp[, ncol(temp)] 312 | temp <- temp[, -((ncol(temp) - 2):ncol(temp))] 313 | if (is.null(ncol(temp))) { 314 | return(polls) 315 | } 316 | reps <- data.frame(temp[, x$party == "REP"]) 317 | dems <- data.frame(temp[, x$party == "DEM"]) 318 | pos <- apply(reps, 1, function(x) sum(!is.na(x)) > 0) & 319 | apply(dems, 1, function(x) sum(!is.na(x)) > 0) 320 | id <- id[pos] 321 | reps <- data.frame(reps[pos, ]) 322 | dems <- data.frame(dems[pos, ]) 323 | if (nrow(dems)*nrow(reps) == 0) { 324 | return(polls) 325 | } 326 | colnames(reps) <- r.names <- x$slug[x$party == "REP"] 327 | colnames(dems) <- d.names <- x$slug[x$party == "DEM"] 328 | start.column <- match("type", names(p)) + 1 329 | 330 | d.probs <- colSums(x$probs) 331 | r.probs <- rowSums(x$probs) 332 | 333 | r <- NULL 334 | d <- NULL 335 | # now combine the results into one D vs. R number for each poll 336 | agg <- r.res <- d.res <- array(NA, dim=c(length(r.names), length(d.names), 337 | length(unique(id))), 338 | dimnames=list(r.names, d.names, unique(id))) 339 | 340 | if (length(r.names)*length(d.names) == 1) { 341 | 342 | margin <- setNames(dems - reps, "margin") 343 | other <- setNames(100 - (dems + reps), "other") 344 | 345 | } else { 346 | 347 | for (j in 1:length(unique(id))) { 348 | pos <- id == unique(id)[j] 349 | r.temp <- data.frame(reps[pos, ]) 350 | d.temp <- data.frame(dems[pos, ]) 351 | jj <- 1 352 | # 353 | for (jj in 1:nrow(r.temp)) { 354 | row <- which(!is.na(r.temp[jj, ])) 355 | col <- which(!is.na(d.temp[jj, ])) 356 | r.res[cbind(row, col, j)] <- pmax(r.res[cbind(row, col, j)], 357 | r.temp[cbind(jj, row)], na.rm=T) 358 | d.res[cbind(row, col, j)] <- pmax(d.res[cbind(row, col, j)], 359 | d.temp[cbind(jj, col)], na.rm=T) 360 | } 361 | } 362 | imputed <- agg <- apply(d.res - r.res, 3, as.vector) 363 | other <- agg.other <- apply(100 - (d.res + r.res), 3, as.vector) 364 | for (j in 1:ncol(agg)) { 365 | y <- agg[, j] 366 | m <- colMeans(agg - y, na.rm=T) 367 | w <- nrow(agg) - colSums(is.na(agg - y)) # weights 368 | pos <- which(is.na(y)) 369 | if (length(pos) > 0) { 370 | imputed[pos, j] <- apply(agg[pos, ] - matrix( 371 | m, length(pos), ncol(agg), byrow=T), 1, weighted.mean, w=w, na.rm=T) 372 | } 373 | # other: 374 | y <- agg.other[, j] 375 | m <- colMeans(agg.other - y, na.rm=T) 376 | w <- nrow(agg.other) - colSums(is.na(agg.other - y)) # weights 377 | pos <- which(is.na(y)) 378 | if (length(pos) > 0) { 379 | other[pos, j] <- apply(agg.other[pos, ] - matrix( 380 | m, length(pos), ncol(agg.other), byrow=T), 1, weighted.mean, w=w, na.rm=T) 381 | } 382 | } 383 | imputed[is.na(imputed)] <- NA 384 | other[is.na(other)] <- NA 385 | probs <- as.vector(outer(r.probs, d.probs)) 386 | margin <- apply(imputed, 2, weighted.mean, w=probs, na.rm=T) 387 | other <- apply(other, 2, weighted.mean, w=probs, na.rm=T) 388 | } 389 | 390 | general <- p[match(unique(id), p$id), 1:match("type", names(p))] 391 | general <- cbind(general, margin, other) 392 | general$office <- race.name 393 | 394 | general$poll_id <- sapply(strsplit(general$poll_id, "_"), function(x) x[1]) 395 | start <- as.Date(general$start, format="%Y-%m-%d") 396 | end <- as.Date(general$end, format="%Y-%m-%d") 397 | general$mid_date <- as.numeric(electionDay(2014) - (start + (end - start)/2)) 398 | colnames(general)[match("sample", colnames(general))] <- "sample_size" 399 | colnames(general)[match("type", colnames(general))] <- "population" 400 | general <- general[order(general$mid_date), ] 401 | general <- general[!is.na(general$margin), ] 402 | 403 | return(rbind(polls, general)) 404 | } 405 | 406 | 407 | 408 | imputePollsNew <- function(polls, races, race.name, probs, delete=F, a0=20) { 409 | # combines multiple candidate combos from single poll into one D-R number. 410 | # also use to specify what polls say if candidate x wins 411 | 412 | # polls: polls 413 | # races: races 414 | # race.name: eg, "ak-senate-class-ii-2014" 415 | 416 | # add'l arguments for specifying primary winner 417 | # party: "DEM" or "REP" 418 | # candidate: must match candidate name in races object 419 | # a0: dispersion param for Dirichlet dist. passed to probGet fxn 420 | 421 | # if candidate specified, delete polls first 422 | x <- races[[match(race.name, names(races))]] 423 | 424 | if (delete) { 425 | polls <- polls[!(polls$office == race.name), ] 426 | } 427 | 428 | race.polls <- x$polls 429 | if (is.null(race.polls)) { 430 | return(polls) 431 | } 432 | p <- race.polls[race.polls$branch == "sen_g", ] 433 | if (is.null(p)) { 434 | return(polls) 435 | } 436 | if (nrow(p) == 0) { 437 | return(polls) 438 | } 439 | p$id <- substr(p$poll_id, 1, 5) 440 | # if it's general election polling, just make composite ... impute values 441 | reduced <- p[, -(1:match("type", names(p)))] # just numbers 442 | temp <- reduced[apply(reduced, 1, function(x) sum(!is.na(x)) > 0), ] 443 | # split into Dem table and Rep table 444 | id <- temp[, ncol(temp)] 445 | temp <- temp[, -((ncol(temp) - 2):ncol(temp))] 446 | if (is.null(ncol(temp))) { 447 | return(polls) 448 | } 449 | reps <- data.frame(temp[, x$party == "REP"]) 450 | dems <- data.frame(temp[, x$party == "DEM"]) 451 | pos <- apply(reps, 1, function(x) sum(!is.na(x)) > 0) & 452 | apply(dems, 1, function(x) sum(!is.na(x)) > 0) 453 | id <- id[pos] 454 | reps <- data.frame(reps[pos, ]) 455 | dems <- data.frame(dems[pos, ]) 456 | if (nrow(dems)*nrow(reps) == 0) { 457 | return(polls) 458 | } 459 | colnames(reps) <- r.names <- rownames(probs) 460 | colnames(dems) <- d.names <- colnames(probs) 461 | start.column <- match("type", names(p)) + 1 462 | 463 | d.probs <- colSums(probs) 464 | r.probs <- rowSums(probs) 465 | 466 | r <- NULL 467 | d <- NULL 468 | # now combine the results into one D vs. R number for each poll 469 | 470 | agg <- r.res <- d.res <- array(NA, dim=c(length(r.names), length(d.names), 471 | length(unique(id))), 472 | dimnames=list(r.names, d.names, unique(id))) 473 | 474 | if (length(r.names)*length(d.names) == 1) { 475 | 476 | margin <- setNames(dems - reps, "margin") 477 | other <- setNames(100 - (dems + reps), "other") 478 | 479 | } else { 480 | 481 | for (j in 1:length(unique(id))) { 482 | pos <- id == unique(id)[j] 483 | r.temp <- data.frame(reps[pos, ]) 484 | d.temp <- data.frame(dems[pos, ]) 485 | jj <- 1 486 | # 487 | for (jj in 1:nrow(r.temp)) { 488 | row <- which(!is.na(r.temp[jj, ])) 489 | col <- which(!is.na(d.temp[jj, ])) 490 | r.res[cbind(row, col, j)] <- pmax(r.res[cbind(row, col, j)], 491 | r.temp[cbind(jj, row)], na.rm=T) 492 | d.res[cbind(row, col, j)] <- pmax(d.res[cbind(row, col, j)], 493 | d.temp[cbind(jj, col)], na.rm=T) 494 | } 495 | } 496 | imputed <- agg <- apply(d.res - r.res, 3, as.vector) 497 | other <- agg.other <- apply(100 - (d.res + r.res), 3, as.vector) 498 | for (j in 1:ncol(agg)) { 499 | y <- agg[, j] 500 | m <- colMeans(agg - y, na.rm=T) 501 | w <- nrow(agg) - colSums(is.na(agg - y)) # weights 502 | pos <- which(is.na(y)) 503 | if (length(pos) > 0) { 504 | imputed[pos, j] <- apply(agg[pos, ] - matrix( 505 | m, length(pos), ncol(agg), byrow=T), 1, weighted.mean, w=w, na.rm=T) 506 | } 507 | # other: 508 | y <- agg.other[, j] 509 | m <- colMeans(agg.other - y, na.rm=T) 510 | w <- nrow(agg.other) - colSums(is.na(agg.other - y)) # weights 511 | pos <- which(is.na(y)) 512 | if (length(pos) > 0) { 513 | other[pos, j] <- apply(agg.other[pos, ] - matrix( 514 | m, length(pos), ncol(agg.other), byrow=T), 1, weighted.mean, w=w, na.rm=T) 515 | } 516 | } 517 | imputed[is.na(imputed)] <- NA 518 | other[is.na(other)] <- NA 519 | probs <- as.vector(outer(r.probs, d.probs)) 520 | margin <- apply(imputed, 2, weighted.mean, w=probs, na.rm=T) 521 | other <- apply(other, 2, weighted.mean, w=probs, na.rm=T) 522 | 523 | } 524 | 525 | general <- p[match(unique(id), p$id), 1:match("type", names(p))] 526 | general <- cbind(general, margin, other) 527 | general$office <- race.name 528 | 529 | general$poll_id <- sapply(strsplit(general$poll_id, "_"), function(x) x[1]) 530 | start <- as.Date(general$start, format="%Y-%m-%d") 531 | end <- as.Date(general$end, format="%Y-%m-%d") 532 | # add middle date as days until election 533 | general$mid_date <- as.numeric(electionDay(2014) - (start + (end - start)/2)) 534 | colnames(general)[match("sample", colnames(general))] <- "sample_size" 535 | colnames(general)[match("type", colnames(general))] <- "population" 536 | colnames(general)[match("cycle", colnames(general))] <- "year" 537 | general <- general[order(general$mid_date), ] 538 | general <- general[!is.na(general$margin), ] 539 | 540 | return(rbind(polls, general)) 541 | } 542 | 543 | 544 | matchupSwitch <- function(race.name, d) { 545 | 546 | x <- new.races[[match(race.name, race.names)]] 547 | 548 | fec.ids <- x$fecid 549 | parties <- x$party 550 | 551 | d.pos <- which(parties == "DEM") 552 | r.pos <- which(parties == "REP") 553 | 554 | d.names <- x$slug[d.pos] 555 | r.names <- x$slug[r.pos] 556 | 557 | x$serious[x$day_in < d] <- 0 558 | x$serious[x$day_out < d] <- 1 559 | 560 | if (!is.null(x$polls)) { 561 | race.polls <- subset(x$polls, add_date >= d) 562 | if (nrow(race.polls) == 0) { 563 | race.polls <- NULL 564 | } 565 | } else { 566 | race.polls <- NULL 567 | } 568 | 569 | fec.temp <- subset(fec.new, daysOut >= d) 570 | fec.temp <- aggregate(list(money = fec.temp$money), 571 | list(fecid = fec.temp$cand_id), sum, na.rm = T) 572 | money <- fec.temp$money[match(fec.ids, fec.temp$fecid)] 573 | money[is.na(money)] <- 0 574 | 575 | d.probs <- probGet(d.names, d.pos, "D", money, race.polls) 576 | r.probs <- probGet(r.names, r.pos, "R", money, race.polls) 577 | 578 | probs <- outer(r.probs, d.probs) 579 | 580 | if (sum(probs) == 0) { 581 | probs <- probs + 1 582 | } 583 | 584 | rownames(probs) <- r.names 585 | colnames(probs) <- d.names 586 | 587 | return(probs) 588 | } 589 | 590 | 591 | outputGen <- function(results, sim_list, sim_list_fund, breaks = c(-1e-8, 5, 15, 35), 592 | best = F, snowflakes = F) { 593 | 594 | # output directory 595 | wd <- paste0(dataDir, "public/_big_assets/") 596 | 597 | # postfix 598 | pf <- paste0(ifelse(best, "-best", ""), ifelse(snowflakes, "-snowflakes", "")) 599 | 600 | res <- ddply(results, .(office, d), summarize, 601 | mean = weighted.mean(mean, matchupProb), 602 | probs = weighted.mean(prob, matchupProb)) 603 | res <- arrange(res, office, desc(d)) 604 | race.names <- unique(res$office) 605 | day.seq <- rev(sort(unique(res$d))) 606 | 607 | ##### state ratings ##### 608 | df <- subset(res, d == min(day.seq)) 609 | df$state <- toupper(substr(df$office, 1, 2)) 610 | # set breakpoints 611 | df$fips <- state.fips$fips[match(df$state, state.fips$abb)] 612 | df$probs <- 100*df$probs 613 | df$rating <- 8 - cut(df$probs, breaks=c(breaks, rev(100 - breaks)), labels=F) 614 | df$state[match("ok-senate-class-iii-2014", df$office)] <- "OK-2" 615 | df$state[match("sc-senate-class-iii-2014", df$office)] <- "SC-2" 616 | df$fips[match("ok-senate-class-iii-2014", df$office)] <- "40-2" 617 | df$fips[match("sc-senate-class-iii-2014", df$office)] <- "45-2" 618 | rownames(df) <- NULL 619 | 620 | df <- subset(df, select = c(probs, state, office, fips, rating)) 621 | colnames(df) <- c("dem-pct", "state", "office", "fips", "nyt-rating") 622 | new.df <- list() 623 | for (i in 1:nrow(df)) { 624 | new.df[[i]] <- df[i, ] 625 | } 626 | 627 | # df <- list("date"=format(Sys.time(), format = "%a %b %d %Y %H:%M:%S GMT-0500 (EST)"), 628 | # "data"=new.df) 629 | df <- list("date"=format(Sys.time(), format = "%Y-%m-%dT%T%z"), 630 | "data"=new.df) 631 | filename <- paste0(wd, "nyt-ratings-today", pf, ".json") 632 | write(toJSON(df), file=filename) 633 | 634 | ##### histogram ##### 635 | x <- sim_list[[length(sim_list)]] 636 | hist <- histoMatic(x) 637 | tab <- cbind(34:70, hist) 638 | filename <- paste0(wd, "histogram", pf, ".tsv") 639 | write.table(tab, file=filename, quote=F, sep='\t', row.names=F, 640 | col.names=c("dem", "chance")) 641 | 642 | ##### line chart ##### 643 | z <- sapply(sim_list, demProb) 644 | tab <- cbind(trim(format(electionDay(2014) - day.seq, "%e-%b-%y")), 645 | 100*z) 646 | if (just.today) { 647 | filename <- paste0(wd, "senate-likelihood", pf, "-new.tsv") 648 | } else { 649 | filename <- paste0(wd, "senate-likelihood", pf, ".tsv") 650 | } 651 | write.table(tab, file = filename, quote = F, sep='\t', row.names = F, 652 | col.names = c("date", "dem")) 653 | 654 | ##### all states time-series ##### 655 | z <- res$probs 656 | tab <- cbind(rep(race.names, each = length(day.seq)), 657 | trim(format(electionDay(2014) - day.seq, "%e-%b-%y")), 658 | 100*z) 659 | if (just.today) { 660 | filename <- paste0(wd, "senate-likelihood-all", pf, "-new.tsv") 661 | } else { 662 | filename <- paste0(wd, "senate-likelihood-all", pf, ".tsv") 663 | } 664 | write.table(tab, file = filename, 665 | quote = F, sep = '\t', row.names = F, 666 | col.names = c("office", "date", "dem")) 667 | } 668 | 669 | pollPredict <- function(yr, days, polls, races, 670 | fund.mean, 671 | fund.se, 672 | ratings = NULL, 673 | race.names = NULL, 674 | # parameters for bayes stuff: 675 | prior.alpha = 200, 676 | k.min = "useSpline", 677 | ...) { # add'l parameters passed to houseEffects fxn 678 | # prior.alpha numeric 679 | # k.min if "useSpline", the empirical spline is used. else should be numeric. 680 | load(paste(workingDir, "model/louisiana/la.RData", sep = "/")) 681 | 682 | #### incorporate non-polling info as prior #### 683 | if (is.null(race.names)) { 684 | race.names <- names(races)[sapply(races, function(x) x$cycle == yr)] 685 | } 686 | 687 | res <- data.frame(office = race.names, 688 | prior.mean = fund.mean, 689 | prior.var = fund.se^2, 690 | mean = fund.mean, 691 | var = fund.se^2, 692 | prior.alpha = prior.alpha, 693 | stringsAsFactors = F, row.names = NULL) 694 | 695 | # add polling info 696 | polls <- subset(polls, year == yr & add_date >= days) 697 | 698 | ### throw out pollsters 699 | dropme <- c() # "Public Policy Polling", "Rasmussen Reports", "Harper Polling", "Wenzel Strategies" 700 | if (length(dropme) > 0) { 701 | polls <- subset(polls, !(pollster %in% dropme)) 702 | } 703 | 704 | polls <- ddply(polls, .(office), mutate, min_days = min(mid_date)) 705 | h.eff <- houseEffects(polls, ratings, ...) # calculate house effects 706 | 707 | # apply house effects 708 | poll.pos <- match(polls$pollster, h.eff$pollster) 709 | polls$est <- polls$margin - h.eff$mu[poll.pos] 710 | 711 | # additional variance due to house effect estimation 712 | polls$var <- (h.eff$beta / ((h.eff$alpha - 1)*h.eff$kappa))[poll.pos] 713 | polls$pie <- 0 714 | polls$tau <- NA 715 | 716 | for (i in seq_along(unique(polls$office))) { 717 | pos <- which(polls$office == unique(polls$office)[i]) 718 | dd <- polls$min_days[pos[1]] 719 | polls$tau[pos] <- (1 / (10000*polls$sample_varnum/(polls$sample_size*timeWeight( 720 | polls$mid_date - dd, dd)) + polls$var + polls$pie^2))[pos] 721 | } 722 | 723 | load("optimizing/splines-new.RData") 724 | 725 | gamma <- gamma.spline$y[match(round(days), gamma.spline$x)] 726 | k.min <- gamma*k.spline$y[match(round(days), k.spline$x)] 727 | cap <- cap.spline$y[match(round(days), cap.spline$x)] 728 | 729 | res$kappa <- k <- k.min 730 | res$prior.beta <- (res$prior.alpha - 1)*k*res$prior.var 731 | res$poll.var <- res$pollAvg11 <- res$pollAvg10 <- res$pollAvg01 <- res$pollAvg00 <- res$trimmed <- res$jackknife1 <- res$simpAvg <- res$prob50 <- res$fit50 <- res$tau <- res$pollAvg <- res$beta <- res$alpha <- NA 732 | poll.df <- NULL 733 | for (i in 1:nrow(res)) { 734 | race <- res$office[i] 735 | p <- polls[polls$office == race, ] 736 | if (nrow(p) == 0) next 737 | m <- res$prior.mean[i] 738 | v <- res$prior.var[i] 739 | alpha <- res$prior.alpha[i] 740 | k <- k.min 741 | beta <- (alpha - 1)*k*v 742 | tau <- p$tau / (max(p$tau)) 743 | n <- gamma*min(1, cap / sum(tau))*tau 744 | p$weight <- n 745 | res$tau[i] <- tau <- sum(n) 746 | res$pollAvg[i] <- mu.star <- weighted.mean(p$est, n) 747 | # remove lv-adjustment for simp average 748 | q <- p$margin 749 | q[p$population == "rv"] <- q[p$population == "rv"] - lv.adj 750 | 751 | # 01 : time-weights 752 | # 10 : house effects + pollster ratings + lv-adjust 753 | 754 | res$pollAvg00[i] <- mean(q) 755 | res$pollAvg01[i] <- weighted.mean(q, timeWeight(p$mid_date - days, days)) 756 | res$pollAvg10[i] <- mean(p$est) 757 | res$pollAvg11[i] <- mu.star 758 | 759 | res$mean[i] <- weighted.mean(c(m, p$est), c(k, n)) 760 | 761 | # poll tracker 762 | poll.temp <- rbind(p, c(rep(NA, ncol(p) - 1), k)) 763 | # add fundamentals model 764 | poll.temp$office[nrow(poll.temp)] <- race 765 | poll.temp$est[nrow(poll.temp)] <- m 766 | poll.df <- rbind(poll.df, poll.temp) 767 | 768 | # 769 | q <- quantile(p$est, probs = c(.15, .85)) 770 | pos <- (p$est - q[1])*(p$est - q[2]) < 0 771 | res$trimmed[i] <- weighted.mean(p$est[pos], p$tau[pos]) 772 | 773 | # 774 | if (yr == 2014) { 775 | land.mean <- weighted.mean((100 - p$other + p$est) / 2, n) 776 | other <- weighted.mean(p$other, n) 777 | new <- t(setNames(c(NA, land.mean, other), 778 | c("result", "pollAvg", "other"))) 779 | pred <- predict.lm(lm.obj, newdata=as.data.frame(new), 780 | se=T, interval="prediction") 781 | t.star <- qt(.975, pred$df) 782 | scale <- -diff(pred$fit[, 1:2]) / t.star 783 | m <- pred$fit[1, 1] 784 | res$prob50[i] <- pt((m - 50) / scale, pred$df) 785 | } 786 | # 787 | res$alpha[i] <- alpha + sum(n)/2 788 | res$poll.var[i] <- sum(n*(p$est - mu.star)^2) / sum(n) 789 | c <- beta + sum((p$est - mu.star)^2) / 2 790 | res$beta[i] <- c + k*sum(n)*(mu.star - m)^2 / (2*(k + sum(n))) 791 | res$kappa[i] <- k + sum(n) 792 | } 793 | pos <- is.na(res$beta) 794 | res$alpha[pos] <- res$prior.alpha[pos] 795 | res$beta[pos] <- res$prior.beta[pos] 796 | res$tau[pos] <- 0 797 | res$poll.var[pos] <- res$pollAvg11[pos] <- res$pollAvg01[pos] <- res$pollAvg10[pos] <- res$pollAvg00[pos] <- res$pollAvg[pos] <- 100*sign(res$prior.mean[pos]) 798 | res$scale <- sqrt((res$beta / res$alpha)*(1/res$kappa)) 799 | 800 | # probability of Dem win 801 | res$prob <- pt(res$mean / res$scale, 2*res$alpha) 802 | return(list(forecast = res, 803 | poll.df = poll.df, 804 | h.eff = h.eff)) 805 | } 806 | 807 | 808 | pollPrep <- function(races, max.margin=20) { 809 | # does some things 810 | # skip unopposed races 811 | races <- races[sapply(races, function(x) length(x$result)) == 2] 812 | # restrict to "close" races 813 | pos <- sapply(races, function(x) (abs(diff(x$result)) < max.margin)) 814 | races <- races[pos] 815 | # with polling... 816 | pos <- sapply(races, function(x) !is.null(x$polls)) 817 | races <- races[pos] 818 | pos <- sapply(races, function(x) nrow(x$polls) > 0) 819 | races <- races[pos] 820 | 821 | ##### rv vs. lv ##### 822 | results <- data.frame(stringsAsFactors=F) 823 | names <- NULL 824 | for (i in seq_along(races)) { 825 | p <- races[[i]]$polls 826 | p.id <- apply(cbind(p$end_date, p$pollster), 1, function(x) paste(x, collapse="")) 827 | tab <- table(p.id) 828 | dupes <- names(tab)[tab > 1] 829 | for (d in dupes) { 830 | pos <- which(p.id == d) 831 | ord <- match(c("lv", "rv", "a"), p[pos, ]$population) 832 | # ord[is.na(ord)] <- 0 833 | results <- rbind(results, c(p[pos[1], ]$mid_date, p[pos, ]$margin[ord], 834 | races[[i]]$cycle)) 835 | names <- c(names, p[pos[1], ]$pollster) 836 | } 837 | } 838 | results <- cbind(results, names) 839 | colnames(results) <- c("mid_date", "lv", "rv", "a", "cycle", "pollster") 840 | lv.adj <<- mean(results$lv - results$rv, na.rm=T) 841 | 842 | # if both a and rv, use rv. if both a/rv and lv, use lv. 843 | for (i in seq_along(races)) { 844 | p <- races[[i]]$polls 845 | p <- p[p$population == "lv" | p$population == "rv", ] 846 | p.id <- apply(cbind(p$end_date, p$pollster), 1, function(x) paste(x, collapse="")) 847 | tab <- table(p.id) 848 | dupes <- names(tab)[tab > 1] 849 | for (d in dupes) { 850 | pos <- which(p.id == d) 851 | bad <- pos[match("rv", p[pos, ]$population)] 852 | if (is.na(bad)) { 853 | bad <- NULL 854 | } 855 | if (length(bad) > 0) { 856 | p <- p[-bad, ] 857 | p.id <- p.id[-bad] 858 | } 859 | } 860 | races[[i]]$polls <- p 861 | } 862 | 863 | # remove overlapping polls 864 | for (i in seq_along(races)) { 865 | p <- races[[i]]$polls 866 | ord <- rev(order(as.Date(p$end, format="%m/%d/%Y"))) 867 | p <- p[ord, ] 868 | bads <- NULL 869 | for (pollster in unique(p$pollster)) { 870 | pos <- which(p$pollster == pollster) 871 | if (length(pos) > 1) { 872 | starts <- as.Date(p[pos, ]$start, format="%m/%d/%Y") 873 | ends <- as.Date(p[pos, ]$end, format="%m/%d/%Y") 874 | for (j in pos) { 875 | if (j %in% bads) next 876 | k.pos <- which(starts[j] <= ends) 877 | bads <- c(bads, k.pos[k.pos > j]) 878 | } 879 | } 880 | } 881 | if (length(bads) > 0) { 882 | races[[i]]$polls <- p[-bads, ] 883 | } 884 | } 885 | for (i in seq_along(races)) { 886 | p <- races[[i]]$polls 887 | races[[i]]$polls <- p[order(p$mid_date), ] 888 | } 889 | 890 | polls <- do.call(rbind, lapply(races, function(x) x$polls)) 891 | pos <- match(sapply(strsplit(rownames(polls), ".", fixed=T), function(x) x[1]), 892 | names(races)) 893 | temp <- t(sapply(races[pos], function(x) x$result)) 894 | office <- names(races[pos]) 895 | polls <- cbind(polls, temp, office) 896 | polls$obsMargin <- polls$"1" - polls$"2" 897 | # add year 898 | polls$year <- as.numeric(sapply(strsplit(rownames(polls), ".", fixed=T), 899 | function(x) { 900 | y <- x[1] 901 | substr(y, nchar(y) - 3, nchar(y)) 902 | })) 903 | polls$pollster[polls$pollster == "CNN/Opinion Research"] <- "CNN / Opinion Research" 904 | polls <- polls[!is.na(polls$margin), ] 905 | # add field for sample variance 906 | p2 <- (100 - polls$other - polls$margin)/200 907 | p1 <- p2 + polls$margin/100 908 | # if sample size is zero, assign sample size equal to 350 909 | polls$sample_size[polls$sample_size == 0] <- 350 910 | # reduce sample size to adjust for those answering other than top two 911 | polls$sample_size <- (100 - polls$other)*polls$sample_size / 100 912 | # numerator for sampling variance: 913 | polls$sample_varnum <- (p1*(1 - p1) + p2*(1 - p2) + 2*p1*p2) 914 | # adjust the rv polls 915 | x <- polls[polls$population == "rv", ]$margin 916 | polls[polls$population == "rv", ]$margin <- x + lv.adj 917 | 918 | return(polls) 919 | } 920 | 921 | 922 | pollPrep2014 <- function(polls, lv.adj) { 923 | # for prepping 2014 polls, in form they come in from the combined csv. 924 | # needs lv adjustment to be specified (saved to global env when you calc 925 | # pollster ratings) 926 | 927 | polls$pollster <- gsub(" (D)", "", polls$pollster, fixed=T) 928 | polls$pollster <- gsub(" (R)", "", polls$pollster, fixed=T) 929 | p <- polls[polls$population == "lv" | polls$population == "rv", , drop = F] 930 | p.id <- apply(cbind(p$start_date, p$mid_date, p$pollster, p$office), 931 | 1, function(x) paste(x, collapse="")) 932 | tab <- table(p.id) 933 | dupes <- names(tab)[tab > 1] 934 | for (d in dupes) { 935 | pos <- which(p.id == d) 936 | bad <- pos[match("rv", p[pos, ]$population)] 937 | if (is.na(bad)) { 938 | bad <- NULL 939 | } 940 | if (length(bad) > 0) { 941 | p <- p[-bad, ] 942 | p.id <- p.id[-bad] 943 | } 944 | } 945 | 946 | # remove overlapping polls 947 | polls <- p[rev(order(as.Date(p$end, format="%Y-%m-%d"))), ] 948 | for (office in unique(polls$office)) { 949 | pos <- polls$office == office 950 | p <- polls[pos, ] 951 | polls <- polls[!pos, ] 952 | bads <- NULL 953 | for (pollster in unique(p$pollster)) { 954 | pos <- which(p$pollster == pollster) 955 | if (length(pos) > 1) { 956 | starts <- as.Date(p[pos, ]$start, format="%m/%d/%Y") 957 | ends <- as.Date(p[pos, ]$end, format="%m/%d/%Y") 958 | for (j in pos) { 959 | if (j %in% bads) next 960 | k.pos <- which(starts[j] <= ends) 961 | bads <- c(bads, k.pos[k.pos > j]) 962 | } 963 | } 964 | } 965 | if (length(bads) > 0) { 966 | cat(unlist(p[bads, ])) 967 | p <- p[-bads, ] 968 | } 969 | polls <- rbind(polls, p) 970 | } 971 | 972 | colnames(polls)[match("cycle", colnames(polls))] <- "year" 973 | # add field for sampling variance 974 | p2 <- (100 - polls$other - polls$margin)/200 975 | p1 <- p2 + polls$margin/100 976 | # numerator for sampling variance: 977 | polls$sample_varnum <- (p1*(1 - p1) + p2*(1 - p2) + 2*p1*p2) 978 | # if sample size is zero, assign sample size equal to 350 979 | polls$sample_size[polls$sample_size == 0] <- 350 980 | # reduce sample size to adjust for those answering other than top two 981 | polls$sample_size <- (100 - polls$other)*polls$sample_size / 100 982 | # adjust the rv polls 983 | x <- polls[polls$population == "rv", ]$margin 984 | polls[polls$population == "rv", ]$margin <- x + lv.adj 985 | 986 | return(polls) 987 | } 988 | 989 | 990 | # primaries... 991 | probGet <- function(names, pos, party, money, days, 992 | race.polls = NULL, a0 = 20, dir.sims = 100000) { 993 | n <- length(pos) 994 | primary.code <- paste0("sen_", toupper(substr(party, 1, 1)), "_p") 995 | if (n <= 1) return(1) 996 | x.fec <- money[pos] 997 | x.fec[is.na(x.fec)] <- 0 998 | fec.probs <- (x.fec + 1) / (sum(x.fec) + n) 999 | if (!is.null(race.polls)) { 1000 | start.column <- match("type", names(race.polls)) + 1 1001 | gen <- race.polls[start.column:(ncol(race.polls) - 1)] 1002 | id <- substr(rownames(gen), 1, 5) 1003 | init.probs <- colMeans(cbind(aggregate(gen, by=list(id), function(x) mean(!is.na(x)))[, -1])) 1004 | poll.probs <- init.probs[pos] 1005 | poll.probs <- nrow(gen)*poll.probs / sum(poll.probs) 1006 | flatten <- rep(1/n, n) 1007 | probs <- rowSums(cbind(poll.probs, flatten), na.rm=T) 1008 | probs <- probs / sum(probs) 1009 | } else { 1010 | probs <- rep(1/n, n) 1011 | } 1012 | probs <- (probs + 10*fec.probs) / sum(probs, 10*fec.probs) 1013 | 1014 | if (!is.null(race.polls)) { 1015 | primary <- race.polls[race.polls$branch == primary.code, ] 1016 | # then incorporate primary polling if it exists 1017 | if (nrow(primary) > 0) { 1018 | primary[is.na(primary)] <- 0 1019 | tau <- (primary$sample*timeWeight(primary$mid_date - days, days)) 1020 | tau <- tau / sum(tau) 1021 | start.column <- 1 + match("type", colnames(primary)) 1022 | if (race.name == "tx-senate-class-ii-2014" & primary.code == "sen_D_p") { 1023 | election <- primary[1, ] 1024 | election[start.column:(ncol(election) - 1)] <- 0 1025 | election$"david-alameel" <- 47.1 1026 | election$"kesha-rogers" <- 21.7 1027 | election$"maxey-scherr" <- 17.7 1028 | election$"harry-kim" <- 8.9 1029 | election$"mike-fjetland" <- 4.6 1030 | primary <- rbind(primary, election) 1031 | tau <- c(tau, 3) 1032 | } 1033 | # get party 1034 | p <- rbind(matrix(as.numeric(unlist(primary[, start.column:(ncol(primary) - 1)])), 1035 | nrow=length(tau))[, pos]) 1036 | est <- colSums(rbind(p*tau)) 1037 | est <- 4*est / sum(est) 1038 | probs <- (est + probs) / sum(est + probs) 1039 | # simulate to get win probabilities from vote estimates 1040 | results <- table(max.col(rdirichlet(dir.sims, a0*probs))) 1041 | probs <- 0*probs 1042 | probs[as.numeric(names(results))] <- results / sum(results) 1043 | } 1044 | } 1045 | # flatten. 1046 | probs <- probs + .001 1047 | s <- x$serious[pos] + (1 - max(x$serious[pos])) 1048 | probs*s / sum(probs*s) 1049 | } 1050 | 1051 | 1052 | slugMake <- function(string) { 1053 | # convert to lowercase, replace spaces with hyphens and delete all but 1054 | # hyphens and alphanumeric characters 1055 | string <- tolower(gsub(" ", "-", string)) 1056 | return(gsub("[^\\w\\-]", "", string, perl=T)) 1057 | } 1058 | 1059 | 1060 | textify <- function(n, d = 1) { 1061 | n <- d*round(n / d) 1062 | ones <- c("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine") 1063 | return( 1064 | if (n < 10 & n == round(n)) { 1065 | ones[n + 1] 1066 | } else { 1067 | as.character(n) 1068 | }) 1069 | } 1070 | 1071 | 1072 | timeWeight <- function(days.old, h=NA) { 1073 | # h gives days until election. used to specify poll half-life. as election day 1074 | # gets closer, polls become "stale" more quickly 1075 | if (any(is.na(h))) { 1076 | h <- -as.numeric(electionDay(2014) - Sys.Date()) 1077 | } 1078 | h <- -abs(h) 1079 | h <- min(h, -6) 1080 | h <- 140/(20.945732*1.0075898097^h) 1081 | # h determines half-life of poll in days 1082 | # ensure days.old is positive 1083 | days.old <- abs(days.old) 1084 | 1 / 2^(as.numeric(days.old)/h) 1085 | } 1086 | 1087 | 1088 | # remove leading/trailing whitespace from string 1089 | trim <- function(x) gsub("^\\s+|\\s+$", "", x) 1090 | 1091 | 1092 | 1093 | 1094 | -------------------------------------------------------------------------------- /model/data/misc/races.csv: -------------------------------------------------------------------------------- 1 | year,class,state,special notes,wikipediaLink,postal,office,, 1992,iii,Alabama,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alabama,_1992",AL,al-senate-class-iii-1992,, 1992,iii,Alaska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alaska,_1992",AK,ak-senate-class-iii-1992,, 1992,iii,Arizona,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arizona,_1992",AZ,az-senate-class-iii-1992,, 1992,iii,Arkansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arkansas,_1992",AR,ar-senate-class-iii-1992,, 1992,i,California,Special: Class 1,"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_California,_1992",CA,ca-senate-class-i-1992,seymour,done 1992,iii,California,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_California,_1992",CA,ca-senate-class-iii-1992,, 1992,iii,Colorado,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Colorado,_1992",CO,co-senate-class-iii-1992,, 1992,iii,Connecticut,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Connecticut,_1992",CT,ct-senate-class-iii-1992,, 1992,iii,Florida,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Florida,_1992",FL,fl-senate-class-iii-1992,, 1992,iii,Georgia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Georgia,_1992",GA,ga-senate-class-iii-1992,, 1992,iii,Hawaii,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Hawaii,_1992",HI,hi-senate-class-iii-1992,, 1992,iii,Idaho,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Idaho,_1992",ID,id-senate-class-iii-1992,, 1992,iii,Illinois,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Illinois,_1992",IL,il-senate-class-iii-1992,, 1992,iii,Indiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Indiana,_1992",IN,in-senate-class-iii-1992,, 1992,iii,Iowa,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Iowa,_1992",IA,ia-senate-class-iii-1992,, 1992,iii,Kansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kansas,_1992",KS,ks-senate-class-iii-1992,, 1992,iii,Kentucky,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kentucky,_1992",KY,ky-senate-class-iii-1992,, 1992,iii,Louisiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Louisiana,_1992",LA,la-senate-class-iii-1992,, 1992,iii,Maryland,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maryland,_1992",MD,md-senate-class-iii-1992,, 1992,iii,Missouri,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Missouri,_1992",MO,mo-senate-class-iii-1992,, 1992,iii,Nevada,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nevada,_1992",NV,nv-senate-class-iii-1992,, 1992,iii,New Hampshire,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Hampshire,_1992",NH,nh-senate-class-iii-1992,, 1992,iii,New York,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_York,_1992",NY,ny-senate-class-iii-1992,, 1992,iii,North Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Carolina,_1992",NC,nc-senate-class-iii-1992,, 1992,iii,North Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Dakota,_1992",ND,nd-senate-class-iii-1992,, 1992,i,North Dakota,Special: Class 1,"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_North_Dakota,_1992",ND,nd-senate-class-i-1992,, 1992,iii,Ohio,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Ohio,_1992",OH,oh-senate-class-iii-1992,, 1992,iii,Oklahoma,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oklahoma,_1992",OK,ok-senate-class-iii-1992,, 1992,iii,Oregon,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oregon,_1992",OR,or-senate-class-iii-1992,, 1992,iii,Pennsylvania,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Pennsylvania,_1992",PA,pa-senate-class-iii-1992,, 1992,iii,South Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Carolina,_1992",SC,sc-senate-class-iii-1992,, 1992,iii,South Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Dakota,_1992",SD,sd-senate-class-iii-1992,, 1992,iii,Utah,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Utah,_1992",UT,ut-senate-class-iii-1992,, 1992,iii,Vermont,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Vermont,_1992",VT,vt-senate-class-iii-1992,, 1992,iii,Washington,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Washington,_1992",WA,wa-senate-class-iii-1992,, 1992,iii,Wisconsin,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wisconsin,_1992",WI,wi-senate-class-iii-1992,, 1994,i,Arizona,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arizona,_1994",AZ,az-senate-class-i-1994,, 1994,i,California,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_California,_1994",CA,ca-senate-class-i-1994,, 1994,i,Connecticut,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Connecticut,_1994",CT,ct-senate-class-i-1994,, 1994,i,Delaware,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Delaware,_1994",DE,de-senate-class-i-1994,, 1994,i,Florida,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Florida,_1994",FL,fl-senate-class-i-1994,, 1994,i,Hawaii,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Hawaii,_1994",HI,hi-senate-class-i-1994,, 1994,i,Indiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Indiana,_1994",IN,in-senate-class-i-1994,, 1994,i,Maine,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maine,_1994",ME,me-senate-class-i-1994,, 1994,i,Maryland,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maryland,_1994",MD,md-senate-class-i-1994,, 1994,i,Massachusetts,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Massachusetts,_1994",MA,ma-senate-class-i-1994,, 1994,i,Michigan,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Michigan,_1994",MI,mi-senate-class-i-1994,, 1994,i,Minnesota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Minnesota,_1994",MN,mn-senate-class-i-1994,, 1994,i,Mississippi,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Mississippi,_1994",MS,ms-senate-class-i-1994,, 1994,i,Missouri,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Missouri,_1994",MO,mo-senate-class-i-1994,, 1994,i,Montana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Montana,_1994",MT,mt-senate-class-i-1994,, 1994,i,Nebraska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nebraska,_1994",NE,ne-senate-class-i-1994,, 1994,i,Nevada,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nevada,_1994",NV,nv-senate-class-i-1994,, 1994,i,New Jersey,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Jersey,_1994",NJ,nj-senate-class-i-1994,, 1994,i,New Mexico,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Mexico,_1994",NM,nm-senate-class-i-1994,, 1994,i,New York,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_York,_1994",NY,ny-senate-class-i-1994,, 1994,i,North Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Dakota,_1994",ND,nd-senate-class-i-1994,, 1994,i,Ohio,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Ohio,_1994",OH,oh-senate-class-i-1994,, 1994,ii,Oklahoma,Special: Class 2,"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_Oklahoma,_1994",OK,ok-senate-class-ii-1994,no incumbent,done 1994,i,Pennsylvania,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Pennsylvania,_1994",PA,pa-senate-class-i-1994,, 1994,i,Rhode Island,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Rhode_Island,_1994",RI,ri-senate-class-i-1994,, 1994,i,Tennessee,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Tennessee,_1994",TN,tn-senate-class-i-1994,, 1994,ii,Tennessee,Special: Class 2,"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_Tennessee,_1994",TN,tn-senate-class-ii-1994,no incumbent,done 1994,i,Texas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Texas,_1994",TX,tx-senate-class-i-1994,, 1994,i,Utah,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Utah,_1994",UT,ut-senate-class-i-1994,, 1994,i,Vermont,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Vermont,_1994",VT,vt-senate-class-i-1994,, 1994,i,Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Virginia,_1994",VA,va-senate-class-i-1994,, 1994,i,Washington,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Washington,_1994",WA,wa-senate-class-i-1994,, 1994,i,West Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_West_Virginia,_1994",WV,wv-senate-class-i-1994,, 1994,i,Wisconsin,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wisconsin,_1994",WI,wi-senate-class-i-1994,, 1994,i,Wyoming,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wyoming,_1994",WY,wy-senate-class-i-1994,, 1996,ii,Alabama,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alabama,_1996",AL,al-senate-class-ii-1996,, 1996,ii,Alaska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alaska,_1996",AK,ak-senate-class-ii-1996,, 1996,ii,Arkansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arkansas,_1996",AR,ar-senate-class-ii-1996,, 1996,ii,Colorado,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Colorado,_1996",CO,co-senate-class-ii-1996,, 1996,ii,Delaware,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Delaware,_1996",DE,de-senate-class-ii-1996,, 1996,ii,Georgia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Georgia,_1996",GA,ga-senate-class-ii-1996,, 1996,ii,Idaho,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Idaho,_1996",ID,id-senate-class-ii-1996,, 1996,ii,Illinois,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Illinois,_1996",IL,il-senate-class-ii-1996,, 1996,ii,Iowa,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Iowa,_1996",IA,ia-senate-class-ii-1996,, 1996,ii,Kansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kansas,_1996",KS,ks-senate-class-ii-1996,, 1996,iii,Kansas,Special: Class 3,"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_Kansas,_1996",KS,ks-senate-class-iii-1996,no incumbent,done 1996,ii,Kentucky,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kentucky,_1996",KY,ky-senate-class-ii-1996,, 1996,ii,Louisiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Louisiana,_1996",LA,la-senate-class-ii-1996,, 1996,ii,Maine,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maine,_1996",ME,me-senate-class-ii-1996,, 1996,ii,Massachusetts,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Massachusetts,_1996",MA,ma-senate-class-ii-1996,, 1996,ii,Michigan,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Michigan,_1996",MI,mi-senate-class-ii-1996,, 1996,ii,Minnesota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Minnesota,_1996",MN,mn-senate-class-ii-1996,, 1996,ii,Mississippi,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Mississippi,_1996",MS,ms-senate-class-ii-1996,, 1996,ii,Montana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Montana,_1996",MT,mt-senate-class-ii-1996,, 1996,ii,Nebraska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nebraska,_1996",NE,ne-senate-class-ii-1996,, 1996,ii,New Hampshire,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Hampshire,_1996",NH,nh-senate-class-ii-1996,, 1996,ii,New Jersey,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Jersey,_1996",NJ,nj-senate-class-ii-1996,, 1996,ii,New Mexico,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Mexico,_1996",NM,nm-senate-class-ii-1996,, 1996,ii,North Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Carolina,_1996",NC,nc-senate-class-ii-1996,, 1996,ii,Oklahoma,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oklahoma,_1996",OK,ok-senate-class-ii-1996,, 1996,ii,Oregon,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oregon,_1996",OR,or-senate-class-ii-1996,, 1996,ii,Rhode Island,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Rhode_Island,_1996",RI,ri-senate-class-ii-1996,, 1996,ii,South Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Carolina,_1996",SC,sc-senate-class-ii-1996,, 1996,ii,South Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Dakota,_1996",SD,sd-senate-class-ii-1996,, 1996,ii,Tennessee,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Tennessee,_1996",TN,tn-senate-class-ii-1996,, 1996,ii,Texas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Texas,_1996",TX,tx-senate-class-ii-1996,, 1996,ii,Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Virginia,_1996",VA,va-senate-class-ii-1996,, 1996,ii,West Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_West_Virginia,_1996",WV,wv-senate-class-ii-1996,, 1996,ii,Wyoming,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wyoming,_1996",WY,wy-senate-class-ii-1996,, 1998,iii,Alabama,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alabama,_1998",AL,al-senate-class-iii-1998,, 1998,iii,Alaska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alaska,_1998",AK,ak-senate-class-iii-1998,, 1998,iii,Arizona,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arizona,_1998",AZ,az-senate-class-iii-1998,, 1998,iii,Arkansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arkansas,_1998",AR,ar-senate-class-iii-1998,, 1998,iii,California,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_California,_1998",CA,ca-senate-class-iii-1998,, 1998,iii,Colorado,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Colorado,_1998",CO,co-senate-class-iii-1998,, 1998,iii,Connecticut,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Connecticut,_1998",CT,ct-senate-class-iii-1998,, 1998,iii,Florida,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Florida,_1998",FL,fl-senate-class-iii-1998,, 1998,iii,Georgia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Georgia,_1998",GA,ga-senate-class-iii-1998,, 1998,iii,Hawaii,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Hawaii,_1998",HI,hi-senate-class-iii-1998,, 1998,iii,Idaho,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Idaho,_1998",ID,id-senate-class-iii-1998,, 1998,iii,Illinois,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Illinois,_1998",IL,il-senate-class-iii-1998,, 1998,iii,Indiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Indiana,_1998",IN,in-senate-class-iii-1998,, 1998,iii,Iowa,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Iowa,_1998",IA,ia-senate-class-iii-1998,, 1998,iii,Kansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kansas,_1998",KS,ks-senate-class-iii-1998,, 1998,iii,Kentucky,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kentucky,_1998",KY,ky-senate-class-iii-1998,, 1998,iii,Louisiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Louisiana,_1998",LA,la-senate-class-iii-1998,, 1998,iii,Maryland,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maryland,_1998",MD,md-senate-class-iii-1998,, 1998,iii,Missouri,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Missouri,_1998",MO,mo-senate-class-iii-1998,, 1998,iii,Nevada,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nevada,_1998",NV,nv-senate-class-iii-1998,, 1998,iii,New Hampshire,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Hampshire,_1998",NH,nh-senate-class-iii-1998,, 1998,iii,New York,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_York,_1998",NY,ny-senate-class-iii-1998,, 1998,iii,North Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Carolina,_1998",NC,nc-senate-class-iii-1998,, 1998,iii,North Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Dakota,_1998",ND,nd-senate-class-iii-1998,, 1998,iii,Ohio,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Ohio,_1998",OH,oh-senate-class-iii-1998,, 1998,iii,Oklahoma,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oklahoma,_1998",OK,ok-senate-class-iii-1998,, 1998,iii,Oregon,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oregon,_1998",OR,or-senate-class-iii-1998,, 1998,iii,Pennsylvania,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Pennsylvania,_1998",PA,pa-senate-class-iii-1998,, 1998,iii,South Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Carolina,_1998",SC,sc-senate-class-iii-1998,, 1998,iii,South Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Dakota,_1998",SD,sd-senate-class-iii-1998,, 1998,iii,Utah,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Utah,_1998",UT,ut-senate-class-iii-1998,, 1998,iii,Vermont,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Vermont,_1998",VT,vt-senate-class-iii-1998,, 1998,iii,Washington,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Washington,_1998",WA,wa-senate-class-iii-1998,, 1998,iii,Wisconsin,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wisconsin,_1998",WI,wi-senate-class-iii-1998,, 2000,i,Arizona,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arizona,_2000",AZ,az-senate-class-i-2000,, 2000,i,California,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_California,_2000",CA,ca-senate-class-i-2000,, 2000,i,Connecticut,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Connecticut,_2000",CT,ct-senate-class-i-2000,, 2000,i,Delaware,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Delaware,_2000",DE,de-senate-class-i-2000,, 2000,i,Florida,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Florida,_2000",FL,fl-senate-class-i-2000,, 2000,iii,Georgia,Class 3: Special,"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_Georgia,_2000",GA,ga-senate-class-iii-2000,zell miller,done 2000,i,Hawaii,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Hawaii,_2000",HI,hi-senate-class-i-2000,, 2000,i,Indiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Indiana,_2000",IN,in-senate-class-i-2000,, 2000,i,Maine,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maine,_2000",ME,me-senate-class-i-2000,, 2000,i,Maryland,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maryland,_2000",MD,md-senate-class-i-2000,, 2000,i,Massachusetts,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Massachusetts,_2000",MA,ma-senate-class-i-2000,, 2000,i,Michigan,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Michigan,_2000",MI,mi-senate-class-i-2000,, 2000,i,Minnesota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Minnesota,_2000",MN,mn-senate-class-i-2000,, 2000,i,Mississippi,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Mississippi,_2000",MS,ms-senate-class-i-2000,, 2000,i,Missouri,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Missouri,_2000",MO,mo-senate-class-i-2000,, 2000,i,Montana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Montana,_2000",MT,mt-senate-class-i-2000,, 2000,i,Nebraska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nebraska,_2000",NE,ne-senate-class-i-2000,, 2000,i,Nevada,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nevada,_2000",NV,nv-senate-class-i-2000,, 2000,i,New Jersey,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Jersey,_2000",NJ,nj-senate-class-i-2000,, 2000,i,New Mexico,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Mexico,_2000",NM,nm-senate-class-i-2000,, 2000,i,New York,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_York,_2000",NY,ny-senate-class-i-2000,, 2000,i,North Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Dakota,_2000",ND,nd-senate-class-i-2000,, 2000,i,Ohio,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Ohio,_2000",OH,oh-senate-class-i-2000,, 2000,i,Pennsylvania,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Pennsylvania,_2000",PA,pa-senate-class-i-2000,, 2000,i,Rhode Island,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Rhode_Island,_2000",RI,ri-senate-class-i-2000,, 2000,i,Tennessee,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Tennessee,_2000",TN,tn-senate-class-i-2000,, 2000,i,Texas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Texas,_2000",TX,tx-senate-class-i-2000,, 2000,i,Utah,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Utah,_2000",UT,ut-senate-class-i-2000,, 2000,i,Vermont,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Vermont,_2000",VT,vt-senate-class-i-2000,, 2000,i,Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Virginia,_2000",VA,va-senate-class-i-2000,, 2000,i,Washington,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Washington,_2000",WA,wa-senate-class-i-2000,, 2000,i,West Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_West_Virginia,_2000",WV,wv-senate-class-i-2000,, 2000,i,Wisconsin,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wisconsin,_2000",WI,wi-senate-class-i-2000,, 2000,i,Wyoming,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wyoming,_2000",WY,wy-senate-class-i-2000,, 2002,ii,Alabama,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alabama,_2002",AL,al-senate-class-ii-2002,, 2002,ii,Alaska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alaska,_2002",AK,ak-senate-class-ii-2002,, 2002,ii,Arkansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arkansas,_2002",AR,ar-senate-class-ii-2002,, 2002,ii,Colorado,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Colorado,_2002",CO,co-senate-class-ii-2002,, 2002,ii,Delaware,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Delaware,_2002",DE,de-senate-class-ii-2002,, 2002,ii,Georgia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Georgia,_2002",GA,ga-senate-class-ii-2002,, 2002,ii,Idaho,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Idaho,_2002",ID,id-senate-class-ii-2002,, 2002,ii,Illinois,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Illinois,_2002",IL,il-senate-class-ii-2002,, 2002,ii,Iowa,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Iowa,_2002",IA,ia-senate-class-ii-2002,, 2002,ii,Kansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kansas,_2002",KS,ks-senate-class-ii-2002,, 2002,ii,Kentucky,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kentucky,_2002",KY,ky-senate-class-ii-2002,, 2002,ii,Louisiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Louisiana,_2002",LA,la-senate-class-ii-2002,, 2002,ii,Maine,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maine,_2002",ME,me-senate-class-ii-2002,, 2002,ii,Massachusetts,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Massachusetts,_2002",MA,ma-senate-class-ii-2002,, 2002,ii,Michigan,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Michigan,_2002",MI,mi-senate-class-ii-2002,, 2002,ii,Minnesota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Minnesota,_2002",MN,mn-senate-class-ii-2002,, 2002,ii,Mississippi,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Mississippi,_2002",MS,ms-senate-class-ii-2002,, 2002,i,Missouri,(Class 1: Special),"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_Missouri,_2002",MO,mo-senate-class-i-2002,carnahan,done 2002,ii,Montana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Montana,_2002",MT,mt-senate-class-ii-2002,, 2002,ii,Nebraska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nebraska,_2002",NE,ne-senate-class-ii-2002,, 2002,ii,New Hampshire,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Hampshire,_2002",NH,nh-senate-class-ii-2002,, 2002,ii,New Jersey,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Jersey,_2002",NJ,nj-senate-class-ii-2002,, 2002,ii,New Mexico,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Mexico,_2002",NM,nm-senate-class-ii-2002,, 2002,ii,North Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Carolina,_2002",NC,nc-senate-class-ii-2002,, 2002,ii,Oklahoma,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oklahoma,_2002",OK,ok-senate-class-ii-2002,, 2002,ii,Oregon,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oregon,_2002",OR,or-senate-class-ii-2002,, 2002,ii,Rhode Island,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Rhode_Island,_2002",RI,ri-senate-class-ii-2002,, 2002,ii,South Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Carolina,_2002",SC,sc-senate-class-ii-2002,, 2002,ii,South Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Dakota,_2002",SD,sd-senate-class-ii-2002,, 2002,ii,Tennessee,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Tennessee,_2002",TN,tn-senate-class-ii-2002,, 2002,ii,Texas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Texas,_2002",TX,tx-senate-class-ii-2002,, 2002,ii,Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Virginia,_2002",VA,va-senate-class-ii-2002,, 2002,ii,West Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_West_Virginia,_2002",WV,wv-senate-class-ii-2002,, 2002,ii,Wyoming,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wyoming,_2002",WY,wy-senate-class-ii-2002,, 2004,iii,Alabama,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alabama,_2004",AL,al-senate-class-iii-2004,, 2004,iii,Alaska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alaska,_2004",AK,ak-senate-class-iii-2004,, 2004,iii,Arizona,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arizona,_2004",AZ,az-senate-class-iii-2004,, 2004,iii,Arkansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arkansas,_2004",AR,ar-senate-class-iii-2004,, 2004,iii,California,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_California,_2004",CA,ca-senate-class-iii-2004,, 2004,iii,Colorado,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Colorado,_2004",CO,co-senate-class-iii-2004,, 2004,iii,Connecticut,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Connecticut,_2004",CT,ct-senate-class-iii-2004,, 2004,iii,Florida,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Florida,_2004",FL,fl-senate-class-iii-2004,, 2004,iii,Georgia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Georgia,_2004",GA,ga-senate-class-iii-2004,, 2004,iii,Hawaii,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Hawaii,_2004",HI,hi-senate-class-iii-2004,, 2004,iii,Idaho,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Idaho,_2004",ID,id-senate-class-iii-2004,, 2004,iii,Illinois,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Illinois,_2004",IL,il-senate-class-iii-2004,, 2004,iii,Indiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Indiana,_2004",IN,in-senate-class-iii-2004,, 2004,iii,Iowa,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Iowa,_2004",IA,ia-senate-class-iii-2004,, 2004,iii,Kansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kansas,_2004",KS,ks-senate-class-iii-2004,, 2004,iii,Kentucky,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kentucky,_2004",KY,ky-senate-class-iii-2004,, 2004,iii,Louisiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Louisiana,_2004",LA,la-senate-class-iii-2004,, 2004,iii,Maryland,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maryland,_2004",MD,md-senate-class-iii-2004,, 2004,iii,Missouri,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Missouri,_2004",MO,mo-senate-class-iii-2004,, 2004,iii,Nevada,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nevada,_2004",NV,nv-senate-class-iii-2004,, 2004,iii,New Hampshire,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Hampshire,_2004",NH,nh-senate-class-iii-2004,, 2004,iii,New York,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_York,_2004",NY,ny-senate-class-iii-2004,, 2004,iii,North Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Carolina,_2004",NC,nc-senate-class-iii-2004,, 2004,iii,North Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Dakota,_2004",ND,nd-senate-class-iii-2004,, 2004,iii,Ohio,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Ohio,_2004",OH,oh-senate-class-iii-2004,, 2004,iii,Oklahoma,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oklahoma,_2004",OK,ok-senate-class-iii-2004,, 2004,iii,Oregon,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oregon,_2004",OR,or-senate-class-iii-2004,, 2004,iii,Pennsylvania,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Pennsylvania,_2004",PA,pa-senate-class-iii-2004,, 2004,iii,South Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Carolina,_2004",SC,sc-senate-class-iii-2004,, 2004,iii,South Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Dakota,_2004",SD,sd-senate-class-iii-2004,, 2004,iii,Utah,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Utah,_2004",UT,ut-senate-class-iii-2004,, 2004,iii,Vermont,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Vermont,_2004",VT,vt-senate-class-iii-2004,, 2004,iii,Washington,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Washington,_2004",WA,wa-senate-class-iii-2004,, 2004,iii,Wisconsin,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wisconsin,_2004",WI,wi-senate-class-iii-2004,, 2006,i,Arizona,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arizona,_2006",AZ,az-senate-class-i-2006,, 2006,i,California,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_California,_2006",CA,ca-senate-class-i-2006,, 2006,i,Connecticut,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Connecticut,_2006",CT,ct-senate-class-i-2006,, 2006,i,Delaware,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Delaware,_2006",DE,de-senate-class-i-2006,, 2006,i,Florida,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Florida,_2006",FL,fl-senate-class-i-2006,, 2006,i,Hawaii,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Hawaii,_2006",HI,hi-senate-class-i-2006,, 2006,i,Indiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Indiana,_2006",IN,in-senate-class-i-2006,, 2006,i,Maine,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maine,_2006",ME,me-senate-class-i-2006,, 2006,i,Maryland,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maryland,_2006",MD,md-senate-class-i-2006,, 2006,i,Massachusetts,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Massachusetts,_2006",MA,ma-senate-class-i-2006,, 2006,i,Michigan,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Michigan,_2006",MI,mi-senate-class-i-2006,, 2006,i,Minnesota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Minnesota,_2006",MN,mn-senate-class-i-2006,, 2006,i,Mississippi,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Mississippi,_2006",MS,ms-senate-class-i-2006,, 2006,i,Missouri,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Missouri,_2006",MO,mo-senate-class-i-2006,, 2006,i,Montana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Montana,_2006",MT,mt-senate-class-i-2006,, 2006,i,Nebraska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nebraska,_2006",NE,ne-senate-class-i-2006,, 2006,i,Nevada,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nevada,_2006",NV,nv-senate-class-i-2006,, 2006,i,New Jersey,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Jersey,_2006",NJ,nj-senate-class-i-2006,, 2006,i,New Mexico,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Mexico,_2006",NM,nm-senate-class-i-2006,, 2006,i,New York,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_York,_2006",NY,ny-senate-class-i-2006,, 2006,i,North Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Dakota,_2006",ND,nd-senate-class-i-2006,, 2006,i,Ohio,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Ohio,_2006",OH,oh-senate-class-i-2006,, 2006,i,Pennsylvania,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Pennsylvania,_2006",PA,pa-senate-class-i-2006,, 2006,i,Rhode Island,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Rhode_Island,_2006",RI,ri-senate-class-i-2006,, 2006,i,Tennessee,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Tennessee,_2006",TN,tn-senate-class-i-2006,, 2006,i,Texas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Texas,_2006",TX,tx-senate-class-i-2006,, 2006,i,Utah,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Utah,_2006",UT,ut-senate-class-i-2006,, 2006,i,Vermont,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Vermont,_2006",VT,vt-senate-class-i-2006,, 2006,i,Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Virginia,_2006",VA,va-senate-class-i-2006,, 2006,i,Washington,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Washington,_2006",WA,wa-senate-class-i-2006,, 2006,i,West Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_West_Virginia,_2006",WV,wv-senate-class-i-2006,, 2006,i,Wisconsin,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wisconsin,_2006",WI,wi-senate-class-i-2006,, 2006,i,Wyoming,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wyoming,_2006",WY,wy-senate-class-i-2006,, 2008,ii,Alabama,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",AL,al-senate-class-ii-2008,, 2008,ii,Alaska,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",AK,ak-senate-class-ii-2008,, 2008,ii,Arkansas,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",AR,ar-senate-class-ii-2008,, 2008,ii,Colorado,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",CO,co-senate-class-ii-2008,, 2008,ii,Delaware,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",DE,de-senate-class-ii-2008,, 2008,ii,Georgia,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",GA,ga-senate-class-ii-2008,, 2008,ii,Idaho,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",ID,id-senate-class-ii-2008,, 2008,ii,Illinois,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",IL,il-senate-class-ii-2008,, 2008,ii,Iowa,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",IA,ia-senate-class-ii-2008,, 2008,ii,Kansas,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",KS,ks-senate-class-ii-2008,, 2008,ii,Kentucky,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",KY,ky-senate-class-ii-2008,, 2008,ii,Louisiana,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",LA,la-senate-class-ii-2008,, 2008,ii,Maine,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",ME,me-senate-class-ii-2008,, 2008,ii,Massachusetts,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",MA,ma-senate-class-ii-2008,, 2008,ii,Michigan,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",MI,mi-senate-class-ii-2008,, 2008,ii,Minnesota,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",MN,mn-senate-class-ii-2008,, 2008,ii,Mississippi,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",MS,ms-senate-class-ii-2008,, 2008,i,Mississippi,(Special: Class 1),"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",MS,ms-senate-class-i-2008,wicker,done 2008,ii,Montana,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",MT,mt-senate-class-ii-2008,, 2008,ii,Nebraska,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",NE,ne-senate-class-ii-2008,, 2008,ii,New Hampshire,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",NH,nh-senate-class-ii-2008,, 2008,ii,New Jersey,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",NJ,nj-senate-class-ii-2008,, 2008,ii,New Mexico,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",NM,nm-senate-class-ii-2008,, 2008,ii,North Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",NC,nc-senate-class-ii-2008,, 2008,ii,Oklahoma,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",OK,ok-senate-class-ii-2008,, 2008,ii,Oregon,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",OR,or-senate-class-ii-2008,, 2008,ii,Rhode Island,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",RI,ri-senate-class-ii-2008,, 2008,ii,South Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",SC,sc-senate-class-ii-2008,, 2008,ii,South Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",SD,sd-senate-class-ii-2008,, 2008,ii,Tennessee,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",TN,tn-senate-class-ii-2008,, 2008,ii,Texas,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",TX,tx-senate-class-ii-2008,, 2008,ii,Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",VA,va-senate-class-ii-2008,, 2008,ii,West Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",WV,wv-senate-class-ii-2008,, 2008,ii,Wyoming,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",WY,wy-senate-class-ii-2008,, 2008,i,Wyoming,(Special: Class 1),"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2008",WY,wy-senate-class-i-2008,barrasso,done 2010,iii,Alabama,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alabama,_2010",AL,al-senate-class-iii-2010,, 2010,iii,Alaska,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Alaska,_2010",AK,ak-senate-class-iii-2010,, 2010,iii,Arizona,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arizona,_2010",AZ,az-senate-class-iii-2010,, 2010,iii,Arkansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Arkansas,_2010",AR,ar-senate-class-iii-2010,, 2010,iii,California,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_California,_2010",CA,ca-senate-class-iii-2010,, 2010,iii,Colorado,(Class 3: Special) and (Class 3: General),"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Colorado,_2010",CO,co-senate-class-iii-2010,,not actually special? 2010,iii,Connecticut,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Connecticut,_2010",CT,ct-senate-class-iii-2010,, 2010,ii,Delaware,(Class 2: Special),"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_Delaware,_2010",DE,de-senate-class-ii-2010,,done 2010,iii,Florida,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Florida,_2010",FL,fl-senate-class-iii-2010,, 2010,iii,Georgia,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Georgia,_2010",GA,ga-senate-class-iii-2010,, 2010,iii,Hawaii,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Hawaii,_2010",HI,hi-senate-class-iii-2010,, 2010,iii,Idaho,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Idaho,_2010",ID,id-senate-class-iii-2010,, 2010,iii,Illinois,(Class 3: Special) and (Class 3: General),"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Illinois,_2010",IL,il-senate-class-iii-2010,,not actually special? 2010,iii,Indiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Indiana,_2010",IN,in-senate-class-iii-2010,, 2010,iii,Iowa,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Iowa,_2010",IA,ia-senate-class-iii-2010,, 2010,iii,Kansas,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kansas,_2010",KS,ks-senate-class-iii-2010,, 2010,iii,Kentucky,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Kentucky,_2010",KY,ky-senate-class-iii-2010,, 2010,iii,Louisiana,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Louisiana,_2010",LA,la-senate-class-iii-2010,, 2010,iii,Maryland,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Maryland,_2010",MD,md-senate-class-iii-2010,, 2010,iii,Missouri,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Missouri,_2010",MO,mo-senate-class-iii-2010,, 2010,iii,Nevada,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Nevada,_2010",NV,nv-senate-class-iii-2010,, 2010,iii,New Hampshire,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_Hampshire,_2010",NH,nh-senate-class-iii-2010,, 2010,i,New York,(Class 1: Special),"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_New_York,_2010",NY,ny-senate-class-i-2010,gillibrand,done 2010,iii,New York,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_New_York,_2010",NY,ny-senate-class-iii-2010,, 2010,iii,North Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Carolina,_2010",NC,nc-senate-class-iii-2010,, 2010,iii,North Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_North_Dakota,_2010",ND,nd-senate-class-iii-2010,, 2010,iii,Ohio,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Ohio,_2010",OH,oh-senate-class-iii-2010,, 2010,iii,Oklahoma,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oklahoma,_2010",OK,ok-senate-class-iii-2010,, 2010,iii,Oregon,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Oregon,_2010",OR,or-senate-class-iii-2010,, 2010,iii,Pennsylvania,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Pennsylvania,_2010",PA,pa-senate-class-iii-2010,, 2010,iii,South Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Carolina,_2010",SC,sc-senate-class-iii-2010,, 2010,iii,South Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_South_Dakota,_2010",SD,sd-senate-class-iii-2010,, 2010,iii,Utah,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Utah,_2010",UT,ut-senate-class-iii-2010,, 2010,iii,Vermont,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Vermont,_2010",VT,vt-senate-class-iii-2010,, 2010,iii,Washington,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Washington,_2010",WA,wa-senate-class-iii-2010,, 2010,i,West Virginia,(Class 1: Special),"http://en.wikipedia.org/wiki/United_States_Senate_special_election_in_West_Virginia,_2010",WV,wv-senate-class-i-2010,,done 2010,iii,Wisconsin,,"http://en.wikipedia.org/wiki/United_States_Senate_election_in_Wisconsin,_2010",WI,wi-senate-class-iii-2010,, 2012,i,Arizona,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",AZ,az-senate-class-i-2012,, 2012,i,California,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",CA,ca-senate-class-i-2012,, 2012,i,Connecticut,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",CT,ct-senate-class-i-2012,, 2012,i,Delaware,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",DE,de-senate-class-i-2012,, 2012,i,Florida,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",FL,fl-senate-class-i-2012,, 2012,i,Hawaii,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",HI,hi-senate-class-i-2012,, 2012,i,Indiana,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",IN,in-senate-class-i-2012,, 2012,i,Maine,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",ME,me-senate-class-i-2012,, 2012,i,Maryland,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",MD,md-senate-class-i-2012,, 2012,i,Massachusetts,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",MA,ma-senate-class-i-2012,, 2012,i,Michigan,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",MI,mi-senate-class-i-2012,, 2012,i,Minnesota,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",MN,mn-senate-class-i-2012,, 2012,i,Mississippi,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",MS,ms-senate-class-i-2012,, 2012,i,Missouri,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",MO,mo-senate-class-i-2012,, 2012,i,Montana,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",MT,mt-senate-class-i-2012,, 2012,i,Nebraska,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",NE,ne-senate-class-i-2012,, 2012,i,Nevada,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",NV,nv-senate-class-i-2012,, 2012,i,New Jersey,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",NJ,nj-senate-class-i-2012,, 2012,i,New Mexico,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",NM,nm-senate-class-i-2012,, 2012,i,New York,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",NY,ny-senate-class-i-2012,, 2012,i,North Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",ND,nd-senate-class-i-2012,, 2012,i,Ohio,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",OH,oh-senate-class-i-2012,, 2012,i,Pennsylvania,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",PA,pa-senate-class-i-2012,, 2012,i,Rhode Island,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",RI,ri-senate-class-i-2012,, 2012,i,Tennessee,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",TN,tn-senate-class-i-2012,, 2012,i,Texas,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",TX,tx-senate-class-i-2012,, 2012,i,Utah,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",UT,ut-senate-class-i-2012,, 2012,i,Vermont,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",VT,vt-senate-class-i-2012,, 2012,i,Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",VA,va-senate-class-i-2012,, 2012,i,Washington,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",WA,wa-senate-class-i-2012,, 2012,i,West Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",WV,wv-senate-class-i-2012,, 2012,i,Wisconsin,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",WI,wi-senate-class-i-2012,, 2012,i,Wyoming,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2012",WY,wy-senate-class-i-2012,, 2014,ii,Alabama,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",AL,al-senate-class-ii-2014,, 2014,ii,Alaska,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",AK,ak-senate-class-ii-2014,, 2014,ii,Arkansas,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",AR,ar-senate-class-ii-2014,, 2014,ii,Colorado,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",CO,co-senate-class-ii-2014,, 2014,ii,Delaware,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",DE,de-senate-class-ii-2014,, 2014,ii,Georgia,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",GA,ga-senate-class-ii-2014,, 2014,iii,Hawaii,(special: Class 3),"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",HI,hi-senate-class-iii-2014,, 2014,ii,Idaho,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",ID,id-senate-class-ii-2014,, 2014,ii,Illinois,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",IL,il-senate-class-ii-2014,, 2014,ii,Iowa,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",IA,ia-senate-class-ii-2014,, 2014,ii,Kansas,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",KS,ks-senate-class-ii-2014,, 2014,ii,Kentucky,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",KY,ky-senate-class-ii-2014,, 2014,ii,Louisiana,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",LA,la-senate-class-ii-2014,, 2014,ii,Maine,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",ME,me-senate-class-ii-2014,, 2014,ii,Massachusetts,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",MA,ma-senate-class-ii-2014,, 2014,ii,Michigan,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",MI,mi-senate-class-ii-2014,, 2014,ii,Minnesota,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",MN,mn-senate-class-ii-2014,, 2014,ii,Mississippi,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",MS,ms-senate-class-ii-2014,, 2014,ii,Montana,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",MT,mt-senate-class-ii-2014,, 2014,ii,Nebraska,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",NE,ne-senate-class-ii-2014,, 2014,ii,New Hampshire,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",NH,nh-senate-class-ii-2014,, 2014,ii,New Jersey,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",NJ,nj-senate-class-ii-2014,, 2014,ii,New Mexico,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",NM,nm-senate-class-ii-2014,, 2014,ii,North Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",NC,nc-senate-class-ii-2014,, 2014,ii,Oklahoma,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",OK,ok-senate-class-ii-2014,, 2014,iii,Oklahoma,(special: Class 3),"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",OK,ok-senate-class-iii-2014,, 2014,ii,Oregon,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",OR,or-senate-class-ii-2014,, 2014,ii,Rhode Island,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",RI,ri-senate-class-ii-2014,, 2014,ii,South Carolina,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",SC,sc-senate-class-ii-2014,, 2014,iii,South Carolina,(special: Class 3),"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",SC,sc-senate-class-iii-2014,, 2014,ii,South Dakota,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",SD,sd-senate-class-ii-2014,, 2014,ii,Tennessee,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",TN,tn-senate-class-ii-2014,, 2014,ii,Texas,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",TX,tx-senate-class-ii-2014,, 2014,ii,Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",VA,va-senate-class-ii-2014,, 2014,ii,West Virginia,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",WV,wv-senate-class-ii-2014,, 2014,ii,Wyoming,,"http://en.wikipedia.org/wiki/United_States_Senate_elections,_2014",WY,wy-senate-class-ii-2014,, --------------------------------------------------------------------------------