Closed sky0502 closed 2 years ago
This is because you factor variable (race
) as a predictor which, after subsetting, only has one level ("white"
). R cannot process factors with only one level very well. This is not just true of WeightIt
; the same thing will happen if you try to use lm()
or glm()
or any other related function with the same input.
To solve the issue, don't include the subsetting variable in the model formula if it only has one level after the subset. I've edited the internal code of weightit()
to avoid this problem in the future using the solution described here, but it might be a while before the updated version is uploaded to CRAN.
Thank you so much!
Hi,
The motivation of my issue is to discard units outside a region of common support, similar to
discard
in {MatchIt}. Since there is no such command in {WeightIt}, I chose to calculate the ps first then pass on toweightit
. I encountered an error if a factor only have one level left after discarding units. Please see example below.I looked into the source code and discovered that the error was produced by
model.matrix
inget.covs.and.treat.from.formula
when callingweightit2ps
. However, I didn't see this design matrix being used subsequently but I could be wrong. Is there a way to get around the error?Thanks!