-
Notifications
You must be signed in to change notification settings - Fork 12
/
declare_potential_outcomes.R
378 lines (303 loc) · 13.2 KB
/
declare_potential_outcomes.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#' Potential Outcomes
#'
#' @inheritParams declare_internal_inherit_params
#'
#' @return a function that returns a data.frame
#'
#' @export
#'
#' @details
#'
#' A `declare_potential_outcomes` declaration returns a function. That function takes data and returns data with potential outcomes columns appended. These columns describe the outcomes that each unit would express if that unit were in the corresponding treatment condition.
#'
#' The potential outcomes function can sometimes be a stumbling block for users, as some are uncomfortable asserting anything in particular about the very causal process that they are conducting a study to learn about! We recommend trying to imagine what your preferred theory would predict, what an alternative theory would predict, and what your study would reveal if there were no differences in potential outcomes for any unit (i.e., all treatment effects are zero).
#'
#' @examples
#'
#' # Default handler
#'
#' # By default, there are two ways of declaring potential outcomes:
#' # as separate variables or using a formula:
#'
#' # As separate variables
#'
#' my_potential_outcomes <- declare_potential_outcomes(
#' Y_Z_0 = .05,
#' Y_Z_1 = .30 + .01 * age
#' )
#'
#' # Using a formula
#' my_potential_outcomes <- declare_potential_outcomes(Y ~ .05 + .25 * Z + .01 * age * Z)
#'
#' # conditions defines the "range" of the potential outcomes function
#' my_potential_outcomes <- declare_potential_outcomes(
#' formula = Y ~ .05 + .25 * Z + .01 * age * Z,
#' conditions = 1:4
#' )
#'
#' # Multiple assignment variables can be specified in conditions.
#' # A 2x2 factorial potential outcome:
#'
#' my_potential_outcomes <- declare_potential_outcomes(
#' formula = Y ~ .05 + .25 * Z1 + .01 * age * Z2,
#' conditions = list(Z1 = 0:1, Z2 = 0:1)
#' )
#'
#' ########################################################
#' # Custom handler
#'
#' my_po_function <- function(data) {
#'
#' data$Y_treated <- rexp(nrow(data), .2)
#' data$Y_untreated <- rexp(nrow(data), .4)
#' data
#' }
#'
#' custom_potential <- declare_potential_outcomes(handler = my_po_function)
#'
declare_potential_outcomes <- make_declarations(potential_outcomes_handler, "potential_outcomes");
### Default handler calls either the formula handler or non-formula handler
### this can be determind at declare time in the validation_fn, and the correct function returned instead
### If possible, we do so, even though much of the logic is essentially duplicated
### this makes tracing the execution in run_design much simpler
potential_outcomes_handler <- function(..., data, level) {
(function(formula, ...) UseMethod("potential_outcomes"))(..., data = data, level = level)
}
validation_fn(potential_outcomes_handler) <- function(ret, dots, label) {
declare_time_error_if_data(ret)
# Below is a similar redispatch strategy, only at declare time
validation_delegate <- function(formula=NULL, ...) {
potential_outcomes <- function(formula, ...) UseMethod("potential_outcomes", formula)
for (c in class(formula)) {
s3method <- getS3method("potential_outcomes", class(formula))
if (is.function(s3method)) return(s3method)
}
declare_time_error("Could not find appropriate implementation", ret)
}
s3method <- eval_tidy(quo(validation_delegate(!!!dots)))
# explicitly name all dots, for easier s3 handler validation
dots <- rename_dots(s3method, dots)
if ("level" %in% names(dots)) {
dots$level <- reveal_nse_helper(dots$level)
}
ret <- build_step(
currydata(s3method, dots, strictDataParam = attr(ret, "strictDataParam")),
handler = s3method,
dots = dots,
label = label,
step_type = attr(ret, "step_type"),
causal_type = attr(ret, "causal_type"),
call = attr(ret, "call"))
if (has_validation_fn(s3method)) ret <- validate(s3method, ret, dots, label)
ret
}
#' @param formula a formula to calculate potential outcomes as functions of assignment variables
#' @param conditions see \code{\link{expand_conditions}}
#' @param assignment_variables The name of the assignment variable
#' @param level a character specifying a level of hierarchy for fabricate to calculate at
#' @param data a data.frame
#' @importFrom fabricatr fabricate
#' @importFrom rlang quos := !! !!! as_quosure
#' @rdname declare_potential_outcomes
potential_outcomes.formula <- function(formula,
conditions = c(0, 1),
assignment_variables = "Z", # only used to provide a default - read from names of conditions immediately after.
data,
level = NULL,
label = outcome_variable) {
outcome_variable <- as.character(formula[[2]])
to_restore <- assignment_variables %i% colnames(data)
to_null <- setdiff(assignment_variables, to_restore)
# Build a single large fabricate call -
# fabricate( Z=1, Y_Z_1=f(Z), Z=2, Y_Z_2=f(Z), ..., Z=NULL)
condition_quos <- quos()
### If assn vars already present, swap them out
if (length(to_restore) > 0) {
restore_mangled <- paste(rep("_", max(nchar(colnames(data)))), collapse = "")
restore_mangled <- setNames(
lapply(to_restore, as.symbol),
paste0(".", restore_mangled, to_restore)
)
condition_quos <- c(condition_quos, quos(!!!restore_mangled))
}
# build call
expr = as_quosure(formula)
for (i in 1:nrow(conditions)) {
condition_values <- conditions[i, , drop = FALSE]
out_name <- paste0(outcome_variable, "_", paste0(assignment_variables, "_", condition_values, collapse = "_"))
condition_quos <- c(condition_quos, quos(!!!condition_values, !!out_name := !!expr) )
}
# clean up
if (length(to_restore) > 0) {
to_restore <- setNames(
lapply(names(restore_mangled), as.symbol),
to_restore
)
restore_mangled <- lapply(restore_mangled, function(x) NULL)
condition_quos <- c(condition_quos, quos(!!!to_restore), quos(!!!restore_mangled))
}
if (length(to_null) > 0) {
to_null <- lapply(setNames(nm = to_null), function(x) NULL)
condition_quos <- c(condition_quos, quos(!!!to_null))
}
if (is.character(level)) {
condition_quos <- quos(!!level := modify_level(!!!condition_quos))
}
### Actually do it and return
### Note ID_label=NA
structure(
fabricate(data = data,!!!condition_quos, ID_label = NA),
outcome_variable = outcome_variable,
assignment_variables = assignment_variables)
}
validation_fn(potential_outcomes.formula) <- function(ret, dots, label) {
dots$formula <- eval_tidy(dots$formula)
outcome_variable <- as.character(dots$formula[[2]])
if (length(dots$formula) < 3) {
declare_time_error("Must provide an outcome in potential outcomes formula", ret)
}
if ("ID_label" %in% names(dots)) {
declare_time_error("Must not pass ID_label.", ret)
}
if ("assignment_variables" %in% names(dots)) {
dots$assignment_variables <- reveal_nse_helper(dots$assignment_variables)
}
dots$conditions <- eval_tidy(quo(expand_conditions(!!!dots)))
dots$assignment_variables <- names(dots$conditions)
ret <- build_step(currydata(potential_outcomes.formula,
dots,
strictDataParam = attr(ret, "strictDataParam"),
cloneDots = FALSE
),
handler = potential_outcomes.formula,
dots = dots,
label = label,
step_type = attr(ret, "step_type"),
causal_type = attr(ret, "causal_type"),
call = attr(ret, "call"))
# Note that this sets a design_validation callback for later use!!! see below
# step_meta is the data that design_validation will use for design time checks
structure(ret,
potential_outcomes_formula = formula,
step_meta = list(outcome_variables = outcome_variable,
assignment_variables = names(dots$conditions)),
design_validation = pofdv)
}
# A design time validation
#
# Checks for unrevealed outcome variables.
#
# If there are any, inject a declare_reveal step after the latest assign/reveal of an assn variable
#
#
pofdv <- function(design, i, step){
if (i == length(design)) {
return(design)
}
this_step_meta <- attr(step, "step_meta")
check <- function(var_type, step_type, step_attr, callback = identity, from = 1, to = length(design)) {
vars <- this_step_meta[[var_type]]
assn_steps <- Filter(function(step_j) attr(step_j, "step_type") == step_type,
design[from:to])
for (step_j in assn_steps) {
if (is.null(step_meta <- attr(step_j, "step_meta"))) next;
step_assn <- step_meta[[step_attr]]
vars <- setdiff(vars, step_assn)
if (length(vars) == 0) return(c())
}
callback(vars)
}
unrevealed_outcomes <- check("outcome_variables", "reveal", "outcome_variables",
from = i + 1,
function(vars){
vars
}
)
if (length(unrevealed_outcomes) == 0) return(design)
# warning(
# "Outcome variables (", paste(unrevealed_outcomes, sep = ", "),
# ") were declared in a potential outcomes step (", attr(step, "label"),
# "), but never later revealed.", call. = FALSE)
prev_unassigned <- check("assignment_variables", "assignment", "assignment_variables", to = i - 1)
prev_unrevealed <- check("assignment_variables", "reveal", "outcome_variables", to = i - 1)
if (length(prev_unassigned %i% prev_unrevealed) == 0) {
new_step <- eval_tidy(quo(declare_reveal(outcome_variables = !!this_step_meta$outcome_variables,
assignment_variables = !!this_step_meta$assignment_variables,
label = !!paste("Autogenerated by", attr(step, "label"))) ))
attr(new_step, "auto-generated") <- TRUE
# warning("Attempting to inject a `declare_reveal(", this_step_meta$outcome_variables, ", ",
# this_step_meta$assignment_variables,
# ")` step after PO (", attr(step, "label"),
# ")", call. = FALSE)
design <- insert_step(design, new_step, after = i)
return(design)
}
unassigned_vars <- check("assignment_variables", "assignment", "assignment_variables", from = i + 1)
unrevealed_vars <- check("assignment_variables", "reveal", "outcome_variables", from = i + 1)
cant_find <- prev_unassigned %i% prev_unrevealed %i% unassigned_vars %i% unrevealed_vars
new_step <- eval_tidy(quo(declare_reveal(outcome_variables = !!this_step_meta$outcome_variables,
assignment_variables = !!this_step_meta$assignment_variables,
label = !!paste("Autogenerated by", attr(step, "label"))) ))
attr(new_step, "auto-generated") <- TRUE
for (step_j in design[length(design):(i + 1)]) {
if (is.null(step_meta <- attr(step_j, "step_meta"))) next;
if (attr(step_j, "step_type") == "assignment") {
if (any(step_meta$assignment_variables %in% attr(step, "step_meta")$assignment_variables)) {
design <- insert_step(design, new_step, after = step_j)
break;
}
}
else if (attr(step_j, "step_type") == "reveal") {
if (any(step_meta$outcome_variables %in% attr(step, "step_meta")$assignment_variables)) {
design <- insert_step(design, new_step, after = step_j)
break;
}
}
}
design
}
#' @importFrom fabricatr fabricate add_level modify_level
#' @rdname declare_potential_outcomes
potential_outcomes.NULL <- function(formula=stop("Not provided"), ..., data, level = NULL) {
if (is.character(level)) {
fabricate(data = data,!!level := modify_level(...))
} else {
fabricate(data = data, ..., ID_label = NA)
}
}
validation_fn(potential_outcomes.NULL) <- function(ret, dots, label){
if ("ID_label" %in% names(dots)) {
declare_time_error("Must not pass ID_label.", ret)
}
if ("" %in% names(dots)) {
declare_time_warn("Unnamed declared argument in potential outcome", ret)
}
ret
}
#' Expand assignment conditions
#'
#' Internal helper to eagerly build assignment conditions for potential outcomes.
#'
#' If conditions is a data.frame, it is returned unchanged
#'
#' Otherwise, if conditions is a list, it is passed to expand.grid for expansion to a data.frame
#'
#' Otherwise, if condition is something else, box it in a list with assignment_variables for names,
#' and pass that to expand.grid.
#'
#' @param conditions the conditions
#' @param assignment_variables the name of assignment variables, if conditions is not already named.
#' @return a data.frame of potential outcome conditions
#' @keywords internal
expand_conditions <- function() {
if (!is.data.frame(conditions)) {
if (!is.list(conditions)) {
conditions <- rep(list(conditions), length(assignment_variables))
conditions <- setNames(conditions, assignment_variables)
}
conditions <- expand.grid(conditions, stringsAsFactors = FALSE)
}
conditions
}
formals(expand_conditions) <- formals(potential_outcomes.formula)
formals(expand_conditions)["label"] <- list(NULL) # Fixes R CMD Check warning outcome is undefined