Skip to content

Bad understanding in statsmodels.stats.weightstats.DescrStatsW #8254

Open

Description

@alandema

Describe the bug

Trying to understand the calculations in DescrStatsW.tconfint_mean() I got confused with some method calls.

By the comments, why the number of observations should be equal to the to sum of weights?.

image

Also, in the method tconfint_mean, the dof is equal to (sum_weights - 1), but it should be equal to the number of observations -1.

image

Also, in the std_mean(), the divisions use sum_weights, but it should use the number of observations.

image

Activity

  1. changed the title [-]Bad calculation in statsmodels.stats.weightstats.DescrStatsW[/-] [+]Bad understanding in statsmodels.stats.weightstats.DescrStatsW[/+] on Apr 27, 2022
  2. josef-pkt commented on Apr 27, 2022

    @josef-pkt
    Member

    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?

  3. alandema commented on Apr 27, 2022

    @alandema
    Author

    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.

  4. josef-pkt commented on Apr 28, 2022

    @josef-pkt
    Member

    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

  5. alandema commented on Apr 28, 2022

    @alandema
    Author

    I will have a look into WLS, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions