-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path.Rhistory
512 lines (512 loc) · 29.7 KB
/
.Rhistory
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
summary(wi_town_map2$not_returned_diff_pct)
summary(wi_town_map2$turnout2020)
gwam_turnout = errorsarlm(turnout2020 ~ mean_resid + no_closed+ logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_turnout)
wi_town_map2df <- wi_town_map2@data
turnout_lm <- lm(turnout2020 ~ mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2df)
summary(turnout_lm)
summary(wi_town_map2$abs_counted2020pct)
abs_lm <- lm(abs_counted2020pct ~ mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2df)
summary(abs_lm)
summary(gwam_turnout)
gwam_turnout = errorsarlm(diff_turnout_pct2 ~ mean_resid + no_closed+ logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_turnout)
gwam_turnout = errorsarlm(diff_turnout_pct2 ~ mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_turnout)
wi_town_map2df <- wi_town_map2@data
turnout_lm <- rlm(diff_abs_pct2 ~ mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2df)
summary(turnout_lm)
turnout_lm <- rlm(diff_abs_pct2 ~ mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2df, weights=log(PERSONS))
turnout_lm <- rlm(diff_turnout_pct2 ~ mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2df, weights=log(PERSONS))
summary(turnout_lm)
names(wi_town_map2df)
turnout_lm <- rlm(diff_turnout_pct2 ~ mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2df, weights=log(SUM_PERSON))
summary(turnout_lm)
names(wi_town_map2df)
summary(wi_town_map2df$abs_counted2020pct)
install.packages("frm")
?frm
library(frm)
?frm
test_frac <- frm(abs_counted2020pct/100, mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2df )
test_frac <- frm(wi_town_map2df$abs_counted2020pct/100, wi_town_map2df$mean_resid + wi_town_map2df$logppsqkm +
wi_town_map2df$non_white_pct + wi_town_map2df$dem_pct + wi_town_map2df$dem_pct2 + as.factor(wi_town_map2df$county2) )
summary(wi_town_map2df$abs_counted2020pct/100)
wi_town_map2df$frac_abs2020 <- wi_town_map2df$abs_counted2020pct/100
summary(wi_town_map2df$frac_abs2020)
test_frac <- frm(wi_town_map2df$frac_abs2020, wi_town_map2df$mean_resid + wi_town_map2df$logppsqkm +
wi_town_map2df$non_white_pct + wi_town_map2df$dem_pct + wi_town_map2df$dem_pct2 + as.factor(wi_town_map2df$county2),
type="1P")
test_frac <- frm(wi_town_map2df$frac_abs2020, wi_town_map2df$mean_resid + wi_town_map2df$logppsqkm +
wi_town_map2df$non_white_pct + wi_town_map2df$dem_pct + wi_town_map2df$dem_pct2 + as.factor(wi_town_map2df$county2),
type="1P", na.rm=T)
myglm <- glm(frac_abs2020 ~ mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data = wi_town_map2df, family = quasibinomial('logit'))
coeftest(myglm, vcov.=vcovHC(myglm, type="HC0"))
summary(myglm)
library(sandwich)
library(lmtest)
install.packages("lmtest")
library(lmtest)
coeftest(myglm, vcov.=vcovHC(myglm, type="HC0"))
?coeftest
temp_sub <- subset(wi_town_map2df, county2=="MILWAUKEE")
View(temp_sub)
gwam_turnout = errorsarlm(diff_turnout ~ mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_turnout)
gwam_turnout = errorsarlm(diff_turnout ~ mean_resid + no_closed + logppsqkm + non_white_pct + dem_pct + dem_pct2 + as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_turnout)
closed_onlydf <- subset(wi_town_map2, no_closed==0)
nrow(closed_onlydf)
nb2_mil <- poly2nb(closed_onlydf)
nb2_mil[[21]] <- sort(as.integer(c(nb2_mil[[21]], 38)))
nb2_mil[[38]] <- sort(as.integer(c(21, nb2_mil[[38]])))
lw2mil <- nb2listw(nb2_mil, zero.policy = TRUE)
gwam_turnout_small = errorsarlm(diff_turnout ~ mean_resid + no_closed + logppsqkm + non_white_pct + dem_pct + dem_pct2+ as.factor(county2),
data=closed_onlydf, lw2mil, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_turnout_small)
gwam_turnout_small = errorsarlm(diff_turnout ~ mean_resid + no_closed + logppsqkm + non_white_pct + dem_pct + dem_pct2,
data=closed_onlydf, lw2mil, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_turnout_small)
names
names(wi_town_map2@data)
gwam_turnout = errorsarlm(turnout2020 ~ turnout2016 + mean_resid + no_closed + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_turnout)
gwam_turnout = errorsarlm(turnout2020 ~ turnout2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_turnout)
gwam_abs = errorsarlm(abs_counted2020pct ~ abs_counted2016pct + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_abs)
names(wi_town_map2)
wi_town_map2$abs_lost_pct2020 <- (wi_town_map2$abs_not_returned_2020/wi_town_map2$abs_issued_2020)*100
wi_town_map2$abs_lost_pct2016 <- (wi_town_map2$abs_not_returned_2016/wi_town_map2$abs_issued_2016)*100
gwam_abs = errorsarlm(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(wi_town_map2$abs_lost_pct2016)
wi_town_map2$abs_lost_pct2016[wi_town_map2$abs_lost_pct2016 == Inf] <- NA
wi_town_map2$abs_lost_pct2020[wi_town_map2$abs_lost_pct2020 == Inf] <- NA
gwam_abs_lost = errorsarlm(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE)
summary(gwam_abs_lost)
summary(gwam_abs_lost)[1:6]
warnings()
summary(wi_town_map2$provisional_all_2016)
install.packages("quantreg")
library(quantreg)
?rq
wi_town_map2df <- wi_town_map2@data
test_quantreg <- rq(bs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=0.1)
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=0.1)
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=c(0,1,by=.1))
test_quantreg
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=seq(0,1,by=.1))
summary(test_quantreg)
test_quantreg
hist(wi_town_map2$turnout2020)
quantile(wi_town_map2$turnout2020,seq(0,1,by=0.05))
quantile(wi_town_map2$turnout2020,seq(0,1,by=0.05),na.rm=TRUE)
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=0.2)
summary(test_quantreg)
test_quantreg
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=0.2, weights = log(SUM_PERSON))
summary(test_quantreg)
test_quantreg
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=0.1, weights = log(SUM_PERSON))
test_quantreg
summary(test_quantreg)
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=.5, weights = log(SUM_PERSON))
test_quantreg
summary(test_quantreg)
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=.5, weights = log(SUM_PERSON), na.omit=T)
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=.5, weights = log(SUM_PERSON), na.action = na.omit)
test_quantreg
summary(test_quantreg)
test_quantreg <- rq(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm + non_white_pct + dem_pct + dem_pct2
+ as.factor(county2), data=wi_town_map2df, tau=seq(0,1,by=.1), weights = log(SUM_PERSON), na.action = na.omit)
test_quantreg
summary(gwam_turnout)
summary(gwam_abs)
summary(gwam_turnout)
summary(gwam_abs_lost)
library(boot)
rsq <- function(formula, data, indices) {
d <- data[indices,] # allows boot to select sample
fit <- lm(formula, data=d)
return(summary(fit)$r.square)
}
results_turnout_boot <- boot(data=wi_town_map2, statistic=rsq,
R=1000, formula=errorsarlm(turnout2020 ~ turnout2016 + mean_resid + logppsqkm + non_white_pct +
dem_pct + dem_pct2
+ as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE))
summary(results_turnout_boot$t)
results_abs_ct_boot <- boot(data=wi_town_map2, statistic=rsq,
R=1000, formula=errorsarlm(abs_counted2020pct ~ abs_counted2016pct + mean_resid + logppsqkm +
non_white_pct + dem_pct + dem_pct2
+ as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE))
summary(results_abs_ct_boot$t)
results_abslost_boot <- boot(data=wi_town_map2, statistic=rsq,
R=1000, formula=errorsarlm(abs_lost_pct2020 ~ abs_lost_pct2016 + mean_resid + logppsqkm +
non_white_pct + dem_pct + dem_pct2
+ as.factor(county2),
data=wi_town_map2, lw2, tol.solve=1.0e-30, zero.policy = TRUE))
summary(results_abslost_boot$t)
non_white_seq <- seq(0,100,by=1)
logpop_seq <- seq(0,10,by=0.1)
dem_seq <- seq(0,100,by=1)
bdraw_model_mat <- poll_sarprob2$bdraw
rho_vec <- poll_sarprob2$pdraw
bdraw_model_mat2 <- cbind(bdraw_model_mat,rho_vec)
standard_error <- function(x) sd(x) / sqrt(length(x))
pred_nonwhite <- as.matrix(cbind(1,mean(polls_all2b$log_pop_sqkm,na.rm=T),non_white_seq,mean(polls_all2b$dem_pct18,na.rm=T),
mean(polls_all2b$dem_pct2,na.rm=T),1,1))
bdraw_model_mat2 <- bdraw_model_mat2[,c(1:5,73,77)]
xb_nonwhite <- pred_nonwhite %*% t(bdraw_model_mat2)
nonwhite_results <- apply(xb_nonwhite,1,quantile, probs=c(0.025,.5,.975))
nonwhite_results <- pnorm(nonwhite_results)
pred_popdens <- as.matrix(cbind(1,logpop_seq,mean(polls_all2b$non_white_pct,na.rm=T),
mean(polls_all2b$dem_pct18,na.rm=T),
mean(polls_all2b$dem_pct2,na.rm=T),1,1))
pred_dems <- as.matrix(cbind(1,mean(polls_all2b$log_pop_sqkm,na.rm=T),mean(polls_all2b$non_white_pct,na.rm=T),
dem_seq,
dem_seq^2,1,1))
xb_popdens <- pred_popdens %*% t(bdraw_model_mat2)
popdens_results <- apply(xb_popdens,1,quantile, probs=c(0.025,.5,.975))
popdens_results <- pnorm(popdens_results)
xb_dems <- pred_dems %*% t(bdraw_model_mat2)
dems_results <- apply(xb_dems,1,quantile, probs=c(0.025,.5,.975))
dems_results <- pnorm(dems_results)
nonwhite_results1stage <- pnorm(xb_nonwhite)
View(nonwhite_results1stage)
dems_results1stage <- pnorm(xb_dems)
popdens1stage <- pnorm(xb_popdens)
sim.betas_turn <- mvrnorm(10000,mu=coef.sarlm(gwam_turnout),Sigma = vcov.sarlm(gwam_turnout))
head(sim.betas_turn)
summary(poll_sarprob2)
summary(gwam_turnout)
coef(gwam_turnout)
length(coef(gwam_turnout))
sim.betas_turn_sub <- sim.betas[,c(1:8,76)] # 1 is spatial error,
sim.betas_turn <- mvrnorm(1000,mu=coef.sarlm(gwam_turnout),Sigma = vcov.sarlm(gwam_turnout))
sim.betas_turn_sub <- sim.betas_turn[,c(1:8,76)] # 1 is spatial error,
dems_results
nonwhite_results
dems_results[2,]
dem_median <- dems_results[2,]
nonwhite_median <- nonwhite_results[2,]
popdens_median <- popdens_results[2,]
coef(gwam_turnout)
set.seed(1337)
sim.betas_turn <- mvrnorm(1000,mu=coef.sarlm(gwam_turnout),Sigma = vcov.sarlm(gwam_turnout))
sim.betas_turn_sub <- sim.betas_turn[,c(1:8,76)] # 1 is spatial error,
nonwhite2stage_mat <- as.matrix(cbind(mean(1,1,wi_town_map2$turnout2016,na.rm=T), nonwhite_median,mean(wi_town_map$logppsqkm,na.rm=T),
non_white_seq,mean(wi_town_map2$dem_pct,na.rm=T),mean(wi_town_map2$dem_pct2,na.rm=T)))
nonwhite2stage_mat <- as.matrix(cbind(mean(1,1,wi_town_map2$turnout2016,na.rm=T), nonwhite_median,mean(wi_town_map$logppsqkm,na.rm=T),
non_white_seq,mean(wi_town_map2$dem_pct,na.rm=T),mean(wi_town_map2$dem_pct2,na.rm=T),1))
xb2_nonwhite <- nonwhite2stage_mat %*% t(sim.betas_sub)
set.seed(1337)
sim.betas_turn <- mvrnorm(1000,mu=coef.sarlm(gwam_turnout),Sigma = vcov.sarlm(gwam_turnout))
sim.betas_turn_sub <- sim.betas_turn[,c(1:8,76)] # 1 is spatial error,
nonwhite2stage_mat <- as.matrix(cbind(mean(1,1,wi_town_map2$turnout2016,na.rm=T), nonwhite_median,mean(wi_town_map$logppsqkm,na.rm=T),
non_white_seq,mean(wi_town_map2$dem_pct,na.rm=T),mean(wi_town_map2$dem_pct2,na.rm=T),1))
xb2_nonwhite <- nonwhite2stage_mat %*% t(sim.betas_turn_sub)
dim(sim.betas_turn_sub)
dim(nonwhite2stage_mat)
nonwhite2stage_mat <- as.matrix(cbind(1,1,mean(wi_town_map2$turnout2016,na.rm=T), nonwhite_median,mean(wi_town_map$logppsqkm,na.rm=T),
non_white_seq,mean(wi_town_map2$dem_pct,na.rm=T),mean(wi_town_map2$dem_pct2,na.rm=T),1))
nonwhite2stage_mat <- as.matrix(cbind(1,1,mean(wi_town_map2$turnout2016,na.rm=T), nonwhite_median,mean(wi_town_map2$logppsqkm,na.rm=T),
non_white_seq,mean(wi_town_map2$dem_pct,na.rm=T),mean(wi_town_map2$dem_pct2,na.rm=T),1))
xb2_nonwhite <- nonwhite2stage_mat %*% t(sim.betas_turn_sub)
xb2_nonwhite_col <- apply(xb2_nonwhite, 1, quantile, probs=c(0.025,.5,.975))
xb2_nonwhite_col
sim.betas_abs <- mvrnorm(1000,mu=coef.sarlm(gwam_abs),Sigma = vcov.sarlm(gwam_abs))
sim.betas_abs_sub <- sim.betas_abs[,c(1:8,76)] # 1 is spatial error,
xb2_nonwhite_abs <- nonwhite2stage_mat %*% t(sim.betas_abs_sub)
xb2_nonwhite_col_abs <- apply(xb2_nonwhite_abs, 1, quantile, probs=c(0.025,.5,.975))
xb2_nonwhite_col_abs
summary(gwam_abs)
sim.betas_abs_sub <- sim.betas_abs[,c(1:8,79)] # 1 is spatial error,
xb2_nonwhite_abs <- nonwhite2stage_mat %*% t(sim.betas_abs_sub)
xb2_nonwhite_col_abs <- apply(xb2_nonwhite_abs, 1, quantile, probs=c(0.025,.5,.975))
xb2_nonwhite_col_abs
sim.betas_abs_sub <- sim.betas_abs[,c(1:8,78)] # 1 is spatial error,
xb2_nonwhite_abs <- nonwhite2stage_mat %*% t(sim.betas_abs_sub)
xb2_nonwhite_col_abs <- apply(xb2_nonwhite_abs, 1, quantile, probs=c(0.025,.5,.975))
xb2_nonwhite_col_abs
popdens2stage_mat <- as.matrix(cbind(1,1,mean(wi_town_map2$turnout2016,na.rm=T), popdens_median,logpop_seq,
mean(wi_town_map2$non_white_pct,na.rm=T),
mean(wi_town_map2$dem_pct,na.rm=T),mean(wi_town_map2$dem_pct2,na.rm=T),1))
popdens2stage_mat <- as.matrix(cbind(1,1,mean(wi_town_map2$turnout2016,na.rm=T), popdens_median,logpop_seq,
mean(wi_town_map2$non_white_pct,na.rm=T),
mean(wi_town_map2$dem_pct,na.rm=T),mean(wi_town_map2$dem_pct2,na.rm=T),1))
xb2_popdens <- popdens2stage_mat %*% t(sim.betas_turn_sub)
xb2_popdens_col <- apply(xb2_popdens, 1, quantile, probs=c(0.025,.5,.975))
xb2_popdens_col
xb2_popdens_abs <- popdens2stage_mat %*% t(sim.betas_abs_sub)
xb2_popdens_col_abs <- apply(xb2_popdens_abs, 1, quantile, probs=c(0.025,.5,.975))
xb2_popdens_col_abs###good, this should work
plot(non_white_seq,xb2_nonwhite_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Non-white Pop. on Poll Closure", ylim=c(0,1), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col[1,], lty=4)
plot(non_white_seq,xb2_nonwhite_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Non-white Pop. on Poll Closure", ylim=c(0,100), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col[3,],lty=4)
plot(non_white_seq,xb2_nonwhite_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Non-white Pop. on Turnnout", ylim=c(0,100), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col[3,],lty=4)
rug(wi_town_map2$non_white_pct)
plot(logpop_seq,xb2_popdens_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Pop. Density on Turnout", ylim=c(0,100), cex.lab=1.2)
lines(non_white_seq,xb2_popdens_col[1,], lty=4)
lines(non_white_seq,xb2_popdens_col[3,],lty=4)
plot(non_white_seq,xb2_nonwhite_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Non-white Pop. on Turnout", ylim=c(0,100), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col[3,],lty=4)
plot(non_white_seq,xb2_nonwhite_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Non-white Pop. on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col[3,],lty=4)
plot(logpop_seq,xb2_popdens_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Pop. Density on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(non_white_seq,xb2_popdens_col[1,], lty=4)
plot(logpop_seq,xb2_popdens_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Pop. Density on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(logpop_seq,xb2_popdens_col[1,], lty=4)
lines(logpop_seq,xb2_popdens_col[3,],lty=4)
plot(logpop_seq,xb2_popdens_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Log(Pop. Per Square KM)", main="Effect of Pop. Density on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(logpop_seq,xb2_popdens_col[1,], lty=4)
lines(logpop_seq,xb2_popdens_col[3,],lty=4)
rug(wi_town_map2$logppsqkm)
setwd("F:/MEDSL/covid19/cleaned_wi2")
jpeg("elec_nonwhite_plot.jpg", res=600, height = 12, width = 18, units = "in")
par(mfrow=(c(2,1)))
plot(non_white_seq,xb2_nonwhite_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Non-white Pop. on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col[3,],lty=4)
rug(wi_town_map2$non_white_pct)
###Log pop
plot(logpop_seq,xb2_popdens_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Log(Pop. Per Square KM)", main="Effect of Pop. Density on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(logpop_seq,xb2_popdens_col[1,], lty=4)
lines(logpop_seq,xb2_popdens_col[3,],lty=4)
rug(wi_town_map2$logppsqkm)
dev.off()
setwd("F:/MEDSL/covid19/cleaned_wi2")
jpeg("elec_nonwhite_plot.jpg", res=600, height = 12, width = 18, units = "in")
par(mfrow=(c(1,2)))
plot(non_white_seq,xb2_nonwhite_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Non-white Pop. on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col[3,],lty=4)
rug(wi_town_map2$non_white_pct)
###Log pop
plot(logpop_seq,xb2_popdens_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Log(Pop. Per Square KM)", main="Effect of Pop. Density on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(logpop_seq,xb2_popdens_col[1,], lty=4)
lines(logpop_seq,xb2_popdens_col[3,],lty=4)
rug(wi_town_map2$logppsqkm)
dev.off()
setwd("F:/MEDSL/covid19/cleaned_wi2")
jpeg("elec_nonwhite_plot.jpg", res=600, height = 12, width = 20, units = "in")
par(mfrow=(c(1,2)))
plot(non_white_seq,xb2_nonwhite_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Non-white %", main="Effect of Non-white Pop. on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col[3,],lty=4)
rug(wi_town_map2$non_white_pct)
###Log pop
plot(logpop_seq,xb2_popdens_col[2,], type="l",lty=1, col="gray8",ylab="Turnout %",
xlab="Log(Pop. Per Square KM)", main="Effect of Pop. Density on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(logpop_seq,xb2_popdens_col[1,], lty=4)
lines(logpop_seq,xb2_popdens_col[3,],lty=4)
rug(wi_town_map2$logppsqkm)
dev.off()
plot(non_white_seq,xb2_nonwhite_col_abs[2,], type="l",lty=1, col="gray8",ylab="Abs. % of Total Ballots",
xlab="Non-white %", main="Effect of Non-white Pop. on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col_abs[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col_abs[3,],lty=4)
plot(non_white_seq,xb2_nonwhite_col_abs[2,], type="l",lty=1, col="gray8",ylab="Abs. % of Total Ballots",
xlab="Non-white %", main="Effect of Non-white Pop. on Turnout", ylim=c(0,60), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col_abs[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col_abs[3,],lty=4)
rug(wi_town_map2$non_white_pct)
plot(logpop_seq,xb2_popdens_col_abs[2,], type="l",lty=1, col="gray8",ylab="Abs. % of Total Ballots",
xlab="Log(Pop. Per Square KM)", main="Effect of Pop. Density on Turnout", ylim=c(20,60), cex.lab=1.2)
lines(logpop_seq,xb2_popdens_col_abs[1,], lty=4)
lines(logpop_seq,xb2_popdens_col_abs[3,],lty=4)
plot(logpop_seq,xb2_popdens_col_abs[2,], type="l",lty=1, col="gray8",ylab="Abs. % of Total Ballots",
xlab="Log(Pop. Per Square KM)", main="Effect of Pop. Density on Turnout", ylim=c(0,60), cex.lab=1.2)
lines(logpop_seq,xb2_popdens_col_abs[1,], lty=4)
lines(logpop_seq,xb2_popdens_col_abs[3,],lty=4)
rug(wi_town_map2$logppsqkm)
jpeg("elec_absuse_plot.jpg", res=600, height = 12, width = 20, units = "in")
par(mfrow=(c(1,2)))
plot(non_white_seq,xb2_nonwhite_col_abs[2,], type="l",lty=1, col="gray8",ylab="Abs. % of Total Ballots",
xlab="Non-white %", main="Effect of Non-white Pop. on Abs. Ballot Use", ylim=c(0,60), cex.lab=1.2)
lines(non_white_seq,xb2_nonwhite_col_abs[1,], lty=4)
lines(non_white_seq,xb2_nonwhite_col_abs[3,],lty=4)
rug(wi_town_map2$non_white_pct)
###Log pop
plot(logpop_seq,xb2_popdens_col_abs[2,], type="l",lty=1, col="gray8",ylab="Abs. % of Total Ballots",
xlab="Log(Pop. Per Square KM)", main="Effect of Pop. Density on Abs. Ballot Use", ylim=c(0,60), cex.lab=1.2)
lines(logpop_seq,xb2_popdens_col_abs[1,], lty=4)
lines(logpop_seq,xb2_popdens_col_abs[3,],lty=4)
rug(wi_town_map2$logppsqkm)
dev.off()
nonclosed_polls <- subset(polls_all2b, closed==0)
length(unique(nonclosed_polls$county))
sort(unique(nonclosed_polls$county))
length(unique(nonclosed_polls$string_id))
length(unique(nonclosed_polls$string_id))/length(unique(nonclosed_polls$string_id))
length(unique(nonclosed_polls$string_id))/length(unique(polls_all2b$string_id))
length(unique(polls_all2b$string_id))
county_wi_col <- polls_all2b@data %>% group_by(county) %>% summarize(total_polls = sum(dum,na.rm=T), closed_polls=sum(closed,na.rm=T))
polls_all2b$dum=1
county_wi_col <- polls_all2b@data %>% group_by(county) %>% summarize(total_polls = sum(dum,na.rm=T), closed_polls=sum(closed,na.rm=T))
View(county_wi_col)
town_wi_col <- polls_all2b@data %>% group_by(string_id) %>% summarize(total_polls = sum(dum,na.rm=T), closed_polls=sum(closed,na.rm=T))
length(which(town_wi_col$closed_polls>1))
nrow(polls_all2)
nrow(polls_all2b)
temp_sub <- subset(polls_all2b, closed==1)
length(unique(temp_sub$Muni))
length(unique(polls_all2b$Muni))
length(unique(temp_sub$string_id))
length(unique(polls_all2b$string_id))
wd_global <- readline(prompt = "Enter file path for working directory: ")
setwd(wd_global)
county_acs <- read.csv("county2018acs.csv")
names(county_acs)
head(county_acs)
county_acs <- county_acs[,-c(6:55)]
names(county_acs)
county_acs <- subset(county_acs, select=-c(SE_A02002_001,SE_A04001_001))
names(county_acs)
colnames(county_acs)[6:ncol(county_acs)] <- c("total_pop","pop_ppsm","area_miles","male_pop","male5under","male5to9pop","male10to14pop",
"male15to17pop","male18to24pop","male25to34pop","male35to44pop","male45to54pop",
"male55to64pop","male65to74pop","male75to84pop","male85over_pop","female_pop",
"female_pop","female5under","female5to9pop","female10to14pop","female15to17pop","female18to24pop",
"female25to34pop","female35to44pop","female45to54pop","female55to64pop","female65to74pop",
"female75to84pop","female85over_pop","not_hispanic_pop","white_alone_pop","black_alone_pop",
"amer_indian_alone_pop","asian_alone_pop","pac_island_alone_pop","other_race_alone",
"races2plus_alone","hispanic_pop","white_hisp","black_hisp","amer_ind_hisp","asian_hisp",
"pac_island_hisp_pop","other_race_hisp","races2plus_hisp","pop25over","less_than_hs",
"high_school","some_college","college_degree","masters_degree","prof_degree","phd_degree",
"gini_index")
ncol(county_acs)
colnames(county_acs)[6:59] <- c("total_pop","pop_ppsm","area_miles","male_pop","male5under","male5to9pop","male10to14pop",
"male15to17pop","male18to24pop","male25to34pop","male35to44pop","male45to54pop",
"male55to64pop","male65to74pop","male75to84pop","male85over_pop","female_pop",
"female_pop","female5under","female5to9pop","female10to14pop","female15to17pop","female18to24pop",
"female25to34pop","female35to44pop","female45to54pop","female55to64pop","female65to74pop",
"female75to84pop","female85over_pop","not_hispanic_pop","white_alone_pop","black_alone_pop",
"amer_indian_alone_pop","asian_alone_pop","pac_island_alone_pop","other_race_alone",
"races2plus_alone","hispanic_pop","white_hisp","black_hisp","amer_ind_hisp","asian_hisp",
"pac_island_hisp_pop","other_race_hisp","races2plus_hisp","pop25over","less_than_hs",
"high_school","some_college","college_degree","masters_degree","prof_degree","phd_degree",
"gini_index")
temp <- c("total_pop","pop_ppsm","area_miles","male_pop","male5under","male5to9pop","male10to14pop",
"male15to17pop","male18to24pop","male25to34pop","male35to44pop","male45to54pop",
"male55to64pop","male65to74pop","male75to84pop","male85over_pop","female_pop",
"female_pop","female5under","female5to9pop","female10to14pop","female15to17pop","female18to24pop",
"female25to34pop","female35to44pop","female45to54pop","female55to64pop","female65to74pop",
"female75to84pop","female85over_pop","not_hispanic_pop","white_alone_pop","black_alone_pop",
"amer_indian_alone_pop","asian_alone_pop","pac_island_alone_pop","other_race_alone",
"races2plus_alone","hispanic_pop","white_hisp","black_hisp","amer_ind_hisp","asian_hisp",
"pac_island_hisp_pop","other_race_hisp","races2plus_hisp","pop25over","less_than_hs",
"high_school","some_college","college_degree","masters_degree","prof_degree","phd_degree",
"gini_index")
length(temp)
names(county_acs)
colnames(county_acs)[6:59] <- c("total_pop","pop_ppsm","area_miles","male_pop","male5under","male5to9pop","male10to14pop",
"male15to17pop","male18to24pop","male25to34pop","male35to44pop","male45to54pop",
"male55to64pop","male65to74pop","male75to84pop","male85over_pop",
"female_pop","female5under","female5to9pop","female10to14pop","female15to17pop","female18to24pop",
"female25to34pop","female35to44pop","female45to54pop","female55to64pop","female65to74pop",
"female75to84pop","female85over_pop","not_hispanic_pop","white_alone_pop","black_alone_pop",
"amer_indian_alone_pop","asian_alone_pop","pac_island_alone_pop","other_race_alone",
"races2plus_alone","hispanic_pop","white_hisp","black_hisp","amer_ind_hisp","asian_hisp",
"pac_island_hisp_pop","other_race_hisp","races2plus_hisp","pop25over","less_than_hs",
"high_school","some_college","college_degree","masters_degree","prof_degree","phd_degree",
"gini_index")
names(county_acs)
county_acs$male_pct <- (county_acs$male_pop/county_acs$total_pop)*100
county_acs$female_pct <- (county_acs$female_pop/county_acs$total_pop)*100
names(county_acs)
county_acs$age18underpct <- (sum(county_acs[10:13,23:26])/county_acs$total_pop)*100
View(county_acs)
county_acs$age18underpct <- (rowSum(county_acs[10:13,23:26])/county_acs$total_pop)*100
county_acs$age18underpct <- (rowSums(county_acs[10:13,23:26])/county_acs$total_pop)*100
View(county_acs)
county_acs$age18underpct <- (sum(county_acs[,10:13,23:26])/county_acs$total_pop)*100
View(county_acs)
county_acs$age18underpct <- (rowSums(county_acs[,10:13,23:26])/county_acs$total_pop)*100
View(county_acs)
county_acs$age18to24pct <- ((county_acs$male18to24pop+county_acs$female18to24pop)/county_acs$total_pop)*100
county_acs$age25to34pct <- ((county_acs$male25to34pop+county_acs$female25to34pop)/county_acs$total_pop)*100
county_acs$age35to44pct <- ((county_acs$male35to44pop+county_acs$female35to44pop)/county_acs$total_pop)*100
county_acs$age45to54pct <- ((county_acs$male45to54pop+county_acs$female45to54pop)/county_acs$total_pop)*100
county_acs$age55to64pct <- ((county_acs$male55to64pop+county_acs$female55to64pop)/county_acs$total_pop)*100
county_acs$age65to74pct <- ((county_acs$male65to74pop+county_acs$female65to74pop)/county_acs$total_pop)*100
county_acs$age75to84pct <- ((county_acs$male75to84pop+county_acs$female75to84pop)/county_acs$total_pop)*100
county_acs$age85over_pct <- ((county_acs$male85over_pop+county_acs$female85over_pop)/county_acs$total_pop)*100
county_acs$white_pct <- (county_acs$white_alone_pop/county_acs$total_pop)*100
wd_global <- readline(prompt = "Enter file path for working directory: ")
setwd(wd_global)
getwd()
list.files()
wd_global
names(county_acs)
county_acs$white_pct <- (county_acs$white_alone_pop/county_acs$total_pop)*100
county_acs$black_pct <- ((county_acs$black_alone_pop+county_acs$black_hisp)/county_acs$total_pop)*100
county_acs$asianpi_pct <- ((county_acs$asian_alone_pop+county_acs$asian_hisp+county_acs$pac_island_alone_pop+county_acs$pac_island_hisp_pop)/
county_acs$total_pop)*100
county_acs$hispanic_pct <- ((county_acs$hispanic_pop)/county_acs$total_pop)*100
county_acs$other_pct <- (rowSums(county_acs[,c(38,41:42,46,49:40)])/county_acs$total_pop)*100
county_acs$less_than_hs_pct <- (county_acs$less_than_hs/county_acs$pop25over)*100
county_acs$high_school_pct <- (county_acs$high_school/county_acs$pop25over)*100
county_acs$some_college_pct <- (county_acs$some_college/county_acs$pop25over)*100
county_acs$college_degree_pct <- (county_acs$college_degree/county_acs$pop25over)*100
county_acs$masters_degree_pct <- (county_acs$masters_degree/county_acs$pop25over)*100
county_acs$prof_degree_pct <- (county_acs$prof_degree/county_acs$pop25over)*100
county_acs$phd_degree_pct <- (county_acs$phd_degree/county_acs$pop25over)*100
names(county_acs)
county_acs2 <- county_acs[,-c(35:50)]
names(county_acs2)
write.csv(county_acs2, "county_acs_demos.csv",row.names = FALSE)
saveRDS(county_acs2, "county_acs_demos.Rdata")