Skip to content

Commit c1a1a78

Browse files
committed
pkgdown updates; bug fix with cbps
1 parent 64c23d0 commit c1a1a78

File tree

7 files changed

+79
-75
lines changed

7 files changed

+79
-75
lines changed

DESCRIPTION

Lines changed: 1 addition & 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.4.0.9002
4+
Version: 1.4.0.9003
55
Authors@R: c(
66
person("Noah", "Greifer", role=c("aut", "cre"),
77
email = "[email protected]",

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ WeightIt News and Updates
1313

1414
* Fixed a bug in which messages about assuming which treatment level was "treated" were printed many times, including when bootstrapping.
1515

16+
* Fixed a bug in which CBPS for continuous treatments would not yield correct balance in the presence of some extreme density estimates.
17+
1618
* Typo fixes in vignettes and documentation.
1719

1820
# `WeightIt` 1.4.0

R/weightit2cbps.R

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,17 @@ weightit2cbps <- function(covs, treat, s.weights, estimand, focal, subset,
357357

358358
if (solver == "multiroot") {
359359
out <- suppressWarnings({
360-
try(rootSolve::multiroot(f = function(...) colMeans(psi_bal(...)),
361-
start = par_alpha,
362-
Xm = mod_covs,
363-
Xb = bal_covs,
364-
A = treat,
365-
SW = s.weights,
366-
rtol = reltol,
367-
atol = reltol,
368-
ctol = reltol),
369-
silent = TRUE)
360+
try(verbosely({
361+
rootSolve::multiroot(f = function(...) colMeans(psi_bal(...)),
362+
start = par_alpha,
363+
Xm = mod_covs,
364+
Xb = bal_covs,
365+
A = treat,
366+
SW = s.weights,
367+
rtol = reltol,
368+
atol = reltol,
369+
ctol = reltol)
370+
}, verbose = FALSE), silent = TRUE)
370371
})
371372

372373
if (!null_or_error(out) && utils::hasName(out, "root") &&
@@ -624,16 +625,17 @@ weightit2cbps.multi <- function(covs, treat, s.weights, estimand, focal, subset,
624625

625626
if (solver == "multiroot") {
626627
out <- suppressWarnings({
627-
try(rootSolve::multiroot(f = function(...) colMeans(psi_bal(...)),
628-
start = par_alpha,
629-
Xm = mod_covs,
630-
Xb = bal_covs,
631-
A = treat,
632-
SW = s.weights,
633-
rtol = reltol,
634-
atol = reltol,
635-
ctol = reltol),
636-
silent = TRUE)
628+
try(verbosely({
629+
rootSolve::multiroot(f = function(...) colMeans(psi_bal(...)),
630+
start = par_alpha,
631+
Xm = mod_covs,
632+
Xb = bal_covs,
633+
A = treat,
634+
SW = s.weights,
635+
rtol = reltol,
636+
atol = reltol,
637+
ctol = reltol)
638+
}, verbose = FALSE), silent = TRUE)
637639
})
638640

639641
if (!null_or_error(out) && utils::hasName(out, "root") &&
@@ -858,7 +860,7 @@ weightit2cbps.multi <- function(covs, treat, s.weights, estimand, focal, subset,
858860
weightit2cbps.cont <- function(covs, treat, s.weights, subset, missing, moments, int, verbose, ...) {
859861

860862
covs <- covs[subset, , drop = FALSE]
861-
treat <- treat[subset]
863+
treat <- as.numeric(treat[subset])
862864
s.weights <- s.weights[subset]
863865

864866
missing <- .process_missing2(missing, covs)
@@ -920,32 +922,25 @@ weightit2cbps.cont <- function(covs, treat, s.weights, subset, missing, moments,
920922

921923
s.weights <- s.weights / mean_fast(s.weights)
922924

923-
# dens.num <- dnorm(treat, log = TRUE)
924-
925-
# un_s2 <- mean((treat - mean(treat)) ^ 2)
926-
# un_p <- mean(treat)
927-
928-
squish_tol <- 25
925+
squish_tol <- 50
929926

930927
# Balance condition
931928
psi_bal <- function(B, Xm, Xb = Xm, A, SW) {
932929
un_s2 <- exp(B[1L])
933930
un_p <- B[2L]
934931
log.dens.num <- squish(dnorm(A, un_p, sqrt(un_s2), log = TRUE),
935-
lo = -squish_tol, hi = squish_tol)
932+
lo = -Inf, hi = squish_tol)
936933

937934
s2 <- exp(B[3L])
938935
p <- drop(Xm %*% B[-(1:3)])
939936
log.dens.denom <- squish(dnorm(A, p, sqrt(s2), log = TRUE),
940-
lo = -squish_tol, hi = squish_tol)
937+
lo = -squish_tol, hi = Inf)
941938

942939
w <- exp(log.dens.num - log.dens.denom)
943940

944941
cbind(SW * (A - un_p)^2 - un_s2,
945942
SW * (A - un_p),
946943
SW * (A - p)^2 - s2,
947-
# SW * (A - p),
948-
# SW * w * Xb,
949944
SW * w * A * Xb)
950945
}
951946

@@ -965,16 +960,17 @@ weightit2cbps.cont <- function(covs, treat, s.weights, subset, missing, moments,
965960

966961
if (solver == "multiroot") {
967962
out <- suppressWarnings({
968-
try(rootSolve::multiroot(f = function(...) colMeans(psi_bal(...)),
969-
start = par_alpha,
970-
Xm = mod_covs,
971-
Xb = bal_covs,
972-
A = treat,
973-
SW = s.weights,
974-
rtol = reltol,
975-
atol = reltol,
976-
ctol = reltol),
977-
silent = TRUE)
963+
try(verbosely({
964+
rootSolve::multiroot(f = function(...) colMeans(psi_bal(...)),
965+
start = par_alpha,
966+
Xm = mod_covs,
967+
Xb = bal_covs,
968+
A = treat,
969+
SW = s.weights,
970+
rtol = reltol,
971+
atol = reltol,
972+
ctol = reltol)
973+
}, verbose = FALSE), silent = TRUE)
978974
})
979975

980976
if (!null_or_error(out) && utils::hasName(out, "root") &&
@@ -1009,10 +1005,7 @@ weightit2cbps.cont <- function(covs, treat, s.weights, subset, missing, moments,
10091005

10101006
p <- drop(Xm %*% B[-(1:3)])
10111007

1012-
cbind(#SW * (A - un_p)^2 - un_s2,
1013-
#SW * (A - un_p),
1014-
#SW * (A - p)^2 - s2,
1015-
SW * (A - p) * Xm)
1008+
cbind(SW * (A - p) * Xm)
10161009
}
10171010

10181011
# Combine LR and balance
@@ -1070,11 +1063,13 @@ weightit2cbps.cont <- function(covs, treat, s.weights, subset, missing, moments,
10701063

10711064
un_s2 <- exp(par_out[1L])
10721065
un_p <- par_out[2L]
1073-
log.dens.num <- dnorm(treat, un_p, sqrt(un_s2), log = TRUE)
1066+
log.dens.num <- squish(dnorm(treat, un_p, sqrt(un_s2), log = TRUE),
1067+
lo = -Inf, hi = squish_tol)
10741068

10751069
s2 <- exp(par_out[3L])
10761070
p <- drop(mod_covs %*% par_out[-(1:3)])
1077-
log.dens.denom <- dnorm(treat, p, sqrt(s2), log = TRUE)
1071+
log.dens.denom <- squish(dnorm(treat, p, sqrt(s2), log = TRUE),
1072+
lo = -squish_tol, hi = Inf)
10781073

10791074
w <- exp(log.dens.num - log.dens.denom)
10801075

@@ -1197,7 +1192,7 @@ weightitMSM2cbps <- function(covs.list, treat.list, s.weights, subset, missing,
11971192
})
11981193
})
11991194

1200-
squish_tol <- 25
1195+
squish_tol <- 50
12011196

12021197
get_w <- lapply(seq_along(treat.list), function(i) {
12031198
switch(treat.types[i],
@@ -1296,15 +1291,16 @@ weightitMSM2cbps <- function(covs.list, treat.list, s.weights, subset, missing,
12961291

12971292
if (solver == "multiroot") {
12981293
out <- suppressWarnings({
1299-
try(rootSolve::multiroot(f = function(...) colMeans(psi_bal(...)),
1300-
start = par_alpha,
1301-
X.list = covs.list,
1302-
A.list = treat.list,
1303-
SW = s.weights,
1304-
rtol = reltol,
1305-
atol = reltol,
1306-
ctol = reltol),
1307-
silent = TRUE)
1294+
try(verbosely({
1295+
rootSolve::multiroot(f = function(...) colMeans(psi_bal(...)),
1296+
start = par_alpha,
1297+
X.list = covs.list,
1298+
A.list = treat.list,
1299+
SW = s.weights,
1300+
rtol = reltol,
1301+
atol = reltol,
1302+
ctol = reltol)
1303+
}, verbose = FALSE), silent = TRUE)
13081304
})
13091305

13101306
if (!null_or_error(out) && out$estim.precis < 1e-5) {

R/weightit2ebal.R

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,14 @@ weightit2ebal <- function(covs, treat, s.weights, subset, estimand, focal,
241241

242242
if (solver == "multiroot") {
243243
out <- suppressWarnings({
244-
try(rootSolve::multiroot(f = gradient.EB,
245-
start = coef_start,
246-
S = s.weights_t, C = C, Q = Q,
247-
rtol = reltol,
248-
atol = reltol,
249-
ctol = reltol),
250-
silent = TRUE)
244+
try(verbosely({
245+
rootSolve::multiroot(f = gradient.EB,
246+
start = coef_start,
247+
S = s.weights_t, C = C, Q = Q,
248+
rtol = reltol,
249+
atol = reltol,
250+
ctol = reltol)
251+
}, verbose = FALSE), silent = TRUE)
251252
})
252253

253254
if (!null_or_error(out) && utils::hasName(out, "root") &&
@@ -527,11 +528,12 @@ weightit2ebal.cont <- function(covs, treat, s.weights, subset, missing, moments,
527528

528529
if (solver == "multiroot") {
529530
out <- suppressWarnings({
530-
try(rootSolve::multiroot(f = gradient.EB,
531-
start = coef_start,
532-
S = s.weights, C = C, Q = Q,
533-
maxiter = 20),
534-
silent = TRUE)
531+
try(verbosely({
532+
rootSolve::multiroot(f = gradient.EB,
533+
start = coef_start,
534+
S = s.weights, C = C, Q = Q,
535+
maxiter = 20)
536+
}, verbose = FALSE), silent = TRUE)
535537
})
536538

537539
if (!null_or_error(out) && utils::hasName(out, "root") &&
@@ -743,11 +745,12 @@ weightit2ebal.cont <- function(covs, treat, s.weights, subset, missing, moments,
743745
#
744746
# if (solver == "multiroot") {
745747
# out <- suppressWarnings({
746-
# try(rootSolve::multiroot(f = gradient.EB,
748+
# try(verbosely({
749+
# rootSolve::multiroot(f = gradient.EB,
747750
# start = coef_start,
748751
# S = s.weights, C = C, Q = Q,
749-
# maxiter = 20),
750-
# silent = TRUE)
752+
# maxiter = 20)
753+
# }, verbose = FALSE), silent = TRUE)
751754
# })
752755
#
753756
# if (!null_or_error(out) && utils::hasName(out, "root") &&

_pkgdown.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
template:
22
bootstrap: 5
3+
math-rendering: mathjax
4+
bslib:
5+
primary: "#00612E"
36
url: https://ngreifer.github.io/WeightIt/
47
reference:
58
- title: Estimate weights

pkgdown/favicon/favicon.svg

Lines changed: 1 addition & 1 deletion
Loading

vignettes/installing-packages.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ knitr::opts_chunk$set(eval = FALSE)
2121

2222
*WeightIt* is a wrapper for several other packages that aid in estimating balancing weights. In many ways, this is *WeightIt*'s strength, because it is easy to try out several weighting methods without having to learn a completely new syntax for each one. One weakness of this is that when one of these packages is not available (e.g., on CRAN), the method that relies on that package cannot be used.
2323

24-
This document explains how to install each package *WeightIt* uses. You do not need to install every single one; you only need the one you want to use. For example, the *miseam* package provides support for logistic regression with missing data, but if you have no missing data or you don't want to use the approach implemented in *miseam*, you don't need to install it. *WeightIt* strongly depends on a few packages, which are automatically installed along with *WeightIt*, so you don't need to worry about installing them separately; these are not listed here.
24+
This document explains how to install each package *WeightIt* uses. You do not need to install every single one; you only need the one you want to use. For example, the *misaem* package provides support for logistic regression with missing data, but if you have no missing data or you don't want to use the approach implemented in *misaem*, you don't need to install it. *WeightIt* strongly depends on a few packages, which are automatically installed along with *WeightIt*, so you don't need to worry about installing them separately; these are not listed here.
2525

2626
Below we note each method (by name and by the input to the `method` argument of `weightit()`) and how to install the required packages either from CRAN or otherwise when the CRAN version is not available. In many cases, this involves installing the package from the author's GitHub repository, which requires the *pak* package, which contains the function `pkg_install()`.
2727

0 commit comments

Comments
 (0)