Skip to content

Closed form studentized residuals external #373

Open

Description

@jseabold

I can't remember whether we are using the loop still, but I just came across a closed form solution for leave one out studentized residuals

ols_model.resid * ((ols_model.df_resid-1)/(ols_model.ssr * (1 - infl.hat_matrix_diag) - ols_model.resid**2))**.5

Activity

  1. josef-pkt commented on Jul 15, 2012

    @josef-pkt
    Member

    resid_studentized_internal is without loop, but sigma estimate for resid_studentized_external uses LOO loop.

    Looking at your expression and the one in influence and outliers, I don't see yet how they can match up.

    If there is a LOO-sigma estimate without loop, then that would be very useful.

  2. jseabold commented on Jul 15, 2012

    @jseabold
    MemberAuthor
    dta = sm.datasets.stackloss.load()
    dta.exog = sm.add_constant(dta.exog)
    ols_model = sm.OLS(dta.endog, dta.exog).fit()
    infl = ols_model.get_influence()
    np.testing.assert_almost_equal(infl.resid_studentized_external, ols_model.resid * ((ols_model.df_resid-1)/(ols_model.ssr * (1 - infl.hat_matrix_diag) - ols_model.resid**2))**.5, 12)
    
  3. josef-pkt commented on Jul 15, 2012

    @josef-pkt
    Member

    reverse engineering (without going through the math

    sigma2_notobsi = (ols_model.ssr - ols_model.resid**2 / (1 - infl.hat_matrix_diag)) /(ols_model.df_resid-1)
    assert_almost_equal(sigma2_notobsi, infl.sigma2_not_obsi, 11)
    
  4. josef-pkt commented on Jul 15, 2012

    @josef-pkt
    Member

    which means we also get dffits without LOO loop

    looks like mainly dfparams and cov_ratio still need the LOOO loop

  5. noahjgreen295 commented on Jul 26, 2015

    @noahjgreen295

    Sorry to bother you all but I was wondering, in the outliers_influence.py file in statsmodels 0.6.1, I see the following formula for the studentized residuals in get_resid_studentized_external():

    return self.results.resid / sigma / np.sqrt(1 - hii)

    By my understanding of studentized residuals, shouldn't the second / actually be a *, like this:

    return self.results.resid / sigma * np.sqrt(1 - hii)

    Apologies if this is incorrect, I have just been getting a lot of strange results and noticed this when I took a look. Thanks!

  6. josef-pkt commented on Jul 26, 2015

    @josef-pkt
    Member

    @noahjgreen295 I'm (pretty) sure the formula is correct with dividing by np.sqrt(1 - hii)
    There should also be unit tests that verify it against SAS and maybe others, AFAIR.

    What are your strange results? Can you provide your data and example, or at least briefly describe it?

    Also, to check: this was only designed for OLS, I don't think it would be correct yet for WLS or other models. (For WLS we would need wresid and hii would need to use a different hat_matrix.

  7. added this to the 0.8 milestone on Jul 26, 2015
  8. noahjgreen295 commented on Jul 26, 2015

    @noahjgreen295

    I'll find the unit tests; given that they exist, it makes me believe the problem to be on my end, but will run them to be sure. I am running OLS so am OK there.

    Just one last question: is the / correct because the hat matrix has been inverted elsewhere in the code? I'm sorry to bother you here, it just seemed strange to me that the formula in the code is different from the standard textbook formula.

    Anyway I will seek out the unit tests and take a look at them.

  9. josef-pkt commented on Jul 26, 2015

    @josef-pkt
    Member

    https://en.wikipedia.org/wiki/Studentized_residual#How_to_studentize last equation in this section

    I only remember this formula (with division) in any textbook, paper or documentation of other stats packages. The wikipedia page also has the definition of the hat, or projection matrix for OLS.
    (I didn't check the code right now, just what you copied in your comment.)

  10. noahjgreen295 commented on Jul 26, 2015

    @noahjgreen295

    I'm confident the current code is correct. Will examine unit tests later. Thanks for responding, and for pointing me at these two blog entries! They are very helpful.

  11. modified the milestones: 0.8, Someday on Mar 23, 2021
  12. modified the milestones: Someday, 0.15 on Sep 22, 2023
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions