-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculatetotals.R
164 lines (128 loc) · 5.93 KB
/
calculatetotals.R
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
totals = read.csv('seat_loss_gain.csv')
totals = totals[,-1]
rownames(totals) = totals$state
totals = totals[,-1]
intervals = seq(-1000000,1000000,10000)
names = intervals
names(totals) = names
positives = totals[,c(101:201)]
negatives = totals[,c(1:100)]
library(dplyr)
occurence = function(row,val){
occurs = which(row %in% val)
if (val>=0){
return(occurs[1])
} else if (val<0){
if(length(occurs)>0){
return(occurs[length(occurs)])
} else{
return(NA)
}
}
}
transneg = function(val){
return(-(1000000-(10000*(val-1))))
}
gainoneseat = 10000*apply(positives,1,occurence,1)
gaintwoseats = 10000*apply(positives,1,occurence,2)
loseoneseat = transneg(apply(negatives,1,occurence,-1))
losetwoseats = transneg(apply(negatives,1,occurence,-2))
states = read.csv('statepops2010.csv',header=F)[c(1:50),1]
seatlosses = data.frame(states,losetwoseats,loseoneseat,gainoneseat,gaintwoseats)
colnames(seatlosses) = c('State','Lose 2 Seats','Lose 1 Seat','Gain 1 Seat','Gain 2 Seats')
colnames(seatlosses) = c('State','-2','-1','+1','+2')
library(tidyr)
seats_tidy = gather(seatlosses,key='key',value='value',-state)
seats_tidy$value = seats_tidy$value/1000
seats_tidy$key = factor(seats_tidy$key,levels=c('-2','-1','+1','+2'))
library(ggplot2)
library(forcats)
library(ggthemes)
seats = ggplot(seats_tidy) +
geom_col(aes(x=key,y=value)) +
xlab('') +
ylab('') +
#labs(subtitle = 'How Population Changes Would Have Gained/Lost States House Seats In 2010') +
facet_wrap(~State,ncol=10) +
theme_calc()
ggsave('gainsandlosses.jpg',seats,height=10,width=20)
# Adjusting seat losses to remove lose/gain 2 seats columns
seatlosses = seatlosses[,c(1,3,4)]
colnames(seatlosses) = c('state','Lose 1 Seat','Gain 1 Seat')
seatlosses_toprint = seatlosses
seatlosses_toprint = format(seatlosses_toprint,scientific=FALSE,big.mark = ",", big.interval = 3)
#write.csv(seatlosses_toprint,'seatlosses.csv')
# Moving to percentage of population needed for seat losses
populations = read.csv('statepops2010.csv',header=F)[c(1:50),2]
seatlosses_per = seatlosses
seatlosses_per$`Lose 1 Seat` = round((100*seatlosses_per$`Lose 1 Seat`/populations),1)
seatlosses_per$`Gain 1 Seat` = round((100*seatlosses_per$`Gain 1 Seat`/populations),1)
write.csv(seatlosses_per,'seatlosses_per.csv')
graphing_tidy = gather(seatlosses_per,key='key',val='val',-state)
graphing_tidy$val = graphing_tidy$val/100
state_swings = ggplot(graphing_tidy) +
geom_point(aes(y=fct_rev(state),x=val,color=fct_rev(key)),size=15) +
scale_color_manual(values=c('blue4','orangered3')) +
coord_cartesian(c(-.11,.11)) +
scale_x_continuous(breaks=seq(-.1,.1,.05),
labels = scales::percent) +
geom_vline(xintercept=0,color='black') +
xlab('Swing Needed From 2010 Population To Add/Drop Seat(s)') +
ggtitle('How Close States Came To Gaining/Losing Congressional Seat In 2010') +
labs(subtitle='Based on Theoretical Census Apportionment Population Swings') +
theme(axis.ticks.y= element_blank(),
panel.grid.major.y = element_line(size=2,color='gray80'),
panel.background = element_blank(),
axis.text.x = element_text(size=40),
axis.text.y = element_text(size=40),
axis.title.y = element_blank(),
axis.title.x = element_text(size=55),
plot.title = element_text(size=55,hjust=0.5),
plot.subtitle = element_text(size=50,hjust=0.5),
legend.position = 'bottom',
legend.text = element_text(size=40),
legend.title = element_blank(),
legend.key.size = unit(0.7,'in'),
legend.spacing.x = unit(0.5, 'in'))
ggsave('state_swings.jpg',state_swings,height=45,width=30)
ucr = seatlosses
ucr$`Lose 1 Seat` = round((100*ucr$`Lose 1 Seat`/populations),1)
ucr$`Gain 1 Seat` = round((100*ucr$`Gain 1 Seat`/populations),1)
undercount = read.csv('undercount.csv',sep='',header=F)
undercount = undercount[,7:8]
colnames(undercount) = c('uc','RMSE')
ucr = cbind(ucr,undercount)
ucr$miss_gain_raw = ucr$uc > ucr$`Gain 1 Seat`
ucr$avoided_loss = ucr$uc < ucr$`Lose 1 Seat`
ucr_filtered_raw = ucr
ucr_filtered_raw$`Lose 1 Seat` = ucr_filtered_raw$`Lose 1 Seat` * ucr_filtered_raw$avoided_loss
ucr_filtered_raw$`Gain 1 Seat` = ucr_filtered_raw$`Gain 1 Seat` * ucr_filtered_raw$miss_gain_raw
ucr_filtered_raw = ucr_filtered_raw[ucr_filtered_raw$`Lose 1 Seat` < 0 | ucr_filtered_raw$`Gain 1 Seat` > 0,]
ucr_filtered_raw = ucr_filtered_raw[!is.na(ucr_filtered_raw$uc),]
ucr_filtered_raw = ucr_filtered_raw[,2:4]
ucr_filtered_raw$`Lose 1 Seat` = ifelse(ucr_filtered_raw$`Lose 1 Seat` == 0, '--',ucr_filtered_raw$`Lose 1 Seat`)
ucr_filtered_raw$`Gain 1 Seat` = ifelse(ucr_filtered_raw$`Gain 1 Seat` == 0, '--',ucr_filtered_raw$`Gain 1 Seat`)
ucr = seatlosses
ucr$`Lose 1 Seat` = round((100*ucr$`Lose 1 Seat`/populations),1)
ucr$`Gain 1 Seat` = round((100*ucr$`Gain 1 Seat`/populations),1)
ME = pnorm(1) - .5
undercount = read.csv('undercount.csv',sep='',header=F)
undercount = undercount[,7:8]
colnames(undercount) = c('uc','RMSE')
undercount = undercount %>% mutate(
lower_uc = round(uc - qnorm(.5 + ME)*RMSE,2),
upper_uc = round(uc + qnorm(.5 + ME)*RMSE,2)
)
ucr = cbind(ucr,undercount)
ucr$miss_gain_se = ucr$upper_uc > ucr$`Gain 1 Seat`
ucr$avoided_se = ucr$lower_uc < ucr$`Lose 1 Seat`
ucr_filtered_se = ucr
ucr_filtered_se$`Lose 1 Seat` = ucr_filtered_se$`Lose 1 Seat` * ucr_filtered_se$avoided_se
ucr_filtered_se$`Gain 1 Seat` = ucr_filtered_se$`Gain 1 Seat` * ucr_filtered_se$miss_gain_se
ucr_filtered_se = ucr_filtered_se[ucr_filtered_se$`Lose 1 Seat` <0 | ucr_filtered_se$`Gain 1 Seat` > 0,]
ucr_filtered_se = ucr_filtered_se[!is.na(ucr_filtered_se$uc),]
ucr_filtered_se = ucr_filtered_se[,c(2:7)]
ucr_filtered_se$`Lose 1 Seat` = ifelse(ucr_filtered_se$`Lose 1 Seat` == 0, '--',ucr_filtered_se$`Lose 1 Seat`)
ucr_filtered_se$`Gain 1 Seat` = ifelse(ucr_filtered_se$`Gain 1 Seat` == 0, '--',ucr_filtered_se$`Gain 1 Seat`)
write.csv(ucr_filtered_raw,'state_pers_within_uc.csv')
write.csv(ucr_filtered_se,'state_pers_within_uc_se.csv')