ngreifer / WeightIt

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

Possible bug with bs() splines #9

Closed ahinton-mmc closed 4 years ago

ahinton-mmc commented 4 years ago

I was using bs() within weightit() to include spline terms in my model, but I went to re-run my code and it is no longer working. I get the following error:

Error in terms.formula(new.form) : invalid model formula in ExtractVars

I'm not sure this is a bug in WeightIt, it may be something I'm doing wrong. Any help would be appreciated!

Thanks!

require(WeightIt)
#> Loading required package: WeightIt
# bs() works
bs(women$height, df = 5)
#> Error in bs(women$height, df = 5): could not find function "bs"
# bs works with lm
summary(fm1 <- lm(weight ~ bs(height, df = 5), data = women))
#> Error in bs(height, df = 5): could not find function "bs"
# weightit works
weightit(weight ~ height, data = women, estimand="ATE", method="ps", stabilize=T, include.obj = T)
#> A weightit object
#>  - method: "ps" (propensity score weighting)
#>  - number of obs.: 15
#>  - sampling weights: none
#>  - treatment: continuous
#>  - covariates: height
# weightit with splines does not
weightit(weight ~ bs(height, df = 5), data = women, estimand="ATE", method="ps", stabilize=T, include.obj = T)
#> Error: All variables in formula must be variables in data or objects in the global environment.
#> Missing variables: bs(height, df = 5)

Created on 2020-01-02 by the reprex package (v0.2.1)

ngreifer commented 4 years ago

Thanks for letting me know! The issue is that a call to bs() produces a matrix with numbers as the column names, and since WeightIt process formulas differently from lm() to allow more customization, it didn't process those column names correctly. I've fixed this in the GitHub version of WeightIt, which you can install with devtools::install_github("ngrewifer/WeightIt"). I'll put it on CRAN early next week.

For future reference, when using reprex, you need to library() all the packages you're using, so the code you pasted was not really informative and it wasn't clear to me where the bs() function was.

ahinton-mmc commented 4 years ago

Thank you!!

Sent from Workspace ONE Boxer

On Jan 2, 2020 7:47 PM, Noah Greifer notifications@github.com wrote: This message originated outside of MaineHealth. Use caution when opening attachments, clicking links or responding to requests for information.

Thanks for letting me know! The issue is that a call to bs() produces a matrix with numbers as the column names, and since WeightIt process formulas differently from lm() to allow more customization, it didn't process those column names correctly. I've fixed this in the GitHub version of WeightIt, which you can install with devtools::install_github("ngrewifer/WeightIt"). I'll put it on CRAN early next week.

For future reference, when using reprex, you need to library() all the packages you're using, so the code you pasted was not really informative and it wasn't clear to me where the bs() function was.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_ngreifer_WeightIt_issues_9-3Femail-5Fsource-3Dnotifications-26email-5Ftoken-3DALXEKD3BBZNHVAP4VCA24JDQ32DLJA5CNFSM4KCGUNCKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH76QHA-23issuecomment-2D570419228&d=DwMCaQ&c=ST5Jxgx_zZ9nYuPWkSm01Luus8kzn0TCuX9tmQgnWms&r=__BxrAQqu05nqQiE9hB_rg&m=E9QBbXa8o_MgMftl3J7Bn0WcrFJ3Vz0KReHbrxroFZw&s=W9t84Dhr3g_pYFksZxvusbraioNsf49tgfLxz8d-Q_k&e=, or unsubscribehttps://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_notifications_unsubscribe-2Dauth_ALXEKDZVW2FAQM2CDAL3DO3Q32DLJANCNFSM4KCGUNCA&d=DwMCaQ&c=ST5Jxgx_zZ9nYuPWkSm01Luus8kzn0TCuX9tmQgnWms&r=__BxrAQqu05nqQiE9hB_rg&m=E9QBbXa8o_MgMftl3J7Bn0WcrFJ3Vz0KReHbrxroFZw&s=zZ5-Y9JN17XH-yTofFLHZRvbkF7fC7FMKHEtXlMFthc&e=.

CONFIDENTIALITY NOTICE: This email message, including any attachments, is for the use of the intended recipient(s) only and may contain information that is privileged, confidential, and prohibited from unauthorized disclosure under applicable law. If you are not the intended recipient of this message, any dissemination, distribution, or copying of this message is strictly prohibited. If you received this message in error, please notify the sender by reply email and destroy all copies of the original message and attachments.