Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

withVCov

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, ...)

## S3 method for class 'lm'
withVCov(object, vcov, ...)

## S3 method for class 'withVCov'
summary(object, ...)
## S3 method for class '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()

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).

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").

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)
vcov(fmo)

summary(fm)
summary(fmo)

mtable(Default=fm,
       OPG=withSE(fm,"OPG"),
       summary.stats=c("Deviance","N")
       )

vo <- vcovOPG(fm)

mtable(Default=fm,
       OPG=withSE(fm,vo),
       summary.stats=c("Deviance","N")
       )

memisc

Management of Survey Data and Presentation of Analysis Results

v0.99.27.3
GPL-2
Authors
Martin Elff (with contributions from Christopher N. Lawrence, Dave Atkins, Jason W. Morgan, Achim Zeileis)
Initial release
2020-11-18

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.