You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bad understanding in statsmodels.stats.weightstats.DescrStatsW #8254
changed the title [-]Bad calculation in statsmodels.stats.weightstats.DescrStatsW[/-][+]Bad understanding in statsmodels.stats.weightstats.DescrStatsW[/+]on Apr 27, 2022
weights are frequency or case weights
So, the total number of observations is the sum of weights.
This was written before we disambiguated weights.
GLM has freq_weights and var_weights which are the unambiguous names.
WLS weights are var_weights for inference. weightstats weights are freq_weights
eventually we need to rename this.
As a workaround
freq_weights can be normalized so that freq_weights.sum() = nobs = len(x), by dividing weights by the desired nobs.
Aside: unit tests check weights by replicating each nobs according to it's (integer)weight, but weights don't have to be integers.
docstring of class has
If degrees of freedom correction is used, then weights should add up to the
number of observations. ttest also assumes that the sum of weights
corresponds to the sample size.
Do you have a suggestion on how to explain this better in the docs?
Indeed, in the case of frequencies the weights sums up to nobs and I was thinking in my case, where my weights are not frequencies and also are already normalized, so it always will sums up to 1.
By the docstring, I thought that using ddof = 0 was the same as to not use ddof and thus the sum of weights could be not equal to nobs, but this is false.
My suggestion for the docstring is to clarify that the weights MUST sums to the number of observation, in any case. But it would be better if the class was edited to work with freq_weights and var_weights.
For var_weights you can also use WLS with a constant as the only regressor to get mean and mean inference. I did this to compute some regression equivalents to basic meta-analysis.
For var_weights in weightstats:
I'm not sure we can get all statistics that are in methods defined for var_weights. I never looked at those in detail.
WLS, GLM and meta-analysis are easier because they produce results that are well defined for var_weights.
(WLS also has an option to fix the scale. residual variance at 1 if the var_weights are assumed to reflect all uncertainty.)
Activity
[-]Bad calculation in statsmodels.stats.weightstats.DescrStatsW[/-][+]Bad understanding in statsmodels.stats.weightstats.DescrStatsW[/+]josef-pkt commentedon Apr 27, 2022
weightsare frequency or case weightsSo, the total number of observations is the sum of weights.
This was written before we disambiguated
weights.GLM has
freq_weightsandvar_weightswhich are the unambiguous names.WLS weights are
var_weightsfor inference.weightstatsweights arefreq_weightseventually we need to rename this.
As a workaround
freq_weights can be normalized so that
freq_weights.sum() = nobs = len(x), by dividing weights by the desired nobs.Aside: unit tests check weights by replicating each nobs according to it's (integer)weight, but weights don't have to be integers.
docstring of class has
Do you have a suggestion on how to explain this better in the docs?
alandema commentedon Apr 27, 2022
Indeed, in the case of frequencies the weights sums up to nobs and I was thinking in my case, where my weights are not frequencies and also are already normalized, so it always will sums up to 1.
By the docstring, I thought that using ddof = 0 was the same as to not use ddof and thus the sum of weights could be not equal to nobs, but this is false.
My suggestion for the docstring is to clarify that the weights MUST sums to the number of observation, in any case. But it would be better if the class was edited to work with freq_weights and var_weights.
josef-pkt commentedon Apr 28, 2022
For var_weights you can also use WLS with a constant as the only regressor to get mean and mean inference. I did this to compute some regression equivalents to basic meta-analysis.
For var_weights in weightstats:
I'm not sure we can get all statistics that are in methods defined for var_weights. I never looked at those in detail.
WLS, GLM and meta-analysis are easier because they produce results that are well defined for var_weights.
(WLS also has an option to fix the scale. residual variance at 1 if the var_weights are assumed to reflect all uncertainty.)
e.g. https://www.statsmodels.org/dev/examples/notebooks/generated/metaanalysis1.html
alandema commentedon Apr 28, 2022
I will have a look into WLS, thanks.