Skip to content

Commit 0904da9

Browse files
committed
Cleaning, metadata
1 parent 038c974 commit 0904da9

File tree

9 files changed

+98
-43
lines changed

9 files changed

+98
-43
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: WeightIt
22
Type: Package
33
Title: Weighting for Covariate Balance in Observational Studies
4-
Version: 1.5.1
4+
Version: 1.5.1.9000
55
Authors@R: c(
66
person("Noah", "Greifer", role=c("aut", "cre"),
77
email = "noah.greifer@gmail.com",
@@ -46,6 +46,7 @@ Suggests:
4646
gbm (>= 2.1.9),
4747
dbarts (>= 0.9-29),
4848
misaem (>= 1.0.1),
49+
GPBayes,
4950
mlogit,
5051
dfidx,
5152
broom,

R/functions_for_processing.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@
365365

366366
.check_user_method <- function(method) {
367367
#Check to make sure it accepts treat and covs
368-
if (all(c("covs", "treat") %in% names(formals(method)))) {
368+
if (all(c("covs", "treat") %in% rlang::fn_fmls_names(method))) {
369369
}
370-
# else if (all(c("covs.list", "treat.list") %in% names(formals(method)))) {
370+
# else if (all(c("covs.list", "treat.list") %in% rlang::fn_fmls_names(method))) {
371371
# }
372372
else {
373373
.err("the user-provided function to `method` must contain `covs` and `treat` as named parameters")

R/glm_weightit_helpers.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@
519519
ctrl <- stats::glm.control
520520
}
521521

522-
model_call[setdiff(names(model_call), c(names(formals(stats::glm)), names(formals(ctrl))))] <- NULL
522+
model_call[setdiff(names(model_call), c(rlang::fn_fmls_names(stats::glm), rlang::fn_fmls_names(ctrl)))] <- NULL
523523
}
524524
else if (model == "lm") {
525525
model_call[[1L]] <- quote(stats::glm)
@@ -533,11 +533,11 @@
533533
model_call$na.action <- "na.fail"
534534
model_call$family <- "gaussian"
535535

536-
model_call[setdiff(names(model_call), c(names(formals(stats::glm)), names(formals(stats::glm.control))))] <- NULL
536+
model_call[setdiff(names(model_call), c(rlang::fn_fmls_names(stats::glm), rlang::fn_fmls_names(stats::glm.control)))] <- NULL
537537
}
538538
else if (model == "ordinal") {
539539
model_call[[1L]] <- .ordinal_weightit
540-
model_call[setdiff(names(model_call), names(formals(.ordinal_weightit)))] <- NULL
540+
model_call[setdiff(names(model_call), rlang::fn_fmls_names(.ordinal_weightit))] <- NULL
541541

542542
if (is_not_null(weightit)) {
543543
model_call$weights <- weightit[["weights"]] * weightit[["s.weights"]]
@@ -549,7 +549,7 @@
549549
}
550550
else if (model == "multinom") {
551551
model_call[[1L]] <- .multinom_weightit
552-
model_call[setdiff(names(model_call), names(formals(.multinom_weightit)))] <- NULL
552+
model_call[setdiff(names(model_call), rlang::fn_fmls_names(.multinom_weightit))] <- NULL
553553

554554
if (is_not_null(weightit)) {
555555
model_call$weights <- weightit[["weights"]] * weightit[["s.weights"]]
@@ -571,7 +571,7 @@
571571
model_call$robust <- vcov == "HC0"
572572

573573
model_call$cluster <- NULL
574-
model_call[setdiff(names(model_call), c(names(formals(survival::coxph)), names(formals(survival::coxph.control))))] <- NULL
574+
model_call[setdiff(names(model_call), c(rlang::fn_fmls_names(survival::coxph), rlang::fn_fmls_names(survival::coxph.control)))] <- NULL
575575
}
576576

577577
model_call
@@ -774,7 +774,7 @@
774774
if (is_null(fwb.args$verbose)) {
775775
fwb.args$verbose <- FALSE
776776
}
777-
fwb.args <- fwb.args[names(fwb.args) %in% names(formals(fwb::fwb))]
777+
fwb.args <- fwb.args[names(fwb.args) %in% rlang::fn_fmls_names(fwb::fwb)]
778778

779779
if (is_null(cluster)) {
780780
fwb_out <- eval(as.call(c(list(quote(fwb::fwb)), fwb.args)))

R/weightit2bart.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ weightit2bart <- function(covs, treat, s.weights, subset, estimand, focal, stabi
209209
t.lev <- get_treated_level(treat, estimand, focal)
210210
treat <- binarize(treat, one = t.lev)
211211

212-
A <- ...mget(setdiff(c(names(formals(dbarts::bart2)),
213-
names(formals(dbarts::dbartsControl))),
212+
A <- ...mget(setdiff(c(rlang::fn_fmls_names(dbarts::bart2),
213+
rlang::fn_fmls_names(dbarts::dbartsControl)),
214214
c("offset.test", "weights", "subset", "test")))
215215

216216
A[["data"]] <- treat
@@ -227,7 +227,7 @@ weightit2bart <- function(covs, treat, s.weights, subset, estimand, focal, stabi
227227
error = function(e) {
228228
.err(sprintf("(from `dbarts::bart2()`): %s",
229229
conditionMessage(e)),
230-
tidy = FALSE)
230+
tidy = FALSE)
231231
})
232232

233233
p.score <- fitted(fit)
@@ -256,8 +256,8 @@ weightit2bart.multi <- function(covs, treat, s.weights, subset, estimand, focal
256256

257257
ps <- make_df(levels(treat), nrow = length(treat))
258258

259-
A <- ...mget(setdiff(c(names(formals(dbarts::bart2)),
260-
names(formals(dbarts::dbartsControl))),
259+
A <- ...mget(setdiff(c(rlang::fn_fmls_names(dbarts::bart2),
260+
rlang::fn_fmls_names(dbarts::dbartsControl)),
261261
c("offset.test", "weights", "subset", "test")))
262262

263263
A[["formula"]] <- covs
@@ -316,8 +316,8 @@ weightit2bart.cont <- function(covs, treat, s.weights, subset, stabilize, missin
316316
density = ...get("density"),
317317
weights = s.weights)
318318

319-
A <- ...mget(setdiff(c(names(formals(dbarts::bart2)),
320-
names(formals(dbarts::dbartsControl))),
319+
A <- ...mget(setdiff(c(rlang::fn_fmls_names(dbarts::bart2),
320+
rlang::fn_fmls_names(dbarts::dbartsControl)),
321321
c("offset.test", "weights", "subset", "test")))
322322

323323
A[["formula"]] <- covs

R/weightit2energy.R

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#' \item{`moments`}{`integer`; the highest power of each covariate to be balanced. For example, if `moments = 3`, each covariate, its square, and its cube will be balanced. Can also be a named vector with a value for each covariate (e.g., `moments = c(x1 = 2, x2 = 4)`). Values greater than 1 for categorical covariates are ignored. Default is 0 to impose no constraint on balance.}
7373
#' \item{`int`}{`logical`; whether first-order interactions of the covariates are to be balanced. Default is `FALSE`.}
7474
#' \item{`tols`}{when `moments` is positive, a number corresponding to the maximum allowed standardized mean difference (for binary and multi-category treatments) or treatment-covariate correlation (for continuous treatments) allowed. Default is 0. Ignored when `moments = 0`.}
75+
#' \item{`min.w`}{the minimum allowable weight. Negative values (including `-Inf`) are allowed. Default is `1e-8`.}
7576
#' }
7677
#'
7778
#' For binary and multi-category treatments, the following additional arguments can be specified:
@@ -116,7 +117,7 @@
116117
#' @details
117118
#' Energy balancing is a method of estimating weights using
118119
#' optimization without a propensity score. The weights are the solution to a
119-
#' constrain quadratic optimization problem where the objective function
120+
#' constrained quadratic optimization problem where the objective function
120121
#' concerns covariate balance as measured by the energy distance and (for
121122
#' continuous treatments) the distance covariance.
122123
#'
@@ -260,6 +261,9 @@ weightit2energy <- function(covs, treat, s.weights, subset, estimand, focal,
260261
treat <- binarize(treat, one = t.lev)
261262

262263
n <- length(treat)
264+
265+
sw0 <- check_if_zero(s.weights)
266+
263267
diagn <- diag(n)
264268

265269
covs <- scale(covs)
@@ -310,8 +314,16 @@ weightit2energy <- function(covs, treat, s.weights, subset, estimand, focal,
310314

311315
#Constraints for positivity and sum of weights
312316
Amat <- cbind(diagn, s.weights_n_0, s.weights_n_1)
313-
lvec <- c(rep.int(min.w, n), 1, 1)
314-
uvec <- c(ifelse(check_if_zero(s.weights), min.w, Inf), 1, 1)
317+
lvec <- c(ifelse(sw0, 1, min.w), 1, 1)
318+
uvec <- c(ifelse(sw0, 1, Inf), 1, 1)
319+
320+
unbounded <- lvec == -Inf & uvec == Inf
321+
322+
if (any(unbounded)) {
323+
Amat <- Amat[, !unbounded, drop = FALSE]
324+
lvec <- lvec[!unbounded]
325+
uvec <- uvec[!unbounded]
326+
}
315327

316328
if (add_constraints) {
317329
#Exactly balance moments, interactions, and/or quantiles
@@ -347,8 +359,16 @@ weightit2energy <- function(covs, treat, s.weights, subset, estimand, focal,
347359
q <- 2 * (s.weights_n_1[t1] %*% d[t1, t0, drop = FALSE]) * s.weights_n_0[t0]
348360

349361
Amat <- cbind(diag(n0), s.weights_n_0[t0])
350-
lvec <- c(rep.int(min.w, n0), 1)
351-
uvec <- c(ifelse(check_if_zero(s.weights[t0]), min.w, Inf), 1)
362+
lvec <- c(ifelse(sw0[t0], 1, min.w), 1)
363+
uvec <- c(ifelse(sw0[t0], 1, Inf), 1)
364+
365+
unbounded <- lvec == -Inf & uvec == Inf
366+
367+
if (any(unbounded)) {
368+
Amat <- Amat[, !unbounded, drop = FALSE]
369+
lvec <- lvec[!unbounded]
370+
uvec <- uvec[!unbounded]
371+
}
352372

353373
if (add_constraints) {
354374
#Exactly balance moments, interactions, and/or quantiles
@@ -384,8 +404,16 @@ weightit2energy <- function(covs, treat, s.weights, subset, estimand, focal,
384404
q <- 2 * (s.weights_n_0[t0] %*% d[t0, t1, drop = FALSE]) * s.weights_n_1[t1]
385405

386406
Amat <- cbind(diag(n1), s.weights_n_1[t1])
387-
lvec <- c(rep.int(min.w, n1), 1)
388-
uvec <- c(ifelse(check_if_zero(s.weights[t1]), min.w, Inf), 1)
407+
lvec <- c(ifelse(sw0[t1], 1, min.w), 1)
408+
uvec <- c(ifelse(sw0[t1], 1, Inf), 1)
409+
410+
unbounded <- lvec == -Inf & uvec == Inf
411+
412+
if (any(unbounded)) {
413+
Amat <- Amat[, !unbounded, drop = FALSE]
414+
lvec <- lvec[!unbounded]
415+
uvec <- uvec[!unbounded]
416+
}
389417

390418
if (add_constraints) {
391419
#Exactly balance moments, interactions, and/or quantiles
@@ -494,8 +522,8 @@ weightit2energy <- function(covs, treat, s.weights, subset, estimand, focal,
494522
}
495523

496524
# Shrink tiny weights to 0
497-
if (abs(min.w) < .Machine$double.eps) {
498-
w[abs(w) < .Machine$double.eps] <- 0
525+
if (abs(min.w) < 1e-10) {
526+
w[abs(w) < 1e-10] <- 0
499527
}
500528

501529
opt.out$lambda <- lambda
@@ -541,10 +569,12 @@ weightit2energy.multi <- function(covs, treat, s.weights, subset, estimand, foca
541569
s.weights <- s.weights[subset]
542570

543571
n <- length(treat)
544-
levels_treat <- levels(treat)
572+
573+
sw0 <- check_if_zero(s.weights)
574+
545575
diagn <- diag(n)
546576

547-
covs <- scale(covs)
577+
levels_treat <- levels(treat)
548578

549579
min.w <- ...get("min.w", 1e-8)
550580
chk::chk_number(min.w)
@@ -561,6 +591,7 @@ weightit2energy.multi <- function(covs, treat, s.weights, subset, estimand, foca
561591
tols <- ...get("tols", 0)
562592
chk::chk_number(tols)
563593
tols <- abs(tols)
594+
covs <- scale(covs)
564595
}
565596

566597
treat_t <- matrix(0, nrow = n, ncol = length(levels_treat),
@@ -596,8 +627,16 @@ weightit2energy.multi <- function(covs, treat, s.weights, subset, estimand, foca
596627

597628
#Constraints for positivity and sum of weights
598629
Amat <- cbind(diagn, s.weights_n_t)
599-
lvec <- c(rep.int(min.w, n), rep.int(1, length(levels_treat)))
600-
uvec <- c(ifelse(check_if_zero(s.weights), min.w, Inf), rep.int(1, length(levels_treat)))
630+
lvec <- c(ifelse(sw0, 1, min.w), rep.int(1, length(levels_treat)))
631+
uvec <- c(ifelse(sw0, 1, Inf), rep.int(1, length(levels_treat)))
632+
633+
unbounded <- lvec == -Inf & uvec == Inf
634+
635+
if (any(unbounded)) {
636+
Amat <- Amat[, !unbounded, drop = FALSE]
637+
lvec <- lvec[!unbounded]
638+
uvec <- uvec[!unbounded]
639+
}
601640

602641
if (add_constraints) {
603642
#Exactly balance moments, interactions, and/or quantiles
@@ -639,8 +678,16 @@ weightit2energy.multi <- function(covs, treat, s.weights, subset, estimand, foca
639678
rowSums(s.weights_n_t[!in_focal, non_focal, drop = FALSE])
640679

641680
Amat <- cbind(diag(sum(!in_focal)), s.weights_n_t[!in_focal, non_focal])
642-
lvec <- c(rep.int(min.w, sum(!in_focal)), rep.int(1, length(non_focal)))
643-
uvec <- c(ifelse(check_if_zero(s.weights[!in_focal]), min.w, Inf), rep.int(1, length(non_focal)))
681+
lvec <- c(ifelse(sw0[!in_focal], 1, min.w), rep.int(1, length(non_focal)))
682+
uvec <- c(ifelse(sw0[!in_focal], 1, Inf), rep.int(1, length(non_focal)))
683+
684+
unbounded <- lvec == -Inf & uvec == Inf
685+
686+
if (any(unbounded)) {
687+
Amat <- Amat[, !unbounded, drop = FALSE]
688+
lvec <- lvec[!unbounded]
689+
uvec <- uvec[!unbounded]
690+
}
644691

645692
if (add_constraints) {
646693
#Exactly balance moments, interactions, and/or quantiles
@@ -734,8 +781,8 @@ weightit2energy.multi <- function(covs, treat, s.weights, subset, estimand, foca
734781
}
735782

736783
# Shrink tiny weights to 0
737-
if (abs(min.w) < .Machine$double.eps) {
738-
w[abs(w) < .Machine$double.eps] <- 0
784+
if (abs(min.w) < 1e-10) {
785+
w[abs(w) < 1e-10] <- 0
739786
}
740787

741788
opt.out$lambda <- lambda
@@ -860,8 +907,16 @@ weightit2energy.cont <- function(covs, treat, s.weights, subset, missing, verbos
860907
q[] <- q * s.weights
861908

862909
Amat <- cbind(diag(n), s.weights)
863-
lvec <- c(rep.int(min.w, n), n)
864-
uvec <- c(ifelse(sw0, min.w, Inf), n)
910+
lvec <- c(ifelse(sw0, 1, min.w), n)
911+
uvec <- c(ifelse(sw0, 1, Inf), n)
912+
913+
unbounded <- lvec == -Inf & uvec == Inf
914+
915+
if (any(unbounded)) {
916+
Amat <- Amat[, !unbounded, drop = FALSE]
917+
lvec <- lvec[!unbounded]
918+
uvec <- uvec[!unbounded]
919+
}
865920

866921
if (d.moments > 0) {
867922
d.covs <- .apply_moments_int_quantile(covs, moments = d.moments)
@@ -945,8 +1000,8 @@ weightit2energy.cont <- function(covs, treat, s.weights, subset, missing, verbos
9451000
w <- opt.out$x
9461001

9471002
# Shrink tiny weights to 0
948-
if (abs(min.w) < .Machine$double.eps) {
949-
w[abs(w) < .Machine$double.eps] <- 0
1003+
if (abs(min.w) < 1e-10) {
1004+
w[abs(w) < 1e-10] <- 0
9501005
}
9511006

9521007
opt.out$lambda <- lambda

man/dot-weightit_methods.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/macros/macros.Rd

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
% Rd macro for simplifying documentation writing
22

3-
\newcommand{\fun}{\code{\link[=#1]{#1()}}}
4-
53
% Because R packages need conditional use of packages in Suggests, any cross-reference to a doc in another package needs to be conditionally evaluated, too.
64

75
%\pkgfun{}{})tests whether the package is available, and, if so, produces a cross-reference to the function in the package; if not, the function name is displayed without a cross-reference. The first argument is the package, the second is the function name, e.g., \pkgfun{optmatch}{pairmatch}.
8-
\newcommand{\pkgfun}{\ifelse{\Sexpr[results=rd,stage=render]{requireNamespace("#1", quietly = TRUE)}}{\code{\link[#1:#2]{#1::#2()}}}{\code{#1::#2()}}}
9-
%\newcommand{\pkgfun}{\code{\link[#1:#2]{#2()}}}
6+
\newcommand{\pkgfun}{\Sexpr[results=rd,stage=render]{if (rlang::is_installed("#1")) r"(\code{\link[#1:#2]{#1::#2()}})" else r"(\code{#1::#2()})"}}
107

118
%E.g., \pkgfun{sandwich}{vcovCL} is the same as \code{\link[sandwich:vcovCL]{vcovCL}} if the sandwich package is installed and \code{vcovCL} if not.
129

man/method_energy.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/weightit.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)