withSE memisc 0.99.20.1

Add Alternative Variance Estimates to Models Estimates

Description

A simple object-orientation infrastructure to add alternative standard errors, e.g. sandwich estimates or New-West standard errors to fitted regression-type models, such as fitted by lm() or glm().

Usage

withSE(object, vcov, ...)

withVCov(object, vcov, ...)

## S4 method for signature 'lm'
withVCov(object, vcov, ...)

## S4 method for signature 'withVCov'
summary(object, ...)
## S4 method for signature 'withVCov.lm'
summary(object, ...)

Arguments

object

a fitted model object

vcov

a function that returns a variance matrix estimate, a given matrix that is such an estimate, or a character string that identifies a function that returns a variance matrix estimate (e.g. "HAC" for vcovHAC).

...

further arguments, passed to vcov() or, respectively, to the parent method of summary()

Value

withVCov returns a slightly modified model object: It adds an attribute named “.VCov” that contains the alternate covaraince matrix and modifies the class attribute. If e.g. the original model object has class “lm” then the model object modified by withVCov has the class attribute c("withVCov.lm", "withVCov", "lm").

Details

Using withVCov() an alternative variance-covariance matrix is attributed to a fitted model object. Such a matrix may be produced by any of the variance estimators provided by the “sandwich” package or any package that extends it.

withVCov() has no consequences on how a fitted model itself is printed or represented, but it does have consequences what standard errors are reported, when the function summary() or the function mtable() is applied.

withSE() is a convenience front-end to withVCov(). It can be called in the same way as withVCov, but also allows to specify the type of variance estimate by a character string that identifies the function that gives the covariance matrix (e.g. "OPG" for vcovOPG).

Examples

## Generate poisson regression relationship
x <- sin(1:100)
y <- rpois(100, exp(1 + x))
## compute usual covariance matrix of coefficient estimates
fm <- glm(y ~ x, family = poisson)
library(sandwich)
fmo <- withVCov(fm,vcovOPG)
vcov(fm)
             (Intercept)            x
(Intercept)  0.004556711 -0.003730265
x           -0.003730265  0.008148275
vcov(fmo)
             (Intercept)            x
(Intercept)  0.003855303 -0.002969466
x           -0.002969466  0.007396969
summary(fm)
Call:
glm(formula = y ~ x, family = poisson)

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-2.7483  -0.9387  -0.2186   0.6257   3.1485

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  1.00670    0.06750   14.91   <2e-16 ***
x            1.02839    0.09027   11.39   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 278.66  on 99  degrees of freedom
Residual deviance: 122.90  on 98  degrees of freedom
AIC: 386.48

Number of Fisher Scoring iterations: 5
summary(fmo)
Call:
glm(formula = y ~ x, family = poisson)

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-2.7483  -0.9387  -0.2186   0.6257   3.1485

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  1.00670    0.06209   16.21   <2e-16 ***
x            1.02839    0.08601   11.96   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 278.66  on 99  degrees of freedom
Residual deviance: 122.90  on 98  degrees of freedom
AIC: 386.48

Number of Fisher Scoring iterations: 5
mtable(Default=fm,
      OPG=withSE(fm,"OPG"),
      summary.stats=c("Deviance","N")
      )
Calls:
Default: glm(formula = y ~ x, family = poisson)
OPG: glm(formula = y ~ x, family = poisson)

=======================================
                Default       OPG
---------------------------------------
  (Intercept)    1.007***    1.007***
                (0.068)     (0.062)
  x              1.028***    1.028***
                (0.090)     (0.086)
---------------------------------------
  Deviance     122.900     122.900
  N            100         100
=======================================
  Significance: *** = p < 0.001;
                ** = p < 0.01;
                * = p < 0.05
vo <- vcovOPG(fm)
mtable(Default=fm,
      OPG=withSE(fm,vo),
      summary.stats=c("Deviance","N")
      )
Calls:
Default: glm(formula = y ~ x, family = poisson)
OPG: glm(formula = y ~ x, family = poisson)

=======================================
                Default       OPG
---------------------------------------
  (Intercept)    1.007***    1.007***
                (0.068)     (0.062)
  x              1.028***    1.028***
                (0.090)     (0.086)
---------------------------------------
  Deviance     122.900     122.900
  N            100         100
=======================================
  Significance: *** = p < 0.001;
                ** = p < 0.01;
                * = p < 0.05