Skip to content

ENH/REF/TST: prob_weights versus Stata pweights #4234

Open

Description

@josef-pkt

bottom line: I might not agree with Stata's definition of pweights

  • Stata seems to use aweights + HC1
    (according to comments in Stata documentation and reverse engineering the results

  • I think prob_weights should be freq_weights (scaled to nobs) + HC1 (or other robust cov_type)

test case TestGlmPoissonPwNr (which uses vcv robust in Stata, i.e. HC0/HC1), currently has xfail (known failures)

If I switch to var_weights + HC1, then the test passes for bse with corr_fact = np.sqrt((nobs-1)/nobs)
The version to check this

class TestGlmPoissonPwNr(CheckWeight):
    @classmethod
    def setup_class(cls):
        fweights = [1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3]
        # faking aweights by using normalized freq_weights
        fweights = np.array(fweights)
        wsum = fweights.sum()
        nobs = len(cpunish_data.endog)
        fweights = fweights / wsum * nobs

        cls.res1 = GLM(cpunish_data.endog, cpunish_data.exog,
                    family=sm.families.Poisson(), var_weights=fweights).fit(cov_type='HC1')
        # compare with discrete, start close to save time
        #modd = discrete.Poisson(cpunish_data.endog, cpunish_data.exog)
        cls.res2 = res_stata.results_poisson_pweight_nonrobust

for bse:

(Pdb) corr_fact = np.sqrt(16/17)
(Pdb) corr_fact
0.97014250014533188
(Pdb) res1.bse /( corr_fact * res2.bse)
array([ 1.00000038,  1.        ,  0.99999991,  0.99999979,  0.99999997,
        1.00000012,  1.00000002])

however resid_pearson does not include var_weights in this case in Stata

(Pdb) res1.resid_pearson/np.sqrt(res1.model.var_weights) / resid_all['resid_pear
son']
array([ 1.00000012,  1.00000024,  1.00000006,  0.99999758,  1.00000031,
        0.99999996,  0.99999925,  1.00000049,  1.00000003,  1.00000014,
        1.00000029,  0.99999979,  1.00000002,  1.00000092,  0.99999964,
        1.00000018,  0.99999999])

pearson chi2 is strange (33 is sum of original weights before normalizing to nobs=17)
Stata uses the original sum of weights, not the normalized sum of weights.

(Pdb) res2.deviance_p / (res1.resid_pearson**2).sum() / (33/17)
0.99999994482509491

(Pdb) res2.deviance_p / res1.pearson_chi2 / (33/17)
0.99999994482509491

Conclusion: While we can reproduce Stata's results, I'm not sure we want to use the same definitions.
Alternative or to confirm: check what Stata's svy prefix does with simple inverse probability sampling.

Aside: freq_weights + HC0 agrees with Stata according to TestGlmPoissonFwHC

(I don't change TestGlmPoissonPwNr for now and it remains xfails)

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