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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#############################################################################
# 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( miceadds::pool_mi( qhat=betas, u=vars ) )

#------ compute global F-test for hypothesis that all predictors have zero coefficient values 
library(mitml)
Nimp <- 6 # number of imputations
np <- length(betas[[1]])   # number of parameters
beta_names <- names(betas[[1]])
# define vector of parameters for which constraints should be tested
constraints <- beta_names[-1]
# create input for mitml::testConstraints function
qhat <- matrix( unlist(betas), ncol=Nimp)
rownames(qhat) <- beta_names
uhat <- array( unlist(vars), dim=c(np,np,Nimp))
dimnames(uhat) <- list( beta_names, beta_names, NULL )
# compute global F-test
Ftest <- mitml::testConstraints( qhat=betas , uhat = vars, constraints= constraints )
print(Ftest)

## End(Not run)

Example output

Loading required package: mice
* miceadds 2.7-19 (2017-08-24 17:34: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: miceadds::pool_mi(qhat = betas, u = vars)
                results          se         t             p      (lower
(Intercept)  2.58674104 0.076855048 33.657399 7.817127e-227  2.43607617
migrant      0.57110305 0.095141150  6.002692  1.706058e-07  0.38034237
misei       -0.01386381 0.001430282 -9.693063  5.212130e-22 -0.01666784
                 upper) missInfo
(Intercept)  2.73740590      3 %
migrant      0.76186373   32.9 %
misei       -0.01105978    3.3 %
*** This is beta software. Please report any bugs!
*** See the NEWS file for recent changes.

Call:

mitml::testConstraints(qhat = betas, uhat = vars, constraints = constraints)

Hypothesis test calculated from 6 imputed data sets. The following
constraints were specified:

 migrant
 misei

Combination method: D1 

   F.value     df1     df2   P(>F)     RIV 
    82.714       2 100.306   0.000   0.266 

Unadjusted hypothesis test as appropriate in larger samples. 

miceadds documentation built on April 1, 2018, 12:26 p.m.