-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
241 lines (241 loc) · 8.48 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
pacman::p_load(rstan, dagitty, DeclareDesign, CausalQueries, ri2, tidyverse, knitr )
mean <- 0
simplest_design <-
declare_model(N = 100, Y = rnorm(N, mean)) +
declare_inquiry(Q = mean) +
declare_estimator(Y ~ 1)
simplest_design
some_runs <- simplest_design |>
simulate_design(sims = 1000)
some_runs |>
kable(digits = 2) |>
kable_styling(font_size = 16)
pacman::p_load(rstan, dagitty, DeclareDesign, CausalQueries, ri2, tidyverse, knitr, kableExtra)
some_runs |>
kable(digits = 2) |>
kable_styling(font_size = 16)
some_runs |>
kable(digits = 2) |>
kable_styling(font_size = 16) %>%
tidy()
some_runs %>%
tidy()
some_runs
some_runs %>%
ggplot(aes(estimate)) %>%
geom_histogram()
some_runs %>%
ggplot(aes(estimate)) +
geom_histogram()
some_runs %>%
ggplot(aes(estimate, fill = p.value)) +
geom_histogram()
simplest_design |>
diagnose_design()
b <-1
N <- 100
design <-
declare_model(N = N, U = rnorm(N), potential_outcomes(Y ~ b * Z + U)) +
declare_assignment(Z = simple_ra(N), Y = reveal_outcomes(Y ~ Z)) +
declare_inquiry(ate = mean(Y_Z_1 - Y_Z_0)) +
declare_estimator(Y ~ Z, inquiry = "ate", .method = lm_robust)
b <- 0
N <- 100
design <-
declare_model(N = N, U = rnorm(N), potential_outcomes(Y ~ b * Z + U)) +
declare_assignment(Z = simple_ra(N), Y = reveal_outcomes(Y ~ Z)) +
declare_inquiry(ate = mean(Y_Z_1 - Y_Z_0)) +
declare_estimator(Y ~ Z, inquiry = "ate", .method = lm_robust)
redesign(design, N = 1:10*100)
redesign(design, N = 1:10*100) |>
diagnose_design()
simulate_design(design, sims = 500)
simulate_design(design, sims = 500) |>
ggplot(aes(p.value)) +
geom_histogram(color = "black", fill = "white") +
theme_light()
simulate_design(design, sims = 1000) |>
ggplot(aes(p.value)) +
geom_histogram(color = "black", fill = "white") +
theme_light()
simulate_design(design, sims = 10000) |>
ggplot(aes(p.value)) +
geom_histogram(color = "black", fill = "white") +
theme_light()
diag <- redesign(design, N = 1:10*100) |>
diagnose_design()
View(diag)
diag$diagnosands_df
b <- 0
N <- 100
design <-
declare_model(N = N, U = rnorm(N), potential_outcomes(Y ~ b * Z + U)) +
declare_assignment(Z = simple_ra(N), Y = reveal_outcomes(Y ~ Z)) +
declare_inquiry(ate = mean(Y_Z_1 - Y_Z_0)) +
declare_estimator(Y ~ Z, inquiry = "ate", .method = lm_robust)
simulate_design(design, sims = 10000) |>
ggplot(aes(p.value)) +
geom_histogram(color = "black", fill = "white") +
theme_light()
diag <- redesign(design, N = 1:10*100) |>
diagnose_design()
ggplot(diag$diagnosands_df, aes(N, power)) +
geom_point() +
geom_line()
sims <- simulate_design(design, sims = 10000)
geom_point() +
geom_line() +
theme_light()
ggplot(diag$diagnosands_df, aes(N, power)) +
scale_y_continuous("Power", limits = c(0,1)) +
geom_point() +
geom_line() +
theme_light()
diag <- redesign(design, N = (1:100)*10) |>
diagnose_design()
ggplot(diag$diagnosands_df, aes(N, power)) +
scale_y_continuous("Power", limits = c(0,1)) +
geom_point() +
geom_line() +
theme_light()
View(diag)
View(diag)
#| echo: true
b <- 0
N <- 100
design <- declare_model(N = N, U = rnorm(N), potential_outcomes(Y ~ b * Z + U)) +
declare_assignment(Z = simple_ra(N), Y = reveal_outcomes(Y ~ Z)) +
declare_inquiry(ate = mean(Y_Z_1 - Y_Z_0)) +
declare_estimator(Y ~ Z, inquiry = "ate", .method = lm_robust)
ggplot(sims, aes(p.value)) +
geom_histogram(color = "black", fill = "white") +
theme_light()
ggplot(sims, aes(p.value)) +
geom_denisty(color = "black", fill = "white") +
theme_light()
ggplot(sims, aes(p.value)) +
geom_density(color = "black", fill = "white") +
theme_light()
#| echo: true
sims <- simulate_design(design, sims = 1000)
ggplot(sims, aes(p.value)) +
geom_density(color = "black", fill = "white") +
theme_light()
#| echo: true
sims <- simulate_design(design, sims = 10000)
ggplot(sims, aes(p.value)) +
geom_density(color = "black", fill = "white") +
theme_light()
ggplot(sims, aes(p.value)) +
geom_density(fill = "white") +
theme_light()
ggplot(sims, aes(p.value)) +
geom_density(fill = "blue") +
theme_light()
ggplot(sims, aes(p.value)) +
geom_density(fill = "blue", color = "blue", alpha = 0.2 ) +
theme_light()
ggplot(sims, aes(p.value)) +
scale_x_continuous("p-value", scales::label_percent(1)) +
geom_density(fill = "blue", color = "blue", alpha = 0.2) +
theme_light()
ggplot(sims, aes(p.value)) +
scale_x_continuous("p-value", label = scales::label_percent(1)) +
geom_density(fill = "blue", color = "blue", alpha = 0.2) +
theme_light()
ggplot(sims, aes(p.value)) +
scale_x_continuous("p-value", label = scales::label_percent(1)) +
scale_y_continuous("Probability density") +
geom_density(fill = "blue", color = "blue", alpha = 0.2) +
theme_light()
c(100, 150, 200, 250, 300, 350, 400, 450, 500)/10
c(100, 150, 200, 250, 300, 350, 400, 450, 500)/100
1:10/2
2:10/2
2:10/2*100
2:10/200
2:10*200
2:10/2*100
2:10/2*1000
#| echo: false
pacman::p_load(rstan, dagitty, DeclareDesign, CausalQueries, ri2, tidyverse, knitr, kableExtra)
c(2:10/2*100,2:10/2*1000)
diagnose <- redesign(design, N = c(2:10/2*100,2:10/2*1000)) |>
diagnose_design()
ggplot(diagnose$diagnosands_df, aes(N, power)) +
scale_y_continuous("Power", limits = c(0,1)) + theme_light() +
geom_point() + geom_line()
2:10/2*100,2:10/2*1000
c(2:10/2*100,2:10/2*1000)
paste(c(2:10/2*100,2:10/2*1000), sep = ", ")
paste(c(2:10/2*100,2:10/2*1000), collapse = ", ")
diagnose <- redesign(design, N = c(100, 150, 200, 250, 300, 350, 400, 450, 500,
1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000)) |>
diagnose_design()
ggplot(diagnose$diagnosands_df, aes(N, power)) +
scale_y_continuous("Power", limits = c(0,1)) + theme_light() +
geom_point() + geom_line()
ggplot(diagnose$diagnosands_df, aes(N, power)) +
scale_x_log10("N") + scale_y_continuous("Power", limits = c(0,1)) + theme_light() +
geom_point() + geom_line()
n_s <- c(2:10/2*100,2:10/2*1000)
diagnose <- redesign(design, N = n_s) |>
diagnose_design()
ggplot(diagnose$diagnosands_df, aes(N, power)) +
scale_x_log10("N") + scale_y_continuous("Power", limits = c(0,1)) + theme_light() +
geom_point() + geom_line()
diagnose$diagnosands_df
#| echo: true
diagnose <- redesign(design, N = c(100, 150, 200, 250, 300, 350, 400, 450, 500,
1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000)) |>
diagnose_design()
#| echo: true
ggplot(diagnose$diagnosands_df, aes(N, power)) +
scale_x_log10("N") + scale_y_continuous("Power", limits = c(0,1)) + theme_light() +
geom_point() + geom_line()
#| echo: true
ggplot(diagnose$diagnosands_df, aes(N, power)) +
scale_x_log10("N", labels = scales::label_comma(1)) + scale_y_continuous("Power", limits = c(0,1), labels = scales::label_percent(1)) + theme_light() +
geom_point() + geom_line()
declare_model(schools = add_level(N = N_school, u = rnorm(N)),
classrooms = add_level(N = N_classroom, v = rnorm(N)),
students = add_level(N = N_student, w = rnorm(N)),
potential_outcomes(Y ~ b * Z + u + v + w))
design_hierarchical <- declare_model(schools = add_level(N = N_school, u = rnorm(N)),
classrooms = add_level(N = N_classroom, v = rnorm(N)),
students = add_level(N = N_student, w = rnorm(N)),
potential_outcomes(Y ~ b * Z + u + v + w))
design_hierarchical()
design_hierarchical <- declare_model(schools = add_level(N = N_school, u = rnorm(N)),
classrooms = add_level(N = N_classroom, v = rnorm(N)),
students = add_level(N = N_student, w = rnorm(N)),
potential_outcomes(Y ~ b * Z + u + v + w)) +
declare_assignment(Z = simple_ra(N), Y = reveal_outcomes(Y ~ Z))
design_hierarchical$model()
design_hierarchical$assignment()
design_hierarchical <- declare_model(schools = add_level(N = N_school, u = rnorm(N)),
classrooms = add_level(N = N_classroom, v = rnorm(N)),
students = add_level(N = N_student, w = rnorm(N)),
potential_outcomes(Y ~ b * Z + u + v + w)) +
declare_assignment(Z = simple_ra(N), Y = reveal_outcomes(Y ~ Z)) +
declare_inquiry(ate = mean(Y_Z_1 - Y_Z_0)) +
declare_estimator(Y ~ Z, inquiry = "ate", .method = lm_robust)
View(design_hierarchical)
design_hierarchical <- declare_model(schools = add_level(N = N_school, u = rnorm(N)),
classrooms = add_level(N = N_classroom, v = rnorm(N)),
students = add_level(N = N_student, w = rnorm(N)),
potential_outcomes(Y ~ b * Z + u + v + w))
design_hierarchical <- declare_model(schools = add_level(N = N_school, u = rnorm(N)),
classrooms = add_level(N = N_classroom, v = rnorm(N)),
students = add_level(N = N_student, w = rnorm(N)),
potential_outcomes(Y ~ b * Z + u + v + w))
design_hierarchical() %>% head()
N_school <- 20
N_classroom <- 5
N_student <- 5
b <- 4
design_hierarchical <- declare_model(schools = add_level(N = N_school, u = rnorm(N)),
classrooms = add_level(N = N_classroom, v = rnorm(N)),
students = add_level(N = N_student, w = rnorm(N)),
potential_outcomes(Y ~ b * Z + u + v + w))
design_hierarchical() %>% head()