/
weightit.fit.R
541 lines (470 loc) · 19.1 KB
/
weightit.fit.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#' Generate Balancing Weights with Minimal Input Processing
#'
#' @description
#' `weightit.fit()` dispatches one of the weight estimation methods
#' determined by `method`. It is an internal function called by
#' [weightit()] and should probably not be used except in special cases. Unlike
#' `weightit()`, `weightit.fit()` does not accept a formula and data
#' frame interface and instead requires the covariates and treatment to be
#' supplied as a numeric matrix and atomic vector, respectively. In this way,
#' `weightit.fit()` is to `weightit()` what [lm.fit()] is to [lm()] -
#' a thinner, slightly faster interface that performs minimal argument
#' checking.
#'
#' @param covs a numeric matrix of covariates.
#' @param treat a vector of treatment statuses.
#' @param method a string of length 1 containing the name of the method that
#' will be used to estimate weights. See [weightit()] for allowable options.
#' The default is `"glm"` for propensity score weighting using a
#' generalized linear model to estimate the propensity score.
#' @param s.weights a numeric vector of sampling weights. See the individual
#' pages for each method for information on whether sampling weights can be
#' supplied.
#' @param by.factor a factor variable for which weighting is to be done within
#' levels. Corresponds to the `by` argument in [weightit()].
#' @param estimand the desired estimand. For binary and multi-category
#' treatments, can be "ATE", "ATT", "ATC", and, for some methods, "ATO", "ATM",
#' or "ATOS". The default for both is "ATE". This argument is ignored for
#' continuous treatments. See the individual pages for each method for more
#' information on which estimands are allowed with each method and what
#' literature to read to interpret these estimands.
#' @param stabilize `logical`; whether or not to stabilize the weights.
#' For the methods that involve estimating propensity scores, this involves
#' multiplying each unit's weight by the proportion of units in their treatment
#' group. Default is `FALSE`.
#' @param focal when multi-category treatments are used and ATT weights are
#' requested, which group to consider the "treated" or focal group. This group
#' will not be weighted, and the other groups will be weighted to be more like
#' the focal group. Must be non-`NULL` if `estimand = "ATT"` or
#' `"ATC"`.
#' @param ps a vector of propensity scores. If specified, `method` will be
#' ignored and set to `"glm"`.
#' @param moments,int,subclass arguments to customize the weight estimation.
#' See [weightit()] for details.
#' @param is.MSM.method see [weightitMSM()]. Typically can be ignored.
#' @param missing `character`; how missing data should be handled. The
#' options depend on the `method` used. If `NULL`, `covs` covs
#' will be checked for `NA` values, and if present, `missing` will be
#' set to `"ind"`. If `""`, `covs` covs will not be checked for
#' `NA` values; this can be faster when it is known there are none.
#' @param verbose whether to print additional information output by the fitting
#' function.
#' @param include.obj whether to include in the output any fit objects created
#' in the process of estimating the weights. For example, with `method = "glm"`, the `glm` objects containing the propensity score model will be
#' included. See the individual pages for each method for information on what
#' object will be included if `TRUE`.
#' @param ... other arguments for functions called by `weightit.fit()`
#' that control aspects of fitting that are not covered by the above arguments.
#'
#' @returns
#' A `weightit.fit` object with the following elements:
#' \item{weights}{The estimated weights, one for each unit.}
#' \item{treat}{The values of the treatment variable.}
#' \item{estimand}{The estimand requested. ATC is recoded as ATT.}
#' \item{method}{The weight estimation method specified.}
#' \item{ps}{The estimated or provided propensity scores. Estimated propensity scores are returned for binary treatments and only when `method` is `"glm"`, `"gbm"`, `"cbps"`, `"super"`, or `"bart"`.}
#' \item{s.weights}{The provided sampling weights.}
#' \item{focal}{The focal treatment level if the ATT or ATC was requested.}
#' \item{fit.obj}{When `include.obj = TRUE`, the fit object.}
#' \item{info}{Additional information about the fitting. See the individual methods pages for what is included.}
#'
#' The `weightit.fit` object does not have specialized `print()`,
#' `summary()`, or `plot()` methods. It is simply a list containing
#' the above components. Use [as.weightit()] to convert it to a `weightit` object, which does have these methods. See Examples.
#'
#' @details
#' `weightit.fit()` is called by [weightit()] after the arguments to
#' `weightit()` have been checked and processed. `weightit.fit()`
#' dispatches the function used to actually estimate the weights, passing on
#' the supplied arguments directly. `weightit.fit()` is not meant to be
#' used by anyone other than experienced users who have a specific use case in
#' mind. The returned object contains limited information about the supplied
#' arguments or details of the estimation method; all that is processed by
#' `weightit()`.
#'
#' Less argument checking or processing occurs in `weightit.fit()` than
#' does in `weightit()`, which means supplying incorrect arguments can
#' result in errors, crashes, and invalid weights, and error and warning
#' messages may not be helpful in diagnosing the problem. `weightit.fit()`
#' does check to make sure weights were actually estimated, though.
#'
#' `weightit.fit()` may be most useful in speeding up simulation
#' simulation studies that use `weightit()` because the covariates can be
#' supplied as a numeric matrix, which is often how they are generated in
#' simulations, without having to go through the potentially slow process of
#' extracting the covariates and treatment from a formula and data frame. If
#' the user is certain the arguments are valid (e.g., by ensuring the estimated
#' weights are consistent with those estimated from `weightit()` with the
#' same arguments), less time needs to be spent on processing the arguments.
#' Also, the returned object is much smaller than a `weightit` object
#' because the covariates are not returned alongside the weights.
#'
#' @seealso
#' [weightit()], which you should use for estimating weights unless
#' you know better.
#'
#' [as.weightit()] for converting a `weightit.fit` object to a `weightit` object.
#'
#' @examples
#'
#' library("cobalt")
#' data("lalonde", package = "cobalt")
#'
#' # Balancing covariates between treatment groups (binary)
#' covs <- lalonde[c("age", "educ", "race", "married",
#' "nodegree", "re74", "re75")]
#' ## Create covs matrix, splitting any factors using
#' ## cobalt::splitfactor()
#' covs_mat <- as.matrix(splitfactor(covs))
#'
#' WF1 <- weightit.fit(covs_mat, treat = lalonde$treat,
#' method = "glm", estimand = "ATT")
#' str(WF1)
#'
#' # Converting to a weightit object for use with
#' # summary() and bal.tab()
#' W1 <- as.weightit(WF1, covs = covs)
#' W1
#' summary(W1)
#' bal.tab(W1)
#'
#' @export
weightit.fit <- function(covs, treat, method = "glm", s.weights = NULL, by.factor = NULL,
estimand = "ATE", focal = NULL, stabilize = FALSE,
ps = NULL, moments = NULL, int = FALSE,
subclass = NULL, is.MSM.method = FALSE, missing = NULL,
verbose = FALSE, include.obj = FALSE, ...) {
#Checks
if (!check_if_call_from_fun(weightit) && !check_if_call_from_fun(weightitMSM)) {
chk::chk_not_missing(covs, "`covs`")
chk::chk_matrix(covs)
chk::chk_numeric(covs)
chk::chk_not_missing(treat, "`treat`")
chk::chk_vector(treat)
chk::chk_numeric(treat)
chk::chk_not_any_na(treat)
chk::chk_flag(stabilize)
chk::chk_flag(verbose)
chk::chk_flag(include.obj)
if (length(treat) != nrow(covs)) {
.err("`treat` and `'covs` must contain the same number of units")
}
if (!has_treat_type(treat)) treat <- assign_treat_type(treat)
treat.type <- get_treat_type(treat)
.check_acceptable_method(method, msm = FALSE, force = FALSE)
if (is_not_null(ps)) {
chk::chk_vector(ps)
chk::chk_numeric(ps)
if (length(ps) != length(treat)) {
.err("`ps` and `treat` must be the same length")
}
method <- "glm"
}
if (is_null(s.weights)) {
s.weights <- rep(1, length(treat))
}
else {
chk::chk_vector(s.weights)
chk::chk_numeric(s.weights)
if (length(s.weights) != length(treat)) {
.err("`s.weights` and `treat` must be the same length")
}
}
if (is_null(by.factor)) {
by.factor <- factor(rep(1, length(treat)), levels = 1)
}
else {
chk::chk_factor(by.factor)
if (length(by.factor) != length(treat)) {
.err("`by.factor` and `treat` must be the same length")
}
}
#Process estimand and focal
estimand <- .process_estimand(estimand, method, treat.type)
f.e.r <- .process_focal_and_estimand(focal, estimand, treat)
focal <- f.e.r[["focal"]]
estimand <- f.e.r[["estimand"]]
.chk_null_or(missing, chk::chk_string)
if (is_null(missing)) {
if (anyNA(covs)) missing <- "ind"
else missing <- ""
}
else if (missing != "" && anyNA(covs)) {
missing <- .process_missing(missing, method, treat.type)
}
else missing <- ""
#Check subclass
if (is_not_null(subclass)) .check_subclass(method, treat.type)
#Process moments and int
moments.int <- .process_moments_int(moments, int, method)
moments <- moments.int[["moments"]]
int <- moments.int[["int"]]
}
else {
if (!has_treat_type(treat)) treat <- assign_treat_type(treat)
treat.type <- get_treat_type(treat)
}
out <- make_list(c("weights", "treat", "estimand", "method", "ps", "s.weights",
"focal", "fit.obj", "info"))
out$weights <- out$ps <- rep(NA_real_, length(treat))
if (include.obj) {
fit.obj <- make_list(levels(by.factor))
}
info <- make_list(levels(by.factor))
obj <- NULL
if (!is.function(method)) {
fun <- "weightit"
if (is.MSM.method) fun <- paste0(fun, "MSM")
if (is_null(ps))
fun <- paste0(fun, "2", method)
else
fun <- paste0(fun, "2ps")
if (!is.MSM.method) {
fun <- switch(treat.type,
"multinomial" = paste.(fun, "multi"),
"continuous" = paste.(fun, "cont"),
fun)
}
####
# if (FORMULA.TEST) fun <- paste0(".", fun)
####
# fun <- get1(fun, mode = "function")
}
for (i in levels(by.factor)) {
#Run method
if (!is.function(method)) {
obj <- do.call(fun,
alist(covs = covs,
treat = treat,
s.weights = s.weights,
subset = by.factor == i,
estimand = estimand,
focal = focal,
stabilize = stabilize,
subclass = subclass,
ps = ps,
moments = moments,
int = int,
missing = missing,
verbose = verbose,
...))
}
else if (is.MSM.method) {
obj <- weightitMSM2user(Fun = method,
covs.list = covs,
treat.list = treat,
s.weights = s.weights,
subset = by.factor == i,
stabilize = stabilize,
missing = missing,
verbose = verbose,
...)
}
else {
obj <- weightit2user(Fun = method,
covs = covs,
treat = treat,
s.weights = s.weights,
subset = by.factor == i,
estimand = estimand,
focal = focal,
stabilize = stabilize,
subclass = subclass,
ps = ps,
missing = missing,
moments = moments,
int = int,
verbose = verbose,
...)
}
#Extract weights
if (is_null(obj)) {
.err("no object was created. This is probably a bug,\n and you should report it at https://github.com/ngreifer/WeightIt/issues")
}
if (is_null(obj$w) || all(is.na(obj$w))) {
.wrn("no weights were estimated. This is probably a bug,\n and you should report it at https://github.com/ngreifer/WeightIt/issues")
}
if (any(!is.finite(obj$w))) {
.wrn("some weights were estimated as `NA`, which means a value was impossible to compute (e.g., Inf). Check for extreme values of the treatment or covariates and try removing them. Non-finite weights will be set to 0")
obj$w[!is.finite(obj$w)] <- 0
}
# else if (any(!is.finite(obj$w))) probably.a.bug()
out$weights[by.factor == i] <- obj$w
if (is_not_null(obj$ps)) {
out$ps[by.factor == i] <- obj$ps
}
if (include.obj) {
fit.obj[[i]] <- obj$fit.obj
}
info[[i]] <- obj$info
}
if (include.obj) {
if (nlevels(by.factor) == 1) {
fit.obj <- fit.obj[[1]]
}
out$fit.obj <- fit.obj
}
if (nlevels(by.factor) == 1) {
attr(out, "Mparts") <- obj$Mparts
}
if (is_not_null(info) && nlevels(by.factor) == 1) {
info <- info[[1]]
}
out$info <- info
if (all(is.na(out$ps))) {
out$ps <- NULL
}
out$treat <- treat
out$estimand <- estimand
out$method <- method
out$s.weights <- s.weights
out$focal <- focal
class(out) <- "weightit.fit"
out
}
weightitMSM.fit <- function(covs.list, treat.list, method = "glm", s.weights = NULL, by.factor = NULL,
estimand = "ATE", focal = NULL, stabilize = FALSE,
moments = NULL, int = FALSE,
subclass = NULL, is.MSM.method = FALSE, missing = NULL,
verbose = FALSE, include.obj = FALSE, ...) {
#Checks
if (!check_if_call_from_fun(weightitMSM)) {
chk::chk_not_missing(covs.list, "`covs.list`")
chk::chk_list(covs.list)
for (i in seq_along(covs.list)) {
chk::chk_matrix(covs.list[[i]], sprintf("`covs.list[[%s]]", i))
chk::chk_numeric(covs.list[[i]], sprintf("`covs.list[[%s]]", i))
}
chk::chk_not_missing(treat.list, "`treat.list`")
chk::chk_list(treat.list)
for (i in seq_along(treat.list)) {
chk::chk_vector(treat.list[[i]], sprintf("`treat.list[[%s]]", i))
chk::chk_numeric(treat.list[[i]], sprintf("`treat.list[[%s]]", i))
chk::chk_not_any_na(treat.list[[i]], sprintf("`treat.list[[%s]]", i))
}
chk::chk_flag(stabilize)
chk::chk_flag(verbose)
chk::chk_flag(include.obj)
for (i in seq_along(treat.list)) {
n <- length(treat.list[[i]])
if (nrow(covs.list[[i]]) != n) {
.err("treatment and covariates must have the same number of units")
}
if (anyNA(treat.list[[i]])) {
.err(sprintf("no missing values are allowed in the treatment variable. Missing values found in treat.list[[%s]]", i))
}
treat.list[[i]] <- assign_treat_type(treat.list[[i]])
if (anyNA(covs.list[[i]])) {
missing <- .process_missing(missing, method, get_treat_type(treat.list[[i]]))
}
else if (i == length(covs.list)) {
missing <- ""
}
}
.check_acceptable_method(method, msm = TRUE, force = FALSE)
if (is_null(s.weights)) {
s.weights <- rep(1, n)
}
else {
chk::chk_vector(s.weights)
chk::chk_numeric(s.weights)
if (length(s.weights) != n) {
.err("`s.weights` and `treat` must be the same length")
}
}
if (is_null(by.factor)) {
by.factor <- factor(rep(1, n), levels = 1)
}
else {
chk::chk_factor(by.factor)
if (length(by.factor) != n) {
.err("`by.factor` and `treat` must be the same length")
}
}
#Process moments and int
moments.int <- .process_moments_int(moments, int, method)
moments <- moments.int[["moments"]]
int <- moments.int[["int"]]
}
else {
for (i in seq_along(treat.list)) {
if (!has_treat_type(treat.list[[i]])) treat.list[[i]] <- assign_treat_type(treat.list[[i]])
}
}
out <- make_list(c("weights", "treat.list", "method", "s.weights",
"fit.obj", "info"))
out$weights <- rep(NA_real_, length(treat.list[[1]]))
if (include.obj) {
fit.obj <- make_list(levels(by.factor))
}
info <- make_list(levels(by.factor))
obj <- NULL
if (!is.function(method)) {
fun <- paste0("weightitMSM2", method)
}
for (i in levels(by.factor)) {
#Run method
if (!is.function(method)) {
obj <- do.call(fun,
alist(covs.list = covs.list,
treat.list = treat.list,
s.weights = s.weights,
subset = by.factor == i,
estimand = estimand,
focal = focal,
stabilize = stabilize,
subclass = subclass,
moments = moments,
int = int,
missing = missing,
verbose = verbose,
...))
}
else {
obj <- weightitMSM2user(Fun = method,
covs.list = covs.list,
treat.list = treat.list,
s.weights = s.weights,
subset = by.factor == i,
stabilize = stabilize,
missing = missing,
verbose = verbose,
...)
}
#Extract weights
if (is_null(obj)) {
.err("no object was created. This is probably a bug,\n and you should report it at https://github.com/ngreifer/WeightIt/issues")
}
if (is_null(obj$w) || all(is.na(obj$w))) {
.wrn("no weights were estimated. This is probably a bug,\n and you should report it at https://github.com/ngreifer/WeightIt/issues")
}
if (any(!is.finite(obj$w))) {
.wrn("some weights were estimated as `NA`, which means a value was impossible to compute (e.g., Inf). Check for extreme values of the treatment or covariates and try removing them. Non-finite weights will be set to 0")
obj$w[!is.finite(obj$w)] <- 0
}
# else if (any(!is.finite(obj$w))) probably.a.bug()
out$weights[by.factor == i] <- obj$w
if (include.obj) {
fit.obj[[i]] <- obj$fit.obj
}
info[[i]] <- obj$info
}
if (include.obj) {
out$fit.obj <- {
if (nlevels(by.factor) == 1) fit.obj[[1]]
else fit.obj
}
}
if (nlevels(by.factor) == 1) {
attr(out, "Mparts") <- obj$Mparts
}
if (is_not_null(info) && nlevels(by.factor) == 1) {
info <- info[[1]]
}
out$info <- info
out$treat.list <- treat.list
out$method <- method
out$s.weights <- s.weights
class(out) <- "weightitMSM.fit"
out
}