| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/lm_robust.R |
| 3 |
\name{vcovCR.lm_robust} |
| 4 |
\alias{vcovCR.lm_robust} |
| 5 |
\title{Cluster-robust variance-covariance matrix for an \code{estimatr::lm_robust} |
| 6 |
object.} |
| 7 |
\usage{ |
| 8 |
\method{vcovCR}{lm_robust}( |
| 9 |
obj, |
| 10 |
cluster, |
| 11 |
type, |
| 12 |
target = NULL, |
| 13 |
inverse_var = NULL, |
| 14 |
form = "sandwich", |
| 15 |
... |
| 16 |
) |
| 17 |
} |
| 18 |
\arguments{ |
| 19 |
\item{obj}{Fitted model for which to calculate the variance-covariance matrix} |
| 20 |
|
| 21 |
\item{cluster}{Expression or vector indicating which observations belong to |
| 22 |
the same cluster. If not specified, will be detected from the |
| 23 |
\code{clusters} argument of \code{obj}.} |
| 24 |
|
| 25 |
\item{type}{Character string specifying which small-sample adjustment should |
| 26 |
be used, with available options \code{"CR0"}, \code{"CR1"}, \code{"CR1p"}, |
| 27 |
\code{"CR1S"}, \code{"CR2"}, or \code{"CR3"}. If not specified, will be |
| 28 |
detected from the \code{se_type} argument of \code{obj}. See "Details" |
| 29 |
section of \code{\link{vcovCR}} for further information.} |
| 30 |
|
| 31 |
\item{target}{Optional matrix or vector describing the working |
| 32 |
variance-covariance model used to calculate the \code{CR2} and \code{CR4} |
| 33 |
adjustment matrices. If a vector, the target matrix is assumed to be |
| 34 |
diagonal. If not specified, the target is taken to be an identity matrix.} |
| 35 |
|
| 36 |
\item{inverse_var}{Optional logical indicating whether the weights used in |
| 37 |
fitting the model are inverse-variance. If not specified, \code{vcovCR} |
| 38 |
will attempt to infer a value.} |
| 39 |
|
| 40 |
\item{form}{Controls the form of the returned matrix. The default |
| 41 |
\code{"sandwich"} will return the sandwich variance-covariance matrix. |
| 42 |
Alternately, setting \code{form = "meat"} will return only the meat of the |
| 43 |
sandwich and setting \code{form = B}, where \code{B} is a matrix of |
| 44 |
appropriate dimension, will return the sandwich variance-covariance matrix |
| 45 |
calculated using \code{B} as the bread. \code{form = "estfun"} will return the |
| 46 |
(appropriately scaled) estimating function, the transposed crossproduct of |
| 47 |
which is equal to the sandwich variance-covariance matrix.} |
| 48 |
|
| 49 |
\item{...}{Additional arguments available for some classes of objects.} |
| 50 |
} |
| 51 |
\value{ |
| 52 |
An object of class \code{c("vcovCR","clubSandwich")}, which consists |
| 53 |
of a matrix of the estimated variance of and covariances between the |
| 54 |
regression coefficient estimates. |
| 55 |
} |
| 56 |
\description{ |
| 57 |
\code{vcovCR} returns a sandwich estimate of the variance-covariance matrix |
| 58 |
of a set of regression coefficient estimates from an |
| 59 |
\code{\link[estimatr]{lm_robust}} object. |
| 60 |
} |
| 61 |
\examples{ |
| 62 |
|
| 63 |
data("ChickWeight", package = "datasets") |
| 64 |
ChickWeight$Chick <- factor(ChickWeight$Chick, ordered = FALSE) |
| 65 |
|
| 66 |
if (requireNamespace("estimatr", quietly = TRUE)) withAutoprint({ |
| 67 |
library(estimatr) |
| 68 |
|
| 69 |
lm_fit <- lm_robust( |
| 70 |
weight ~ Time + Diet:Time, |
| 71 |
data = ChickWeight |
| 72 |
) |
| 73 |
vcovCR(lm_fit, cluster = ChickWeight$Chick, type = "CR2") |
| 74 |
|
| 75 |
lm_fit_clust <- lm_robust( |
| 76 |
weight ~ Time + Diet:Time, data = ChickWeight, |
| 77 |
clusters = Chick |
| 78 |
) |
| 79 |
conf_int(lm_fit_clust, vcov = "CR2") |
| 80 |
|
| 81 |
# similar model via lm_lin() |
| 82 |
lin_fit_clust <- lm_lin( |
| 83 |
weight ~ Diet, |
| 84 |
covariates = ~ Time, |
| 85 |
data = ChickWeight, |
| 86 |
clusters = Chick |
| 87 |
) |
| 88 |
conf_int(lin_fit_clust, vcov = "CR2") |
| 89 |
|
| 90 |
lm_fit_fe <- lm_robust( |
| 91 |
weight ~ Time:Diet, data = ChickWeight, |
| 92 |
clusters = Chick, |
| 93 |
fixed_effects = ~ Chick |
| 94 |
) |
| 95 |
vcovCR(lm_fit_fe) |
| 96 |
|
| 97 |
# two-way fixed effects model |
| 98 |
data("MortalityRates") |
| 99 |
MortalityRates <- subset(MortalityRates, cause == "Motor Vehicle") |
| 100 |
MortalityRates$state <- factor(MortalityRates$state) |
| 101 |
MortalityRates$year <- factor(MortalityRates$year) |
| 102 |
MLDA_fit <- lm_robust( |
| 103 |
mrate ~ legal + beertaxa + beerpercap + winepercap + spiritpercap, |
| 104 |
fixed_effects = ~ year + state, |
| 105 |
data = MortalityRates, |
| 106 |
cluster = state |
| 107 |
) |
| 108 |
conf_int(MLDA_fit, vcov = "CR2") |
| 109 |
|
| 110 |
if (requireNamespace("plm", quietly = TRUE)) withAutoprint({ |
| 111 |
|
| 112 |
data("Produc", package = "plm") |
| 113 |
lm_individual <- lm_robust( |
| 114 |
log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, |
| 115 |
data = Produc, |
| 116 |
fixed_effects = ~ state, |
| 117 |
cluster = state |
| 118 |
) |
| 119 |
vcovCR(lm_individual, type = "CR2") |
| 120 |
|
| 121 |
}) |
| 122 |
|
| 123 |
}) |
| 124 |
|
| 125 |
} |
| 126 |
\seealso{ |
| 127 |
\code{\link{vcovCR}} |
| 128 |
} |