ngreifer / WeightIt

WeightIt: an R package for propensity score weighting
https://ngreifer.github.io/WeightIt/
102 stars 12 forks source link

Spline functions not working with weightit function #38

Closed sunyeoplee closed 1 year ago

sunyeoplee commented 1 year ago

I see that the Weightit package should work with the spline functions like ns and bs. https://cran.r-project.org/web/packages/WeightIt/news/news.html "Formula interfaces now accept poly(x, .) and other matrix-generating functions of variables, including the rms-class-generating functions from the rms package (e.g., pol(), rcs(), etc.) (the rms package must be loaded to use these latter ones) and the basis-class-generating functions from the splines package (i.e., bs() and ns()). A bug in an early version of this was found by @ahinton-mmc."

However, it is throwing an error, "Error in str2lang(termtext) : :1:75: unexpected numeric constant 1: sex+ns(age, knots = c(quantile(age, probs = c(0.1, 0.5, 0.9), na.rm = T)))1" when I try to run a logistic regression with spline terms to estimate IPWs.

Is there a way around it?

The weightit function works well without the spline functions.

`fixedeffects_htn_dm <- 'sex + ns(age,knots=c(quantile(age, probs=c(0.10, 0.50, 0.90), na.rm=T))) + location + income_cat + exercise_freq + smoking_status + alcohol_freq + cci_cat + ns(pre_sbp,knots=c(quantile(pre_sbp, probs=c(0.10, 0.50, 0.90), na.rm=T))) + ns(pre_dbp,knots=c(quantile(pre_dbp, probs=c(0.10, 0.50, 0.90), na.rm=T))) + ns(pre_tc,knots=c(quantile(pre_tc, probs=c(0.10, 0.50, 0.90), na.rm=T))) + ns(pre_hmg,knots=c(quantile(pre_hmg, probs=c(0.10, 0.50, 0.90), na.rm=T))) + ns(pre_hmg,knots=c(quantile(pre_hmg, probs=c(0.10, 0.50, 0.90), na.rm=T))) + ns(pre_bmi,knots=c(quantile(pre_bmi, probs=c(0.10, 0.50, 0.90), na.rm=T))) + pre_proteinuria + ns(pre_ast,knots=c(quantile(pre_ast, probs=c(0.10, 0.50, 0.90), na.rm=T))) + ns(pre_alt,knots=c(quantile(pre_alt, probs=c(0.10, 0.50, 0.90), na.rm=T))) + ns(pre_ggt,knots=c(quantile(pre_ggt, probs=c(0.10, 0.50, 0.90), na.rm=T))) + ns(pre_to_baseline,knots=c(quantile(pre_to_baseline, probs=c(0.10, 0.50, 0.90), na.rm=T)))'

exposure <- 'PDC_80'

ipw <- weightit(as.formula(paste(exposure, '~', fixedeffects_htn_dm)), estimand='ATE', # average effect method='ps', # propensity score stabilize=TRUE, # stabilized weight missing='ind', # indicator variable data=dataset)`

ngreifer commented 1 year ago

Hello,

Are you sure this is a problem with WeightIt? I am able to run weightit() models with splines using the current version on CRAN. However, the error you got is an error I get with cobalt functions, like bal.tab(). If this is indeed a problem with cobalt, I have already fixed the issue; see here. Just install the development version of cobalt using remotes::install_github("ngreifer/cobalt"). If this is an issue with WeightIt, can you please show me a minimal reproducible example, e.g., using a dataset I have access to (like the lalonde dataset in cobalt)? As of now I cannot reproduce this error.

sunyeoplee commented 1 year ago

You are right. The issue was with cobalt, which was fixed once I installed the development version. Thank you so much for your help and maintaining the package so well.