Skip to content

Add weights to GEE - #2090

Merged
josef-pkt merged 6 commits into
statsmodels:masterfrom
kshedden:gee-weights5
Jan 22, 2015
Merged

josef-pkt merged 6 commits into
statsmodels:masterfrom
kshedden:gee-weights5

Conversation

@kshedden

Copy link
Copy Markdown
Contributor

@josef-pkt

Copy link
Copy Markdown
Member

Does this correspond to aweigths or pweights in Stata?
It allows 3 types of weights:
"iweights, fweights, and pweights are allowed; see [U] 11.1.6 weight. Weights must be constant within panel." http://www.stata.com/manuals13/xtxtgee.pdf

From the change it looks like "Weighting to produce homogeneous variances" in the linked FAQ, i.e. specifying cross-sectional heteroscedasticity.

My guess is that this needs more unit tests, for bse/cov_params and whatever else might be affected by this.

@kshedden

Copy link
Copy Markdown
Contributor Author

These would be pweights. I am using them to analyze a survey data set in
which the observations have weights.

I checked the standard errors against stata and they agree pretty well, but
not perfectly. Our GEE standard errors have never agreed perfectly with
stata, presumably due to a df issue. We test perfectly against R for both
point estimates and standard errors using all the standard covariance
structures, links, etc. But R's GEE doesn't have weights, so I can't test
against that here.

I could include the test against Stata for standard errors as-is with a
very slack tolerance. Better yet, I could figure out how R and Stata
differ, and include an option to give either the R or Stata results.

On Tue, Nov 18, 2014 at 7:40 PM, Josef Perktold [email protected]
wrote:

Does this correspond to aweigths or pweights in Stata?
It allows 3 types of weights:
"iweights, fweights, and pweights are allowed; see [U] 11.1.6 weight.
Weights must be constant within panel."
http://www.stata.com/manuals13/xtxtgee.pdf

From the change it looks like "Weighting to produce homogeneous variances"
in the linked FAQ, i.e. specifying cross-sectional heteroscedasticity.

My guess is that this needs more unit tests, for bse/cov_params and
whatever else might be affected by this.

—
Reply to this email directly or view it on GitHub
#2090 (comment)
.

@josef-pkt

Copy link
Copy Markdown
Member

Better yet, I could figure out how R and Stata differ, and include an option to give either the R or Stata results.

based on my experience with figuring out what Stata is doing, I never tried xtgee in Stata:

  • check whether the bse or cov_params differs by a proportionality factor in the balanced case. For example, the not bias-corrected robust cov_params GEE uses a different scaling than cluster robust in GLM. GLM cluster robust differs from Stata's by (nobs - 1) / (nobs - k_vars) AFAIR (I make an adjustment in unit tests) , but it uses the same small sample correction (n_groups - 1) / .n_groups (I might not remember correctly this one).
  • GEE and GLM cluster robust differ in a non-proportional way in the case of unbalanced panels. There must be a larger difference in the way the unbalanced case is handled.

Also, for the case here for testing the effect of weights, it might be possible to check and write a unit test if there is an approximately constant difference of your results relative to Stata's comparing the case with weights with the one without.

In general: I haven't tried/managed yet to really figure out what adjustments Stata is doing for survey weights. The descriptions in the FAQ seems to point also in that direction, but that might only be used with the svy prefix.

@kshedden

Copy link
Copy Markdown
Contributor Author

Stata apparently multiplies the standard robust covariance matrix by sqrt(N / (N - g)), where N is the total sample size and g is the average group size. I have verified this on examples with constant and with varying group sizes. I don't see this documented anywhere or know of any reference that would explain why this should be done.

@kshedden

Copy link
Copy Markdown
Contributor Author

Also, probably already known, but Stata seems not to count the intercept when determining denominator dof for the scale parameter.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) when pulling da6f491 on kshedden:gee-weights5 into c8e980d on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) when pulling b2ea22f on kshedden:gee-weights5 into c8e980d on statsmodels:master.

@jseabold

Copy link
Copy Markdown
Member

Ready to merge?

@kshedden

Copy link
Copy Markdown
Contributor Author

I have a bit more code in my branch, will get it in tonight. Thanks.

On Fri, Nov 21, 2014 at 2:00 PM, Skipper Seabold [email protected]
wrote:

Ready to merge?

—
Reply to this email directly or view it on GitHub
#2090 (comment)
.

@kshedden

Copy link
Copy Markdown
Contributor Author

I think this is ready now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be done with scale_ddof? This should be mentioned here.

@josef-pkt

Copy link
Copy Markdown
Member

scale_ddof should be a fit keyword.
Actually, the pattern that I have in other models is that it should be a key in the cov_kwds dictionary, which would make for a bit more clumsy interface here. cov_kwds dict is convenient because it can flexibly handle different types of keywords that are needed for various cov_types.

Also in general, prefix naming convention would require ddof_scale instead of reversed.

Related: I don't use it directly in other robust sandwiches, I use small sample correction (which depends on the cov_type) and introduced a generic scale factor that allows users to arbitrarily scale the cov_params.

Comment thread statsmodels/genmod/tests/test_gee.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test to calculate the fit with ddof_scale.
AFAIU, it should give the same result, and will provide a test for the new keyword

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.08%) when pulling 4548113 on kshedden:gee-weights5 into c8e980d on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

(Just remembered what I wanted to ask earlier.)

Does the overall scaling of the weights effect any results?
My guess is not in GEE, since you are only using the estimating equations. Is there an overall scale that depends on the weights scaling?
Stata rescales the weights internally and in WLS we deviate from Stata for llf IIRC, where we use the same definition in WLS and GLS and we don't do any weight normalization as Stata does.

@kshedden

Copy link
Copy Markdown
Contributor Author

I think you are right. The estimates should not depend on the
normalization of the scale parameter (the estimates shouldn't depend on the
scale parameter at all).

On Sat, Nov 22, 2014 at 5:30 PM, Josef Perktold [email protected]
wrote:

(Just remembered what I wanted to ask earlier.)

Does the overall scaling of the weights effect any results?
My guess is not in GEE, since you are only using the estimating equations.
Is there an overall scale that depends on the weights scaling?
Stata rescales the weights internally and in WLS we deviate from Stata for
llf IIRC, where we use the same definition in WLS and GLS and we don't do
any weight normalization as Stata does.

—
Reply to this email directly or view it on GitHub
#2090 (comment)
.

@josef-pkt josef-pkt mentioned this pull request Jan 8, 2015
29 tasks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as general FYI: the fastest way in numpy to get an array with identical elements is empty and fill (there is a new function in recent numpy full that I haven't used yet and won't be compatible for us yet.)

@josef-pkt

Copy link
Copy Markdown
Member

I don't want to hold this up any longer, but I'm also not trying to figure out how the weights really work.
I'll just rely on unit tests and figuring it out later.

Is it ready to merge?

@kshedden

Copy link
Copy Markdown
Contributor Author

I think it's ready, I've been using this a lot. Thanks for the review.

On Wed, Jan 21, 2015 at 8:00 PM, Josef Perktold [email protected]
wrote:

I don't want to hold this up any longer, but I'm also not trying to figure
out how the weights really work.
I'll just rely on unit tests and figuring it out later.

Is it ready to merge?

—
Reply to this email directly or view it on GitHub
#2090 (comment)
.

@josef-pkt

Copy link
Copy Markdown
Member

Can you rebase and force push? It's rather far behind master.
Then, I'll hit the merge button.

Otherwise, I'll do the rebase before merging.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 81.96% when pulling 8a6bc11 on kshedden:gee-weights5 into 60b82bc on statsmodels:master.

@kshedden

Copy link
Copy Markdown
Contributor Author

Rebased and force pushed.

@josef-pkt

Copy link
Copy Markdown
Member

you should fetch from upstream, statsmodels master, before rebase so it rebased to the latest version.

A few commits behind master, but merging anyway. (no related/conflicting commits)

josef-pkt added a commit that referenced this pull request Jan 22, 2015
@josef-pkt
josef-pkt merged commit a8a31db into statsmodels:master Jan 22, 2015
@josef-pkt

Copy link
Copy Markdown
Member

maybe of related interest and application

http://support.sas.com/resources/papers/proceedings14/SAS166-2014.pdf

"
Paper SAS166-2014:
Weighted Methods for Analyzing Missing Data with the GEE Procedures

Missing observations caused by dropouts or skipped visits present a problem in studies of longitudinal data. When the analysis is restricted to complete cases and the missing data depend on previous responses, the generalized estimating equation (GEE) approach, which is commonly used when the population-average effect is of primary interest, can lead to biased parameter estimates. The new GEE procedure in SAS/STAT® 13.2 implements a weighted GEE method, which provides consistent parameter estimates when the dropout mechanism is correctly specified. When none of the data are missing, the method is identical to the usual GEE approach, which is available in the GENMOD procedure. This paper reviews the concepts and statistical methods. Examples illustrate how you can apply the GEE procedure to incomplete longitudinal data.
"

@kshedden

Copy link
Copy Markdown
Contributor Author

Interesting. I had followed the Stata docs which state that there is no application for subject-specific weights, so in #2090 I required cluster-level weights. It was easy to generalize and I now have a working branch for subject-level weights that I will PR later after some more testing.

The update formula step 5 on page 3 of this doc is wrong:

http://support.sas.com/resources/papers/proceedings14/SAS166-2014.pdf

The denominator also needs to include the weight. The correct formula is here:

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.409.9129&rep=rep1&type=pdf

@josef-pkt

Copy link
Copy Markdown
Member

Just in general:
other uses of weights

both would need to be observation specific, in the second if the outlier is an observation and not an individual

@josef-pkt josef-pkt mentioned this pull request Mar 9, 2015
@kshedden kshedden mentioned this pull request Jun 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants