Skip to content

const_idx handling in GEE margins and pep8 - #2833

Merged
josef-pkt merged 10 commits into
statsmodels:masterfrom
kshedden:gee_margins
May 30, 2016
Merged

josef-pkt merged 10 commits into
statsmodels:masterfrom
kshedden:gee_margins

Conversation

@kshedden

Copy link
Copy Markdown
Contributor

This PR addresses comments about const_idx handling made in #2817.

Also, pep8 fixes to GEE code and tests.

@kshedden

Copy link
Copy Markdown
Contributor Author

Also I modified the covariance computation logic so that the bias reduced robust covariance is not calculated unless requested (it involves n_obs x n_obs calculations so is expensive for large clusters)

@kshedden

kshedden commented Mar 6, 2016

Copy link
Copy Markdown
Contributor Author

@josef-pkt ready for review

@josef-pkt

Copy link
Copy Markdown
Member

I'll go over it tonight. thank for the ping


if self.grid:
return self.covariance_matrix_grid(endog_expal, index)
return self.covariance_matrix_grid(endog_expval, index)

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.

this looks like a bug fix, which might mean insufficient test coverage.
?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, flake8 picked this up. We don't use covariance_matrix internally anymore, instead we do everything through covariance_matrix_solve, which has decent test coverage. But covariance_matrix is useful for people who want to see the actual fitted covariance matrices. I will add some tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added a regression test for the place where the bug was fixed. We don't have a generic way to run through all of the covariance structures in a test right now, so it's not easy to have simple consistency checks like this. I will add better, more systematic testing of GEE in a future PR.

@josef-pkt

Copy link
Copy Markdown
Member

changes look fine, (I already read through them a few days ago when I wasn't logged into github)

about pep-8: did you use autoformatting?
Most of it looks good. We still follow in general the rule not to put spaces around power **.
The latest pep-8 version is more agnostic about putting spaces around operators, but so far we stick to the old rules.

One observation, not affected by the changes here but I just saw it:
_handle_constraint(self, mean_params, bcov) uses the naive bcov
Shouldn't this use whatever cov_type has been chosen as the default?

@kshedden

kshedden commented Mar 6, 2016

Copy link
Copy Markdown
Contributor Author

I now have it pulling the correct covariance matrix in handle_constraint. Good catch.

IIRC you now have generic code to handle constraints, so GEE-specific code should all go away in future refactoring.

elif cov_type == "naive":
ncov = cov
elif cov_type == "bias_reduced":
bc_cov = cov

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.

given that bcov and ncov are also attached to the results instance, I think they also need to be transformed to the unconstrained parameter space, don't they?

bcov = self.constraint.unpack_cov(bcov)

I'll add a general question in main comment

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.

@kshedden Do you still want to look at this or do we merge as is?
(I'm trying to work my way through some of the PR backlog.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will look at this again and ping you before we merge.

On Mon, May 30, 2016 at 12:20 AM, Josef Perktold [email protected]
wrote:

In statsmodels/genmod/generalized_estimating_equations.py
#2833 (comment)
:

         if mean_params is None:
             warnings.warn("Unable to estimate constrained GEE "
                           "parameters.", ConvergenceWarning)
             return None
  •        if cov_type == "robust":
    
  •            bcov = cov
    
  •        elif cov_type == "naive":
    
  •            ncov = cov
    
  •        elif cov_type == "bias_reduced":
    
  •            bc_cov = cov
    

@kshedden https://github.com/kshedden Do you still want to look at this
or do we merge as is?
(I'm trying to work my way through some of the PR backlog.)

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/statsmodels/statsmodels/pull/2833/files/45086b1d4ddcc6b10d336f4efaa8db4b95cb7ec0#r65022205,
or mute the thread
https://github.com/notifications/unsubscribe/ACiww0--eJ7NduOE6UOKw0NVnmcSe-p-ks5qGmWFgaJpZM4HeiHE
.

@josef-pkt

Copy link
Copy Markdown
Member

question to the score test:
AFAICS, it only uses cmat and not the actual covariance that is provided as an argument.
Do you know how to do the robust score_test?
I worked on score tests with robust covariance matrices in a PR, but I'm not sure yet how the pieces fit together. And I didn't find a reference to test against in R or in Stata.
For GLM I only have a nonrobust score test attached to the model.

(Now that I understand a lot more than when GEE was initially merged, I should work my way through this code again.)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) to 85.48% when pulling 9cd5a68 on kshedden:gee_margins into fd90132 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.08%) to 85.49% when pulling d7add1e on kshedden:gee_margins into fd90132 on statsmodels:master.

@kshedden

Copy link
Copy Markdown
Contributor Author

@josef-pkt I think this is ready to merge now. I fixed the bug you caught about reparameterizing cov_params

assert_allclose(result.cov_robust.shape, np.r_[4, 4])
assert_allclose(result.cov_naive.shape, np.r_[4, 4])
if cov_type == "bias_reduced":
assert_allclose(result.cov_robust_bc.shape, np.r_[4, 4])

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.

integers can be tested with assert_equal (there is no floating point noise)

@josef-pkt

Copy link
Copy Markdown
Member

I only spot checked the last commit. Looks good to me, merging

@josef-pkt
josef-pkt merged commit 43110bd into statsmodels:master May 30, 2016
@josef-pkt josef-pkt added this to the 0.8 milestone May 30, 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.

3 participants