ngreifer / cobalt

Covariate Balance Tables and Plots - An R package for assessing covariate balance
https://ngreifer.github.io/cobalt/
73 stars 12 forks source link

bal.tab & love.plot functions throws error about the argument to treat #74

Closed sherwinkuah closed 11 months ago

sherwinkuah commented 1 year ago

Hello Noah! When I try to use the functions bal.tab or love.plot on an output from twang::mnps, it gives the following error. My treatment variable has 5 categories and is a factor. Could you advise on this?

Error in `bal.tab()`:
! The argument to `treat` must be a vector of treatment statuses or the (quoted) name of a variable in `data` that contains treatment statuses.
---
Backtrace:
     ▆
  1. ├─cobalt::bal.tab(mnps, un = TRUE, disp = c("m", "sd"))
  2. │ └─base::eval.parent(.call)
  3. │   └─base::eval(expr, p)
  4. │     └─base::eval(expr, p)
  5. ├─cobalt::bal.tab(x = `<mnps>`, un = TRUE, disp = c("m", "sd"))
  6. └─cobalt:::bal.tab.mnps(...)
  7.   ├─base::do.call("x2base.ps", c(list(x), args), quote = TRUE)
  8.   └─cobalt:::x2base.ps(...)
  9.     └─cobalt:::process_treat(ps[["treat"]], datalist = list(data, ps.data))
 10.       └─cobalt:::.process_vector(...)
 11.         └─cobalt:::.err(...)
 12.           └─chk::err(..., call = pkg_caller_call(start = 2))
Run rlang::last_trace(drop = FALSE) to see 1 hidden frame.

Code that I ran:

> mnps <- mnps(treatment ~ sex + age + , 
             data = psdf,
             estimand = "ATT",
             treatATT = "treat1",
             verbose = "False",
             stop.method = c("es.mean"), 
             n.trees = 2000)

> bal.tab(mnps, un = TRUE, disp = c("m", "sd"))

Versions

Thanks in advance, let me know what other info i can provide!

ngreifer commented 1 year ago

Hi, that looks like a bug, which I'll fix shortly. In the meantime, you can always use the formula interface to bal.tab(), which is only a tiny bit more cumbersome:

bal.tab(treatment ~ sex + age , 
        data = psdf,
        estimand = "ATT",
        focal = "treat1",
        weights = m)
sherwinkuah commented 1 year ago

Appreciate so much for the prompt reply and fix! Super helpful :) It works for me now.