-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulations.Rmd
130 lines (93 loc) · 4.13 KB
/
Simulations.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
---
title: "Simulations"
author: "Polistat 2020"
date: "10/5/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE)
library(tidyverse)
library(dplyr)
library(leaps)
library(useful)
```
```{r date}
currentDate <- as.Date(scan(file = "currentDate.txt", what = "character"))
```
```{r init_data, include=FALSE}
state_cor <- as.matrix(read_csv("StateCorrelationWith7") %>% select(-c(X1)))
states <- read.csv(paste("AveragedPolls - ", currentDate, ".csv", sep = "")) %>%
rename(State = StateName, mean = StateMean, variance = Variance, electoral_votes = ElectoralVotes)
states <- cbind(states, state_outcome = rep(0, nrow(states)), distance = rep(0, nrow(states))) %>%
mutate(sd = variance^(1/2)) %>%
select(-c("variance", "X")) %>%
select(c(1,2,7,3,4,5,6))
states_length <- c(1:nrow(states))
cor_sum <- rowSums(state_cor)
row.rnorm <- function(x,y) rnorm(1, mean = x, sd = y)
```
```{r simulation}
#print(Sys.time())
#variables
num_simulations <- 10000
electoral_counts <- as.data.frame(matrix(c(0,0), ncol = 2, nrow = 1, dimnames = list(NULL, c("rep_electoral_counts", "dem_electoral_counts"))))
outcome_counts <- as.data.frame(matrix(c(0,0,0), ncol = 3, nrow = 1, dimnames = list(NULL, c("rep_win_counts", "dem_win_counts", "split_win_counts"))))
electoral_bins <- (matrix(rep(0, 538), ncol = 1, nrow = 538))
stateResults <- cbind(
data.frame(State = states$State, Mean = rep(0, nrow(states)), BidenWins = rep(0, nrow(states)), TrumpWins = rep(0, nrow(states))),
cbind(
states$electoral_votes,
states$BPI
)
) %>%
rename(ElectoralVotes = "1", BPI = "2")
sim_count <- 1
while(sim_count <= num_simulations){
#resetting
electoral_counts$rep_electoral_counts = 0
electoral_counts$dem_electoral_counts = 0
#main
#for(i in states_length){
# states$state_outcome[i] = rnorm(1, mean=states$mean[i], sd=states$sd[i])
#}
states$state_outcome = mapply(row.rnorm, x = states$mean, y = states$sd)
#adding after the fact
states$distance = states$state_outcome - states$mean
shifts <- t(states$distance %*% state_cor)/cor_sum
states$state_outcome + shifts
#convert to electoral votes
for(i in states_length){
if(states$state_outcome[i] > 0.5){
electoral_counts$rep_electoral_counts = electoral_counts$rep_electoral_counts + states$electoral_votes[i]
stateResults$TrumpWins[i] = stateResults$TrumpWins[i] + 1
} else if(states$state_outcome[i] < 0.5){
electoral_counts$dem_electoral_counts = electoral_counts$dem_electoral_counts + states$electoral_votes[i]
stateResults$BidenWins[i] = stateResults$BidenWins[i] + 1
}
stateResults$Mean[i] = stateResults$Mean[i] + states$state_outcome[i]
#print(paste(deparse(electoral_counts), collapse = ""))
}
if(electoral_counts$rep_electoral_counts >= 270){
outcome_counts$rep_win_counts = outcome_counts$rep_win_counts + 1
} else if(electoral_counts$dem_electoral_counts >= 270){
outcome_counts$dem_win_counts = outcome_counts$dem_win_counts + 1
} else {
outcome_counts$split_win_counts = outcome_counts$split_win_counts + 1
}
electoral_bins[electoral_counts$rep_electoral_counts, 1] = electoral_bins[electoral_counts$rep_electoral_counts, 1] + 1
sim_count = sim_count + 1
}
stateResults$Mean <- stateResults$Mean/num_simulations
rep_win_percentage = outcome_counts$rep_win_counts[1]/num_simulations * 100
dem_win_percentage = outcome_counts$dem_win_counts[1]/num_simulations * 100
split_win_percentage = outcome_counts$split_win_counts[1]/num_simulations * 100
win_counts <- data.frame(rep_win_percentage, dem_win_percentage, split_win_percentage)
print(paste("Republican Win%: ", rep_win_percentage))
print(paste("Democratic Win%: ", dem_win_percentage))
#print(Sys.time())
#print(shifts)
#write.csv(stateResults, file = paste("StateOutcomes - ", currentDate, ".csv", sep = ""))
#write.csv(outcome_counts, file = paste("NationalOutcomes - ", currentDate, ".csv", sep = ""))
#write.csv(win_counts, file = paste("NationalWins - ", currentDate, ".csv", sep = ""))
#write.csv(electoral_bins, file = paste("ElectoralBins - ", currentDate, ".csv", sep = ""))
```