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)

Example output

Loading required package: mice
* miceadds 2.5-9 (2017-06-17 14:42:44)
(Intercept)       hisei      female 
 418.803563    1.541466   35.702423 
            (Intercept)       hisei      female
(Intercept)  41.5815200 -0.52881254 -4.24050217
hisei        -0.5288125  0.01238058 -0.01164567
female       -4.2405022 -0.01164567 14.52421045
R^2 = 0.14561 

              Estimate Std. Error   t value     Pr(>|t|)
(Intercept) 418.803563  6.4483734 64.947163 0.000000e+00
hisei         1.541466  0.1112681 13.853619 1.209601e-43
female       35.702423  3.8110642  9.368098 7.385138e-21
R^2 = 0.14561 

              Estimate Std. Error   t value     Pr(>|t|)
(Intercept) 418.803563  6.4483734 64.947163 0.000000e+00
hisei         1.541466  0.1112681 13.853619 1.209601e-43
female       35.702423  3.8110642  9.368098 7.385138e-21
(Intercept)       hisei      female 
-2.76268908  0.02862145 -0.15204493 
              (Intercept)         hisei        female
(Intercept)  0.0627637096 -8.588193e-04 -8.761713e-03
hisei       -0.0008588193  1.631632e-05  8.057552e-05
female      -0.0087617130  8.057552e-05  1.028726e-02
               Estimate  Std. Error    z value     Pr(>|z|)
(Intercept) -2.76268908 0.250526864 -11.027516 2.815328e-28
hisei        0.02862145 0.004039347   7.085664 1.383794e-12
female      -0.15204493 0.101426155  -1.499070 1.338554e-01

 iter imp variable
  1   1  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  1   2  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  1   3  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  1   4  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  1   5  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  1   6  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  2   1  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  2   2  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  2   3  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  2   4  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  2   5  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  2   6  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  3   1  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  3   2  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  3   3  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  3   4  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  3   5  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
  3   6  Dscore  Mscore  denote  manote  misei  migrant  sprengel  groesse
Multiple imputation results:
Call: pool_mi(qhat = betas, u = vars)
                results          se         t            p      (lower
(Intercept)  2.55464084 0.085036645 30.041647 1.371427e-55  2.38616133
migrant      0.58432742 0.087184499  6.702194 2.080518e-10  0.41239974
misei       -0.01326883 0.001758569 -7.545244 2.778636e-09 -0.01682001
                  upper) missInfo
(Intercept)  2.723120344   22.4 %
migrant      0.756255092   16.7 %
misei       -0.009717651   37.8 %

miceadds documentation built on Aug. 25, 2017, 1:03 a.m.