Closed martinzuba closed 6 years ago
Thank you so much for letting me know about this. I'll have it fixed tonight and let you know. By "latest version", do you mean the version on CRAN or the development version on GitHub?
It looks like I already addressed this bug on the development version of cobalt
. Rather than check whether all of the weights are 0, I instead check whether the first weight is 0 and all the weights are the same. I was unable to reproduce your bug using the old code, though. Also I'm surprised WeightIt
was giving you weights that large. Try using the development version of cobalt
and let me know if the problem persists.
Thank you for your reply, Noah! I used the version on CRAN. I will test the development version once I'm back at work :-)
I am also somewhat puzzled by these large weights, my guess is they may be so large because treatment group is only 1/10th of the total sample, and the weighting method makes sure that treatmeant and control have same sum of weights.
Which method in WeightIt
were you using? To my knowledge none of them require the treated weights to sum to the control weights. It doesn't actually matter because weights can be multiplied by a scalar and have the same properties, but I want to make sure that's not a bug too!
Hi Noah! Back to work, at last.
Find attached some code to reproduce the problem with check_if_zero_weights
as well as a suggestion for a function that works for me.
Thank you so much for your help. I discovered what the problem was.
I was indeed a problem with apply
that only occurred if the names of the treatment levels were of different length. But it came down to me failing to appreciate what apply
does. I fed it a data.frame (w.t.mat
) which has a character
column (for the names of the weights) and a column of the same type as the unique values of the treatment (in your case, logical
). apply
runs down the data.frame by row and creates a vector from each row, then operates on that vector. The problem is that the vector must be one single type, and since one of the values (the name of the weights) was character
, it would coerce the other value (the value of the treatment level) to also be character
. When R does this, it added whitespace before TRUE
to make it " TRUE"
. Since no units had " TRUE"
as a treatment level, it failed to return a sensical value.
What I did instead was to use vapply
along the row indices and extract the name of the weights and the unique values of the treatment using the row and column indices. Now there is no coercing of types and the whole thing should work as expected. Thank you so much for telling me about this and doing the work to help me fix it.
I have updated the GitHub version of cobalt
so you can go ahead an install it and give it a try. If it works correctly, I'll close this issue.
Hi. I try to get a bal.tab with preprocessed output from weightit.
I receive the error message: "All weights are zero when treat = TRUE".
However, this is not the case, as all weights are above 1 and none are NA or NULL or whatever.
I have traced the problem to some odd behaviour of the apply function in combination with the check_if_zero function: The check whether all is zero yields "FALSE" if called outside the apply function and "TRUE" (incorrectly) if called via the apply function.
I use latest versions of weightit, cobalt (installed today) and R.
This is from the debugger which puts me in check_if_zero_weights().
Thanks for your help!
Martin