lm.cluster: Cluster Robust Standard Errors for Linear Models and General...

Description Usage Arguments Value Author(s) See Also Examples

View source: R/lm.cluster.R

Description

Computes cluster robust standard errors for linear models (stats::lm) and general linear models (stats::glm) using the multiwayvcov::cluster.vcov function in the multiwayvcov package.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
lm.cluster(data, formula, cluster, ...)

glm.cluster(data, formula, cluster, ...)

## S3 method for class 'lm.cluster'
summary(object,...)
## S3 method for class 'glm.cluster'
summary(object,...)

## S3 method for class 'lm.cluster'
coef(object,...)
## S3 method for class 'glm.cluster'
coef(object,...)

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

Arguments

data

Data frame

formula

An R formula

cluster

Variable name for cluster variable contained in data or a vector with cluster identifiers

...

Further arguments to be passed to stats::lm and stats::glm

object

Object of class lm.cluster or glm.cluster

Value

List with following entries

lm_res

Value of stats::lm

glm_res

Value of stats::glm

vcov

Covariance matrix of parameter estimates

Author(s)

Alexander Robitzsch

See Also

stats::lm, stats::glm, multiwayvcov::cluster.vcov

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#############################################################################
# EXAMPLE 1: Cluster robust standard errors data.ma01
#############################################################################

data(data.ma01)
dat <- data.ma01

#*** Model 1: Linear regression
mod1 <- miceadds::lm.cluster( data = dat , formula = read ~ hisei + female , 
               cluster = "idschool" )
coef(mod1)
vcov(mod1)
summary(mod1)

# estimate Model 1, but cluster is provided as a vector
mod1b <- miceadds::lm.cluster( data = dat, formula = read ~ hisei + female, 
                 cluster = dat$idschool)
summary(mod1b)

#*** Model 2: Logistic regression
dat$highmath <- 1 * ( dat$math > 600 )   # create dummy variable
mod2 <- miceadds::glm.cluster( data = dat , formula = highmath ~ hisei + female , 
                cluster = "idschool" , family="binomial")
coef(mod2)
vcov(mod2)
summary(mod2)		

## Not run: 
#############################################################################
# EXAMPLE 2: Cluster robust standard errors for multiply imputed datasets
#############################################################################

library(mitools)
data(data.ma05)
dat <- data.ma05

# imputation of the dataset: use six imputations
resp <- dat[ , - c(1:2) ]
imp <- mice::mice( resp , imputationMethod="norm" , maxit=3 , m=6 )
datlist <- miceadds::mids2datlist( imp )

# linear regression with cluster robust standard errors
mod <- lapply(  datlist, FUN = function(data){
            miceadds::lm.cluster( data=data , formula=denote ~ migrant+ misei , 
                    cluster = dat$idclass )
            }  )
# extract parameters and covariance matrix
betas <- lapply( mod , FUN = function(rr){ coef(rr) } )
vars <- lapply( mod , FUN = function(rr){ vcov(rr) } )
# conduct statistical inference
summary(pool_mi( qhat = betas, u = vars )) 

## End(Not run)

miceadds documentation built on May 19, 2017, 7:26 a.m.

Search within the miceadds package
Search all R packages, documentation and source code