pbreheny / visreg

Visualization of regression functions
http://pbreheny.github.io/visreg/
62 stars 18 forks source link

Introduce a vcov= argument to allow using sandwich covariance and calculate robust standard errors #89

Open kuiperjh opened 3 years ago

kuiperjh commented 3 years ago

Some models benefit from using heteroskedasticity and autocorrelation consistent covariance estimators (sandwich covariance estimators), giving model-robust standard error estimates. Could this be added to visreg? This could for instance be done by suggesting the package glm.predict and adding a vcov= or sigma= argument.

pbreheny commented 3 years ago

The design of visreg is to handle prediction in a completely object-oriented fashion. If a model fit is of class 'someClass', then predictions are handled via the predict.someClass() method. visreg itself contains no prediction code -- this is completely delegated to the model's predict method.

So, I can't see introducing a vcov argument, as this is extremely model-specific. However, you can always redirect the method to point to some other function:

predict.glm <- function(object) {
  # use whatever package you want here, with whatever variance-covariance matrix you want
}
visreg(fit, 'x')

This is perhaps sub-optimal in the sense that you'd be changing predict.glm() in the global environment. You can always redefine back to the original: predict.glm <- stats::predict.glm(), although perhaps visreg could accept a predict= argument where you can pass some user-defined function into visreg. I could see that being useful. Would that be useful in your use case?

kuiperjh commented 3 years ago

Hello Patrick,

Thank you for the speedy suggestion. Yes, that would be entirely fine, it was/is in fact my plan B!

Best wishes, Jan Herman


Sent from my phone

Jan Herman Kuiper, PhD Senior Lecturer in Biomechanics, Keele University, UK

Postal address: RJAH Orthopaedic Hospital, Oswestry SY10 7AG, United Kingdom, Phone +44-1691-404581, Fax +44-1691-404071


From: Patrick Breheny notifications@github.com Sent: Tuesday, March 2, 2021 6:27:59 PM To: pbreheny/visreg visreg@noreply.github.com Cc: Jan Herman Kuiper j.h.kuiper@keele.ac.uk; Author author@noreply.github.com Subject: Re: [pbreheny/visreg] Introduce a vcov= argument to allow using sandwich covariance and calculate robust standard errors (#89)

The design of visreg is to handle prediction in a completely object-oriented fashion. If a model fit is of class 'someClass', then predictions are handled via the predict.someClass() method. visreg itself contains no prediction code -- this is completely delegated to the model's predict method.

So, I can't see introducing a vcov argument, as this is extremely model-specific. However, you can always redirect the method to point to some other function:

predict.glm <- function(object) {

use whatever package you want here, with whatever variance-covariance matrix you want

} visreg(fit, 'x')

This is perhaps sub-optimal in the sense that you'd be changing predict.glm() in the global environment. You can always redefine back to the original: predict.glm <- stats::predict.glm(), although perhaps visreg could accept a predict= argument where you can pass some user-defined function into visreg. I could see that being useful. Would that be useful in your use case?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/pbreheny/visreg/issues/89#issuecomment-789117353, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AFO27GN6ECPAWTGCQ6MHTYLTBUU27ANCNFSM4YPOW4MA.

pbreheny commented 3 years ago

I'll leave this open in the sense that it would be nicer to pass a custom predict() function than force the user to redefine their environment.

kuiperjh commented 3 years ago

You're right, but for now I'm happy to simply redefine the function...


Sent from my phone

Jan Herman Kuiper, PhD Senior Lecturer in Biomechanics, Keele University, UK

Postal address: RJAH Orthopaedic Hospital, Oswestry SY10 7AG, United Kingdom, Phone +44-1691-404581, Fax +44-1691-404071


From: Patrick Breheny notifications@github.com Sent: Tuesday, March 2, 2021 9:31:13 PM To: pbreheny/visreg visreg@noreply.github.com Cc: Jan Herman Kuiper j.h.kuiper@keele.ac.uk; Author author@noreply.github.com Subject: Re: [pbreheny/visreg] Introduce a vcov= argument to allow using sandwich covariance and calculate robust standard errors (#89)

I'll leave this open in the sense that it would be nicer to pass a custom predict() function than force the user to redefine their environment.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/pbreheny/visreg/issues/89#issuecomment-789230184, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AFO27GKVRP2HAUXWG43R34TTBVKKDANCNFSM4YPOW4MA.