juba/questionr

wtd.var return negative or inconsistance result with hmisc::wtd

poidstotal opened this issue · 1 comments

sample test

var weight
652 0.3265260
504 0.4180193
695 0.2518987
<-------------------->
In such situation the weight sum to 1, and the normal result as reported by hmisc is:
Hmisc::wtd.var(test$var,test$weight)
[1] -1970814
Warning message:
In Hmisc::wtd.var(test$var, test$weight) :
only one effective observation; variance estimate undefined
<-------------------->
when is with questionar, there is no warning
questionr::wtd.var(test$var,test$weight)
[1] -1970814
<-------------------->
Normally in Hmisc, the issue is fixed by adding normwt=TRUE,
Hmisc::wtd.var(test$var,test$weight,normwt=TRUE)
[1] 10775.15
for the ML method, we have
Hmisc::wtd.var(test$var,test$weight,normwt=TRUE,method='ML')
[1] 7033.177
<-------------------->
The result produced by questionr is neighter of those
questionr::wtd.var(test$var,test$weight,normwt=TRUE)
[1] 10549.77

juba commented

In fact, both wtd.mean and wtd.var functions in questionr are exact copies of the corresponding Hmisc functions (as mentioned in the documentation). But wtd.var has been updated in Hmisc and I didn't see it.

I copied these functions because I wanted to make them available without importing the whole Hmisc package. It was a long time ago, and I think I wouldn't do it this way now.

So for the behavior of questionr::wtd.var, I'll advise you to use Hmisc::wtd.var instead. I'll remove the function from questionr in the next release.