Posted on Fri, 13 May 2011 21:46:19 +0100
Recently I was asked whether it was possible to use with 'mtable' sandwich estimators of variance for lm and glm models, as they are provided by the R packages 'sandwich' and 'lmtest'.
An R script that provides this extension can now be downloaded from here.
One trick to make use of this extension is to add a class attribute "lm_sandwich" or "glm_sandwich", respectively, to your lm or glm object like in this example:
library(memisc)
library(sandwich)
library(lmtest)
source("../extras/getSummary-glm-sandwich.R")
data(Mandible)
fm1 <- lm(length ~ age, data=Mandible, subset=(age <= 28))
fm1.sw <- fm1
class(fm1.sw) <- c("lm_sandwich",class(fm1))
mtable(
"Model-based"=fm1,
"Sandwiched-based"=fm1.sw,
summary.stats=c("R-squared","N"))
The output of this example looks like this:
==============================================
Model-based Sandwiched-based
----------------------------------------------
(Intercept) -11.953*** -11.953***
(0.976) (1.010)
age 1.773*** 1.773***
(0.048) (0.054)
----------------------------------------------
R-squared 0.898 0.898
N 158 158
==============================================
library(memisc)
library(sandwich)
library(lmtest)
source("../extras/getSummary-glm-sandwich.R")
data(Mandible)
fm1 <- lm(length ~ age, data=Mandible, subset=(age <= 28))
fm2 <- lm(length ~ age+I(scale(age)^2), data=Mandible, subset=(age <= 28))
# Using 'conventional' i.e. model-based standard errors:
mtable(fm1,fm2,summary.stats=c("R-squared","N"))
======================================
fm1 fm2
--------------------------------------
(Intercept) -11.953*** -11.303***
(0.976) (1.060)
age 1.773*** 1.754***
(0.048) (0.049)
I(scale(age)^2) -0.362
(0.236)
--------------------------------------
R-squared 0.898 0.900
N 158 158
======================================
# Using sandwich-based standard errors:
mtable(fm1,fm2,getSummary=getSummary.lm_sandwich,
summary.stats=c("R-squared","N"))
======================================
fm1 fm2
--------------------------------------
(Intercept) -11.953*** -11.303***
(1.010) (1.348)
age 1.773*** 1.754***
(0.054) (0.063)
I(scale(age)^2) -0.362
(0.258)
--------------------------------------
R-squared 0.898 0.900
N 158 158
======================================