-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_functions.R
242 lines (192 loc) · 7.05 KB
/
sim_functions.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
# Misc functions for simulations ---------------------------------------
# Generate baseline covariates -----------------------------------------
gen_baseline <- function(n, mus, sds, corr_matrix) {
assertive::assert_is_a_number(n)
assertive::assert_is_numeric(mus)
assertive::assert_is_numeric(sds)
assertive::assert_is_symmetric_matrix(corr_matrix)
cov_matrix <- Matrix::nearPD(corr_matrix)$mat * (sds %*% t(sds))
mv_norm <- MASS::mvrnorm(n = n, mu = mus, Sigma = cov_matrix)
mv_norm[, 1] <- ifelse(mv_norm[, 1] > 0.7, 1, 0)
mv_norm[, 2] <- ifelse(mv_norm[, 2] > 0.5, 1, 0)
mv_norm[, 3] <- ifelse(mv_norm[, 3] > 0.9, 1, 0)
colnames(mv_norm) <- glue::glue("x{1:ncol(mv_norm)}")
mv_norm
}
# Proportion associated to an intercept --------------------------------
prop_treat <- function(x_matrix, beta, intercept) {
assertive::assert_is_matrix(x_matrix)
assertive::assert_is_numeric(beta)
assertive::assert_is_a_number(intercept)
coefs <- c(intercept, beta)
des_matrix <- cbind(rep(1, nrow(x_matrix)), x_matrix)
eta <- des_matrix %*% coefs
treat <- stats::rbinom(
n = nrow(x_matrix), size = 1, prob = stats::plogis(eta)
)
p_treat <- mean(treat)
tibble::tibble(treat = list(treat), p_treat = p_treat)
}
# Simulate the treatment -----------------------------------------------
gen_treat <- function(x_matrix, beta, int_grid, prop, delta) {
assertive::assert_is_matrix(x_matrix)
assertive::assert_is_numeric(beta)
assertive::assert_is_numeric(int_grid)
assertive::assert_is_a_number(prop)
assertive::assert_is_a_number(delta)
purrr::map_dfr(
.x = int_grid,
~ prop_treat(
x_matrix = x_matrix, beta = beta, intercept = .x
)
) %>%
dplyr::filter(p_treat > prop - delta & p_treat < prop + delta) %>%
slice(nrow(.)) %>%
.[["treat"]] %>%
unlist()
}
# Compute potential outcomes -------------------------------------------
potential_outcomes <- function(
x_matrix, treatment, beta, intercept_treat, p_treat,
delta_treat, alpha, intercept_outcome, alpha_treatment
) {
assertive::assert_is_matrix(x_matrix)
assertive::assert_is_numeric(treatment)
assertive::assert_is_numeric(beta)
assertive::assert_is_numeric(intercept_treat)
assertive::assert_is_a_number(p_treat)
assertive::assert_is_a_number(delta_treat)
assertive::assert_is_numeric(alpha)
assertive::assert_is_a_number(intercept_outcome)
assertive::assert_is_a_number(alpha_treatment)
# Design matrix
des_mat <- cbind(rep(1, n), treatment, x_matrix)
des_mat_ate <- cbind(rep(1, n), rep(1, n), x_matrix)
des_mat_att <- des_mat[des_mat[, "treatment"] == 1, ]
coef_1 <- c(intercept_outcome, alpha_treatment, alpha)
coef_2 <- c(intercept_outcome, 0, alpha)
# 1) ATE -------------------------------------------------------------
p1 <- mean(stats::plogis(des_mat_ate %*% coef_1))
p0 <- mean(stats::plogis(des_mat_ate %*% coef_2))
ate_true <- abs(p1 - p0)
# 2) ATT -------------------------------------------------------------
p1 <- mean(stats::plogis(des_mat_att %*% coef_1))
p0 <- mean(stats::plogis(des_mat_att %*% coef_2))
att_true <- abs(p1 - p0)
# Outcome
pp <- stats::plogis(des_mat %*% coef_1)
y <- stats::rbinom(n, 1, pp)
# Results in a final tibble
tibble::tibble(
intercept_outcome = intercept_outcome,
coef_treat = alpha_treatment,
prop_outcome = mean(y),
ate = ate_true,
att = att_true
)
}
# Choose intercept and the beta of treatment of the outcome's model ----
beta_treatment <- function(
x_matrix, treatment, beta, intercept_treat, p_treat,
delta_treat, alpha, outcome_grid,
p_outcome, delta_outcome, marginal_rd, delta_rd
) {
assertive::assert_is_matrix(x_matrix)
assertive::assert_is_numeric(treatment)
assertive::assert_is_numeric(beta)
assertive::assert_is_numeric(intercept_treat)
assertive::assert_is_a_number(p_treat)
assertive::assert_is_a_number(delta_treat)
assertive::assert_is_numeric(alpha)
assertive::assert_is_data.frame(outcome_grid)
assertive::assert_is_numeric(p_outcome)
assertive::assert_is_numeric(delta_outcome)
assertive::assert_is_numeric(marginal_rd)
assertive::assert_is_numeric(delta_rd)
purrr::map2_dfr(
.x = outcome_grid$interc, .y = outcome_grid$alpha_tr,
~ {
potential_outcomes(
x_matrix = x_matrix, treatment = treatment,
beta = beta, intercept_treat = intercept_treat,
p_treat = p_treat, delta_treat = delta_treat, alpha = alpha,
intercept_outcome = .x, alpha_treatment = .y
)
}
) %>%
# Filter by marginal OR
filter(
att > marginal_rd - delta_rd &
att < marginal_rd + delta_rd
) %>%
# Filter by proportion of events
filter(
prop_outcome > p_outcome - delta_outcome &
prop_outcome < p_outcome + delta_outcome
) %>%
slice(1)
}
# Simulate the superpopulation -----------------------------------------
sim_pop <- function(
n, mus, sds, corr_matrix,
beta, intercept_treat, p_treat, delta_treat,
alpha, outcome_grid, p_outcome, delta_outcome, marginal_rd, delta_rd
) {
assertive::assert_is_a_number(n)
assertive::assert_is_numeric(mus)
assertive::assert_is_numeric(sds)
assertive::assert_is_symmetric_matrix(corr_matrix)
assertive::assert_is_numeric(beta)
assertive::assert_is_numeric(intercept_treat)
assertive::assert_is_a_number(p_treat)
assertive::assert_is_a_number(delta_treat)
assertive::assert_is_numeric(alpha)
assertive::assert_is_data.frame(outcome_grid)
assertive::assert_is_numeric(p_outcome)
assertive::assert_is_numeric(delta_outcome)
assertive::assert_is_numeric(marginal_rd)
assertive::assert_is_numeric(delta_rd)
# Generate baseline matrix
x_matrix <- gen_baseline(
n = n, mus = mus, sds = sds, corr_matrix = corr_matrix
)
# Simulate treatment mechanism
treatment <- gen_treat(
x_matrix = x_matrix, beta = beta, int_grid = intercept_treat,
prop = p_treat, delta = delta_treat
)
# Choose intercept and treatment's coefficient of the outcome model
coef_out <- beta_treatment(
x_matrix = x_matrix, treatment = treatment, beta = beta,
intercept_treat = intercept_treat, p_treat = p_treat,
delta_treat = delta_treat, alpha = alpha,
outcome_grid = outcome_grid, p_outcome = p_outcome,
delta_outcome = delta_outcome, marginal_rd = marginal_rd,
delta_rd = delta_rd
)
# Simulate the outcome
des_matrix <- cbind(rep(1, n), treatment, x_matrix)
alphas <- c(
coef_out$intercept_outcome, coef_out$coef_treat,
alpha
)
pp <- stats::plogis(des_matrix %*% alphas)
y <- rbinom(n, 1, pp)
dd <- tibble::as_tibble(cbind(y, treatment, x_matrix))
# Store results into a tibble
tibble::tibble(
population = list(dd),
att_true = coef_out$att,
ate_true = coef_out$ate
)
}
# Sample from the superpopulation --------------------------------------
sample_dfs <- function(population, sample_size, n_samples) {
assertive::assert_is_data.frame(population)
assertive::assert_is_a_number(sample_size)
assertive::assert_is_a_number(n_samples)
# Get the population
purrr::rerun(
.n = n_samples, dplyr::sample_n(population, sample_size)
)
}