-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
308 lines (184 loc) · 9.52 KB
/
server.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
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
library(ggplot2)
library(gridExtra)
library(DT)
library(shiny)
library(bcp)
library(dplyr)
options(shiny.maxRequestSize=180000*1024^2)
shinyServer(function(input, output, session) {
dataFetch <- reactive({
read.table(file="http://elections.huffingtonpost.com/pollster/api/v2/questions/00c%20-Pres-45-Trump%20-%20Job%20Approval%20-%20National/poll-responses-clean.tsv", header=TRUE, sep="\t")
})
selectPeopleUnique <- reactive({
unique(dataFetch()$sample_subpopulation)
})
selectPollsterUnique <- reactive({
unique(dataFetch()$survey_house)
})
selectModeUnique <- reactive({
unique(dataFetch()$mode)
})
selectPartisanUnique <- reactive({
unique(dataFetch()$partisan_affiliation)
})
output$selectPeople <- renderUI({
selectInput(inputId = "people_vars", label = h4("Population"), choices = selectPeopleUnique(), selected=c("Likely Voters", "Registered Voters", "Adults"), multiple=TRUE)
})
output$selectPollster <- renderUI({
selectInput(inputId = "pollster_vars", label = h4("Pollster"), choices = selectPollsterUnique(), selected=selectPollsterUnique(), multiple=TRUE)
})
output$selectMode <- renderUI({
selectInput(inputId = "mode_vars", label = h4("Mode"), choices = selectModeUnique(), selected="Live Phone", multiple=TRUE)
})
output$selectPartisan <- renderUI({
selectInput(inputId = "partisan_vars", label = h4("Partisan Affiliation"), choices = selectPartisanUnique(), selected="None", multiple=TRUE)
})
dataSubset <- reactive({
all.trump <- dataFetch()
filter(all.trump,
sample_subpopulation %in% c(input$people_vars),
mode %in% c(input$mode_vars),
survey_house %in% c(input$pollster_vars),
partisan_affiliation %in% c(input$partisan_vars)
)
})
output$trumppolltable <- renderDataTable({
DT::datatable(dataSubset())
})
trumpBCP <- reactive({
input$trumpbayes
isolate(approval.table <- dataSubset())
approval.table$Date <- as.Date(approval.table$end_date, format="%Y-%m-%d")
approve.bc <- bcp(y=approval.table$Approve, burnin=2000, mcmc=10000, w0=mean(approval.table$margin_of_error, na.rm=TRUE)/100, p0=input$prior)
approve.posterior.mean <- approve.bc$posterior.mean
approve.posterior.prob <- approve.bc$posterior.prob
approve.posterior.var <- approve.bc$posterior.var
approve.posterior.sd <- sqrt(approve.posterior.var)
approve.bayes.dataframe <- data.frame(approval.table$Date, approval.table$Approve, approve.posterior.mean, approve.posterior.prob, approve.posterior.sd)
colnames(approve.bayes.dataframe) <- c("Date", "Approve", "PosteriorMean", "PosteriorProb", "PosteriorSD")
notsure.frame <- approval.table[complete.cases(approval.table["Undecided"]),]
notsure.bc <- bcp(y=notsure.frame$Undecided, burnin=2000, mcmc=10000, w0=mean(notsure.frame$margin_of_error, na.rm=TRUE)/100, p0=input$prior)
notsure.posterior.mean <- notsure.bc$posterior.mean
notsure.posterior.prob <- notsure.bc$posterior.prob
notsure.posterior.var <- notsure.bc$posterior.var
notsure.posterior.sd <- sqrt(notsure.posterior.var)
notsure.bayes.dataframe <- data.frame(notsure.frame$Date, notsure.frame$Undecided, notsure.posterior.mean, notsure.posterior.prob, notsure.posterior.sd)
colnames(notsure.bayes.dataframe) <- c("Date", "Undecided", "PosteriorMean", "PosteriorProb", "PosteriorSD")
disapprove.bc <- bcp(y=approval.table$Disapprove, burnin=2000, mcmc=10000, w0=mean(approval.table$margin_of_error, na.rm=TRUE)/100, p0=input$prior)
disapprove.posterior.mean <- disapprove.bc$posterior.mean
disapprove.posterior.prob <- disapprove.bc$posterior.prob
disapprove.posterior.var <- disapprove.bc$posterior.var
disapprove.posterior.sd <- sqrt(disapprove.posterior.var)
disapprove.bayes.dataframe <- data.frame(approval.table$Date, approval.table$Disapprove, disapprove.posterior.mean, disapprove.posterior.prob, disapprove.posterior.sd)
colnames(disapprove.bayes.dataframe) <- c("Date", "Disapprove", "PosteriorMean", "PosteriorProb", "PosteriorSD")
total.frame <- data.frame(
c(approval.table$Date, notsure.frame$Date, approval.table$Date),
c(approval.table$Disapprove, notsure.frame$Undecided, approval.table$Approve),
c(disapprove.posterior.mean, notsure.posterior.mean, approve.posterior.mean),
c(disapprove.posterior.prob, notsure.posterior.prob, approve.posterior.prob),
c(disapprove.posterior.sd, notsure.posterior.sd, approve.posterior.sd),
c(rep("3. Disapprove", length(approval.table$Disapprove)), rep("2. Undecided", length(notsure.frame$Undecided)), rep("1. Approve", length(approval.table$Approve))),
c(approval.table$survey_house, notsure.frame$survey_house, approval.table$survey_house),
c(approval.table$mode, notsure.frame$mode, approval.table$mode)
)
colnames(total.frame) <- c("Date", "Rating", "PosteriorMean", "PosteriorProb", "PosteriorSd", "Type", "Pollster", "Mode")
total.frame$Hodder <- Hodder(total.frame$PosteriorMean)
total.frame$PosteriorProb <- total.frame$PosteriorProb*(total.frame$Hodder/abs(total.frame$Hodder+0.00001))*-1
total.frame
})
###Create Plots
ratingPlot <- reactive({
trump.bcp <- trumpBCP()
ggplot(trump.bcp, aes(Date, Rating)) +
geom_point(alpha=0.5, aes(colour=Type)) +
geom_line(aes(as.Date(Date, format="%Y-%m-%d"), PosteriorMean, colour=Type)) +
theme_light() +
scale_x_date("Date") +
scale_y_continuous("Rating %") +
scale_color_manual(values = rev(cols))
})
posteriorProbPlot <- reactive({
trump.bcp <- trumpBCP()
ggplot(trump.bcp, aes(Date, PosteriorProb)) +
geom_line(aes(colour=Type))+
theme_bw() +
scale_x_date("Date") +
scale_y_continuous("Probability", limits = c(-1, 1), breaks=seq(-1, 1, 0.25)) +
scale_color_manual(values = rev(cols))
})
output$approvalOutput <- renderPlot({
ratingPlot()
})
output$hover_infoapproval <- renderUI({
point.table <- trumpBCP()
hover <- input$plot_hoverapproval
point <- nearPoints(point.table, coordinfo=hover, threshold = 5, maxpoints = 1, addDist = TRUE)
if (nrow(point) == 0) return(NULL)
approval.only <- filter(point, Rating=="Approval")
disapproval.only <- filter(point, Rating=="Disapproval")
# calculate point position INSIDE the image as percent of total dimensions
# from left (horizontal) and from top (vertical)
left_pct <- (hover$x - hover$domain$left) / (hover$domain$right - hover$domain$left)
top_pct <- (hover$domain$top - hover$y) / (hover$domain$top - hover$domain$bottom)
# calculate distance from left and bottom side of the picture in pixels
left_px <- hover$range$left + left_pct * (hover$range$right - hover$range$left)
top_px <- hover$range$top + top_pct * (hover$range$bottom - hover$range$top)
# create style property fot tooltip
# background color is set so tooltip is a bit transparent
# z-index is set so we are sure are tooltip will be on top
style <- paste0("position:absolute; z-index:100; background-color: rgba(245, 245, 245, 0.85); ",
"left:", left_px + 2, "px; top:", top_px + 2, "px;")
# actual tooltip created as wellPanel
wellPanel(
style = style,
p(HTML(paste0("<b> Date: </b>", point$Date, "<br/>"))),
p(HTML(paste0("<b> Pollster: </b>", point$Pollster, "<br/>"))),
p(HTML(paste0("<b> Mode: </b>", point$Mode, "<br/>"))),
p(HTML(paste0("<b> Rating: </b>", point$Rating, "<b>%</b>", "<br/>")))
)
})
output$posteriorProbOutput <- renderPlot({
posteriorProbPlot()
})
output$hover_infoposteriorprob <- renderUI({
point.table <- trumpBCP()
hover <- input$plot_hoverposteriorprob
point <- nearPoints(point.table, coordinfo=hover, threshold = 5, maxpoints = 1, addDist = TRUE)
if (nrow(point) == 0) return(NULL)
# calculate point position INSIDE the image as percent of total dimensions
# from left (horizontal) and from top (vertical)
left_pct <- (hover$x - hover$domain$left) / (hover$domain$right - hover$domain$left)
top_pct <- (hover$domain$top - hover$y) / (hover$domain$top - hover$domain$bottom)
# calculate distance from left and bottom side of the picture in pixels
left_px <- hover$range$left + left_pct * (hover$range$right - hover$range$left)
top_px <- hover$range$top + top_pct * (hover$range$bottom - hover$range$top)
# create style property fot tooltip
# background color is set so tooltip is a bit transparent
# z-index is set so we are sure are tooltip will be on top
style <- paste0("position:absolute; z-index:100; background-color: rgba(245, 245, 245, 0.85); ",
"left:", left_px + 2, "px; top:", top_px + 2, "px;")
# actual tooltip created as wellPanel
wellPanel(
style = style,
p(HTML(paste0("<b> Date: </b>", point$Date, "<br/>",
"<b> Posterior Probability: </b>", point$PosteriorProb, "<br/>"
)))
)
})
output$downloadtrumpplot <- downloadHandler(
filename = function() { paste('TrumpBayes', '.jpg', sep='') },
content = function(file) {
ggsave(file,grid.arrange(ratingPlot(), posteriorProbPlot(), nrow=2), device='jpeg', dpi=300, width=12, height=7)
}
)
output$trumpchangepointtable <- renderDataTable({
DT::datatable(trumpBCP())
})
output$downloadtrumptable <- downloadHandler(
filename = function() { paste('TrumpBayes', '.csv', sep='') },
content = function(file
) {
write.csv(trumpBCP(), file)
}
)
})