ngreifer / cobalt

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

Problem with splines and bal.tab and love.plot #63

Closed markdanese closed 1 year ago

markdanese commented 1 year ago

I am using a spline in my propensity score model and I am getting an error in generating diagnostic output from bal.tab and love.plot. See below for a reproducible example. You can see that the version with the spline gives the error, but the version without the spline does not.

This appears to be a new error -- I have used models with splines successfully using Cobalt as recently as a few weeks ago. I will downgrade and confirm that it is an issue with the most recent package.

(Thanks for your immensely helpful packages -- I couldn't do my work without them.)

Restarting R session...

> library(WeightIt)
> library(cobalt)
 cobalt (Version 4.4.0, Build Date: 2022-08-13)
> library(splines)
> 
> test <- iris
> test$type <- ifelse(test$Species == "virginica", 1, 0)
> ps_nospline <- weightit(type ~ Sepal.Length + Sepal.Width, data = test)
> ps_spline <- weightit(type ~ Sepal.Length + ns(Sepal.Width, knots = 2), data = test)
> 
> bal.tab(ps_nospline)
Call
 weightit(formula = type ~ Sepal.Length + Sepal.Width, data = test)

Balance Measures
                 Type Diff.Adj
prop.score   Distance   0.3324
Sepal.Length  Contin.   0.3764
Sepal.Width   Contin.  -0.5088

Effective sample sizes
           Control Treated
Unadjusted  100.     50.  
Adjusted     70.94   14.84
> bal.tab(ps_spline)
Error in str2lang(termtext) : <text>:1:40: unexpected numeric constant
1: Sepal.Length+ns(Sepal.Width, knots = 2)1
                                           ^
ngreifer commented 1 year ago

Thank you so much for letting me know about this! I will look into it and get back to you.

markdanese commented 1 year ago

I can confirm that the previous version of cobalt doesn't have this problem. See below.

Restarting R session...

> library(WeightIt)
> library(cobalt)
 cobalt (Version 4.3.2, Build Date: 2022-01-19)
> library(splines)
> 
> test <- iris
> test$type <- ifelse(test$Species == "virginica", 1, 0)
> ps_nospline <- weightit(type ~ Sepal.Length + Sepal.Width, data = test)
> ps_spline <- weightit(type ~ Sepal.Length + ns(Sepal.Width, knots = 2), data = test)
> 
> bal.tab(ps_nospline)
Call
 weightit(formula = type ~ Sepal.Length + Sepal.Width, data = test)

Balance Measures
                 Type Diff.Adj
prop.score   Distance   0.3324
Sepal.Length  Contin.   0.3764
Sepal.Width   Contin.  -0.5088

Effective sample sizes
           Control Treated
Unadjusted  100.     50.  
Adjusted     70.94   14.84
> bal.tab(ps_spline)
Call
 weightit(formula = type ~ Sepal.Length + ns(Sepal.Width, knots = 2), 
    data = test)

Balance Measures
                                Type Diff.Adj
prop.score                  Distance   0.3324
Sepal.Length                 Contin.   0.3764
ns(Sepal.Width, knots = 2)1  Contin.   0.5088

Effective sample sizes
           Control Treated
Unadjusted  100.     50.  
Adjusted     70.94   14.84
ngreifer commented 1 year ago

Okay thanks, this should be fixed!

markdanese commented 1 year ago

Thank you. I installed from GitHub and there was no error. I appreciate your help and all your hard work on this package.