| 1 |
#------------------------------------- |
| 2 |
# vcovCR with defaults |
| 3 |
#------------------------------------- |
| 4 |
|
| 5 |
#' Cluster-robust variance-covariance matrix for a gls object. |
| 6 |
#' |
| 7 |
#' \code{vcovCR} returns a sandwich estimate of the variance-covariance matrix |
| 8 |
#' of a set of regression coefficient estimates from a \code{\link[nlme]{gls}} object. |
| 9 |
#' |
| 10 |
#' @param cluster Optional expression or vector indicating which observations |
| 11 |
#' belong to the same cluster. If not specified, will be set to |
| 12 |
#' \code{getGroups(obj)}. |
| 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 the |
| 16 |
#' estimated variance-covariance structure of the \code{gls} object. |
| 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 |
#' |
| 27 |
#' if (requireNamespace("nlme", quietly = TRUE)) { |
| 28 |
#' |
| 29 |
#' library(nlme) |
| 30 |
#' data(Ovary, package = "nlme") |
| 31 |
#' Ovary$time_int <- 1:nrow(Ovary) |
| 32 |
#' lm_AR1 <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), data = Ovary, |
| 33 |
#' correlation = corAR1(form = ~ time_int | Mare)) |
| 34 |
#' vcovCR(lm_AR1, type = "CR2") |
| 35 |
#' |
| 36 |
#' } |
| 37 |
#' |
| 38 |
#' @export |
| 39 |
|
| 40 |
vcovCR.gls <- function(obj, cluster, type, target, inverse_var, form = "sandwich", ...) { |
| 41 |
if (missing(cluster)) cluster <- nlme::getGroups(obj) |
| 42 |
if (missing(target)) target <- NULL |
| 43 |
if (missing(inverse_var) ) inverse_var <- is.null(target) |
| 44 |
vcov_CR(obj, cluster = cluster, type = type, |
| 45 |
target = target, inverse_var = inverse_var, form = form) |
| 46 |
} |
| 47 |
|
| 48 |
# residuals_CS() |
| 49 |
# coef() |
| 50 |
# nobs() |
| 51 |
|
| 52 |
#------------------------------------- |
| 53 |
# model_matrix() |
| 54 |
#------------------------------------- |
| 55 |
|
| 56 |
get_data <- function (object) { |
| 57 |
|
| 58 |
if ("data" %in% names(object)) { |
| 59 |
data <- object$data |
| 60 |
} else { |
| 61 |
dat_call <- object$call$data |
| 62 |
envir_names <- sys.frames() |
| 63 |
data <- simpleError("start") |
| 64 |
i <- 1L |
| 65 |
while (inherits(data, "error") & i <= length(envir_names)) { |
| 66 |
data <- tryCatch(eval(dat_call, envir = envir_names[[i]]), error = function(e) e) |
| 67 |
i <- i + 1L |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
if (inherits(data, "error")) return(NULL) |
| 72 |
|
| 73 |
naAct <- object[["na.action"]] |
| 74 |
if (!is.null(naAct)) { |
| 75 |
data <- if (inherits(naAct, "omit")) { |
| 76 |
data[-naAct, ] |
| 77 |
|
| 78 |
} else if (inherits(naAct, "exclude")) { |
| 79 |
data |
| 80 |
} else eval(object$call$na.action)(data) |
| 81 |
} |
| 82 |
|
| 83 |
subset <- object$call$subset |
| 84 |
if (!is.null(subset)) { |
| 85 |
subset <- eval(asOneSidedFormula(subset)[[2]], data) |
| 86 |
data <- data[subset, ] |
| 87 |
} |
| 88 |
|
| 89 |
data |
| 90 |
} |
| 91 |
|
| 92 |
#' @export |
| 93 |
|
| 94 |
model_matrix.gls <- function(obj) { |
| 95 |
dat <- get_data(obj) |
| 96 |
model.matrix(formula(obj), data = dat) |
| 97 |
} |
| 98 |
|
| 99 |
#------------------------------------- |
| 100 |
# Get (model-based) working variance matrix |
| 101 |
#------------------------------------- |
| 102 |
|
| 103 |
#' @export |
| 104 |
|
| 105 |
targetVariance.gls <- function(obj, cluster = nlme::getGroups(obj)) { |
| 106 |
|
| 107 |
groups <- nlme::getGroups(obj) |
| 108 |
if (is.null(groups)) groups <- cluster |
| 109 |
|
| 110 |
if (is.null(obj$modelStruct$corStruct)) { |
| 111 |
if (is.null(obj$modelStruct$varStruct)) { |
| 112 |
V_list <- matrix_list(rep(1, length(cluster)), cluster, "both") |
| 113 |
} else { |
| 114 |
wts <- nlme::varWeights(obj$modelStruct$varStruct) |
| 115 |
V_list <- matrix_list(1 / wts^2, cluster, "both") |
| 116 |
} |
| 117 |
} else { |
| 118 |
R_list <- nlme::corMatrix(obj$modelStruct$corStruct) |
| 119 |
if (is.null(obj$modelStruct$varStruct)) { |
| 120 |
V_list <- R_list |
| 121 |
} else { |
| 122 |
sd_vec <- 1 / nlme::varWeights(obj$modelStruct$varStruct)[order(order(groups))] |
| 123 |
sd_list <- split(sd_vec, groups) |
| 124 |
V_list <- Map(function(R, s) tcrossprod(s) * R, R = R_list, s = sd_list) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
# check if clustering level is higher than highest level of random effects |
| 129 |
|
| 130 |
tb_groups <- table(groups) |
| 131 |
tb_cluster <- table(cluster) |
| 132 |
if (length(tb_groups) < length(tb_cluster) | |
| 133 |
any(as.vector(tb_groups) != rep(as.vector(tb_cluster), length.out = length(tb_groups))) | |
| 134 |
any(names(tb_groups) != rep(names(tb_cluster), length.out = length(tb_groups)))) { |
| 135 |
|
| 136 |
# check that random effects are nested within clusters |
| 137 |
tb_cross <- table(groups, cluster) |
| 138 |
nested <- apply(tb_cross, 1, function(x) sum(x > 0) == 1) |
| 139 |
if (!all(nested)) stop("Random effects are not nested within clustering variable.") |
| 140 |
|
| 141 |
# expand target_list to level of clustering |
| 142 |
crosswalk <- data.frame(groups, cluster) |
| 143 |
V_list <- add_bdiag(small_mats = V_list, |
| 144 |
big_mats = matrix_list(rep(0, length(cluster)), cluster, dim = "both"), |
| 145 |
crosswalk = crosswalk) |
| 146 |
} |
| 147 |
|
| 148 |
V_list |
| 149 |
} |
| 150 |
|
| 151 |
#------------------------------------- |
| 152 |
# Get weighting matrix |
| 153 |
#------------------------------------- |
| 154 |
|
| 155 |
#' @export |
| 156 |
|
| 157 |
weightMatrix.gls <- function(obj, cluster = nlme::getGroups(obj)) { |
| 158 |
V_list <- targetVariance(obj, cluster) |
| 159 |
lapply(V_list, function(v) chol2inv(chol(v))) |
| 160 |
} |
| 161 |
|
| 162 |
#--------------------------------------- |
| 163 |
# Get bread matrix and scaling constant |
| 164 |
#--------------------------------------- |
| 165 |
|
| 166 |
#' @export |
| 167 |
|
| 168 |
bread.gls <- function(x, ...) { |
| 169 |
vcov(x) * nobs(x) / x$sigma^2 |
| 170 |
} |
| 171 |
|
| 172 |
# v_scale() is default |