| 1 |
#------------------------------------- |
| 2 |
# vcovCR with defaults |
| 3 |
#------------------------------------- |
| 4 |
|
| 5 |
#' Cluster-robust variance-covariance matrix for an mlm object. |
| 6 |
#' |
| 7 |
#' \code{vcovCR} returns a sandwich estimate of the variance-covariance matrix |
| 8 |
#' of a set of regression coefficient estimates from an \code{mlm} object. |
| 9 |
#' |
| 10 |
#' @param cluster Optional expression or vector indicating which observations |
| 11 |
#' belong to the same cluster. If not specified, each row of the data will be |
| 12 |
#' treated as a separate cluster. |
| 13 |
#' @param target Optional matrix or vector describing the working |
| 14 |
#' variance-covariance model used to calculate the \code{CR2} and \code{CR4} |
| 15 |
#' adjustment matrices. If not specified, the target is taken to be an |
| 16 |
#' identity matrix. |
| 17 |
#' @inheritParams vcovCR |
| 18 |
#' |
| 19 |
#' @return An object of class \code{c("vcovCR","clubSandwich")}, which consists |
| 20 |
#' of a matrix of the estimated variance of and covariances between the |
| 21 |
#' regression coefficient estimates. |
| 22 |
#' |
| 23 |
#' @seealso \code{\link{vcovCR}} |
| 24 |
#' |
| 25 |
#' @examples |
| 26 |
#' iris_fit <- lm(cbind(Sepal.Length, Sepal.Width) ~ Species + |
| 27 |
#' Petal.Length + Petal.Width, data = iris) |
| 28 |
#' Vcluster <- vcovCR(iris_fit, type = "CR2") |
| 29 |
#' Vcluster |
| 30 |
#' |
| 31 |
#' @export |
| 32 |
|
| 33 |
vcovCR.mlm <- function(obj, cluster, type, target, inverse_var, form = "sandwich", ...) { |
| 34 |
resids <- residuals(obj) |
| 35 |
d <- ncol(resids) |
| 36 |
N <- nrow(resids) |
| 37 |
|
| 38 |
# Cluster by observation if clustering variable is not specified |
| 39 |
if (missing(cluster)) cluster <- 1:N |
| 40 |
|
| 41 |
# Handle omitted observations in the clustering variable |
| 42 |
if (inherits(na.action(obj), "omit") && length(cluster) != N) { |
| 43 |
cluster <- cluster[-na.action(obj)] |
| 44 |
} |
| 45 |
|
| 46 |
# Handle weights of zero in the clustering variable |
| 47 |
if (!is.null(wts <- weights(obj))) { |
| 48 |
pos_wts <- wts > 0 |
| 49 |
if (!all(pos_wts)) cluster <- cluster[pos_wts] |
| 50 |
N <- sum(pos_wts) |
| 51 |
} |
| 52 |
|
| 53 |
if (length(cluster) == N) cluster <- rep(cluster, each = d) |
| 54 |
if (length(cluster) != d * N) stop("Clustering variable is not correct length.") |
| 55 |
if (missing(target)) target <- NULL |
| 56 |
if (missing(inverse_var)) inverse_var <- is.null(weights(obj)) & is.null(target) |
| 57 |
vcov_CR(obj, cluster = cluster, type = type, |
| 58 |
target = target, inverse_var = inverse_var, form = form) |
| 59 |
} |
| 60 |
|
| 61 |
# nobs() |
| 62 |
|
| 63 |
#------------------------------------- |
| 64 |
# residuals |
| 65 |
#------------------------------------- |
| 66 |
|
| 67 |
#' @export |
| 68 |
|
| 69 |
residuals_CS.mlm <- function(obj) { |
| 70 |
w <- obj$weights |
| 71 |
if (is.null(w) || all(pos_wts <- w > 0)) { |
| 72 |
res <- residuals(obj) |
| 73 |
} else { |
| 74 |
res <- residuals(obj)[pos_wts,,drop=FALSE] |
| 75 |
} |
| 76 |
as.vector(t(res)) |
| 77 |
} |
| 78 |
|
| 79 |
#------------------------------------- |
| 80 |
# model_matrix() |
| 81 |
#------------------------------------- |
| 82 |
|
| 83 |
#' @export |
| 84 |
|
| 85 |
model_matrix.mlm <- function(obj) { |
| 86 |
X <- model.matrix(obj) |
| 87 |
|
| 88 |
w <- obj$weights |
| 89 |
if (!is.null(w) && !all(pos_wts <- w > 0)) { |
| 90 |
X <- X[pos_wts > 0,,drop=FALSE] |
| 91 |
} |
| 92 |
|
| 93 |
d <- ncol(residuals(obj)) |
| 94 |
X_mat <- X %x% diag(1L, nrow = d) |
| 95 |
rownames(X_mat) <- rep(dimnames(X)[[1]], each = d) |
| 96 |
colnames(X_mat) <- paste(rep(colnames(residuals(obj)), ncol(X)), |
| 97 |
rep(colnames(X), each = d), sep = ":") |
| 98 |
i <- unlist(lapply(1:d, function(x) seq(x, ncol(X_mat), d))) |
| 99 |
X_mat[,i] |
| 100 |
} |
| 101 |
|
| 102 |
#---------------------------------------------- |
| 103 |
# get "working" variance-covariance matrix |
| 104 |
#---------------------------------------------- |
| 105 |
|
| 106 |
#' @export |
| 107 |
|
| 108 |
targetVariance.mlm <- function(obj, cluster) { |
| 109 |
matrix_list(rep(1, nobs(obj) * ncol(residuals(obj))), cluster, "both") |
| 110 |
} |
| 111 |
|
| 112 |
#------------------------------------- |
| 113 |
# Get weighting matrix |
| 114 |
#------------------------------------- |
| 115 |
|
| 116 |
#' @export |
| 117 |
|
| 118 |
weightMatrix.mlm <- function(obj, cluster) { |
| 119 |
weights <- weights(obj) |
| 120 |
if (is.null(weights)) { |
| 121 |
weights <- w_scale <- 1 |
| 122 |
} else { |
| 123 |
weights <- weights[weights > 0] |
| 124 |
w_scale <- mean(weights) |
| 125 |
weights <- weights / w_scale |
| 126 |
} |
| 127 |
W <- rep(weights, length.out = nobs(obj)) |
| 128 |
W <- rep(W, each = ncol(residuals(obj))) |
| 129 |
W_list <- matrix_list(W, cluster, "both") |
| 130 |
attr(W_list, "w_scale") <- w_scale |
| 131 |
W_list |
| 132 |
} |
| 133 |
|
| 134 |
#---------------------------------------------- |
| 135 |
# get coefficient estimates |
| 136 |
#---------------------------------------------- |
| 137 |
|
| 138 |
#' @export |
| 139 |
|
| 140 |
coef_CS.mlm <- function(obj) { |
| 141 |
cf <- coef(obj) |
| 142 |
res <- as.vector(cf) |
| 143 |
names(res) <- paste(rep(colnames(cf), each = nrow(cf)), |
| 144 |
rep(rownames(cf), ncol(cf)), sep = ":") |
| 145 |
res |
| 146 |
} |
| 147 |
|
| 148 |
#--------------------------------------- |
| 149 |
# Get bread matrix and scaling constant |
| 150 |
#--------------------------------------- |
| 151 |
|
| 152 |
#' @export |
| 153 |
#' |
| 154 |
|
| 155 |
bread.mlm <- function(x, ...) { |
| 156 |
if(!is.null(x$na.action)) class(x$na.action) <- "omit" |
| 157 |
cf <- coef(x) |
| 158 |
rval <- summary.lm(x) |
| 159 |
rval <- kronecker( |
| 160 |
structure(diag(ncol(cf)), .Dimnames = rep.int(list(colnames(cf)), 2L)), |
| 161 |
structure(rval$cov.unscaled, .Dimnames = rep.int(list(rownames(cf)), 2L)) * as.vector(sum(rval$df[1:2])), |
| 162 |
make.dimnames = TRUE |
| 163 |
) |
| 164 |
return(rval) |
| 165 |
} |
| 166 |
|
| 167 |
#' @export |
| 168 |
|
| 169 |
v_scale.mlm <- function(obj) { |
| 170 |
nobs(obj) |
| 171 |
} |