-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_size.R
165 lines (143 loc) · 5.24 KB
/
sample_size.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
library(tidyverse)
library(lubridate)
library(magrittr)
library(cmdstanr)
library(posterior)
library(bayesplot)
library(tidybayes)
library(here)
library(ThemePark)
library(ggtext)
library(britpol)
# Load polls between 2017 and 2019 election
data("pollbase")
election_day_2017 <- ymd("2017-06-08")
election_day_2019 <- ymd("2019-12-12")
d <- subset(pollbase, end %in% election_day_2017:election_day_2019)
# Prep data
N_days <- as.numeric(election_day_2019 - election_day_2017) + 1
N_polls <- nrow(d)
N_pollsters <- length(unique(d$pollster))
y <- d$con
y_moe <- sqrt(y * (1 - y) / d$n)
start_election <- 0.423
end_election <- 0.436
poll_date <- as.numeric(d$end - election_day_2017) + 1
pollster_id <- as.numeric(as.factor(d$pollster))
nu <- 10
# for plotting
pollster_map <- levels(as.factor(d$pollster))
data_list <- list(
N_days = N_days,
N_polls = N_polls,
N_pollsters = N_pollsters,
start_election = start_election,
end_election = end_election,
y = y,
y_moe = y_moe,
poll_date = poll_date,
pollster_id = pollster_id,
nu = nu,
sample_size_reported = d$n
)
# fit cmdstan model
mod <- cmdstan_model("jackman_pooling_polls.stan")
fit <- mod$sample(data = data_list,
seed = 431343,
chains = 4,
parallel_chains = 4,
iter_warmup = 1000,
iter_sampling = 1000,
max_treedepth = 12)
# plot estimated sample size vs real
sample_rows <- sample(row.names(d), size = 100, replace = FALSE)
ratio_rvar <- as_draws_rvars(fit$draws("sample_ratio"))
ratio_rvar %>%
spread_draws(sample_ratio[j]) %>%
filter(j %in% sample_rows) %>%
ggplot(aes(y = reorder(as.factor(j), desc(sample_ratio)), x = sample_ratio)) +
stat_pointinterval(.width = c(.50, .80, .95),
show.legend = FALSE,
colour = barbie_theme_colors[7]) +
scale_y_discrete(labels = NULL) +
labs(title = "Ratio of effective to reported sample size",
subtitle = "One hundred randomly chosen polls",
y = NULL,
x = "Ratio",
caption = "This is a first attempt, so estimates are probably wrong
Source: {britpol} R package
Chart by sjwild.github.io. 2023/Sept/29") +
coord_flip() +
theme_barbie() +
theme(plot.caption = element_text(size = 10),
legend.position = "top",
legend.direction = "horizontal",
#plot.title = element_markdown(),
plot.background = element_rect(fill = barbie_theme_colors["panel"]),
axis.ticks.x = element_blank())
# Plot histogram of values
ratio_rvar %>%
spread_draws(sample_ratio[j]) %>%
ggplot(aes(x = sample_ratio)) +
geom_histogram(fill = barbie_theme_colors[7]) +
scale_y_continuous(labels = NULL) +
labs(title = "Histogram of the ratio of effective to reported sample size",
y = NULL,
x = "Ratio",
caption = "This is a first attempt, so estimates are probably wrong
Source: {britpol} R package
Chart by sjwild.github.io. 2023/Sept/29") +
theme_barbie() +
theme(plot.caption = element_text(size = 10),
legend.position = "top",
legend.direction = "horizontal",
plot.subtitle = element_markdown(),
plot.background = element_rect(fill = barbie_theme_colors["panel"]),
axis.ticks.y = element_blank())
sigma_rvar <- as_draws_rvars(fit$draws("sigma"))
sigma_rvar %>%
spread_draws(sigma[j]) %>%
ggplot(aes(y = j, x = sigma)) +
stat_pointinterval(.width = c(.50, .80, .95),
show.legend = FALSE,
colour = barbie_theme_colors[7]) +
geom_point(aes(x = y_moe, y = 1:457), data = data.frame(y_moe = y_moe)) +
scale_y_discrete(labels = NULL) +
labs(title = "Ratio of effective to reported sample size",
subtitle = "One hundred randomly chosen polls",
y = NULL,
x = "Ratio",
caption = "This is a first attempt, so estimates are probably wrong
Source: {britpol} R package
Chart by sjwild.github.io. 2023/Sept/29") +
coord_flip() +
theme_barbie() +
theme(plot.caption = element_text(size = 10),
legend.position = "top",
legend.direction = "horizontal",
#plot.title = element_markdown(),
plot.background = element_rect(fill = barbie_theme_colors["panel"]),
axis.ticks.x = element_blank())
# Plot sigma_pollster
sigma_pollster_rvar <- as_draws_rvars(fit$draws("sigma_pollster"))
sigma_pollster_rvar %>%
spread_draws(sigma_pollster[j]) %>%
ggplot(aes(y = reorder(as.factor(j), desc(sigma_pollster)), x = sigma_pollster)) +
stat_pointinterval(.width = c(.50, .80, .95),
show.legend = FALSE,
colour = barbie_theme_colors[7]) +
scale_y_discrete(labels = pollster_map,
breaks = 1:N_pollsters) +
labs(title = "Sigma pollster",
y = NULL,
x = "Size",
caption = "This is a first attempt, so estimates are probably wrong
Source: {britpol} R package
Chart by sjwild.github.io. 2023/Sept/29") +
theme_barbie() +
theme(plot.caption = element_text(size = 10),
legend.position = "top",
legend.direction = "horizontal",
#plot.title = element_markdown(),
plot.background = element_rect(fill = barbie_theme_colors["panel"]),
axis.ticks.x = element_blank())