| 1 |
#------------------------------------- |
| 2 |
# vcovCR with defaults |
| 3 |
#------------------------------------- |
| 4 |
|
| 5 |
#' Cluster-robust variance-covariance matrix for a rma.uni object. |
| 6 |
#' |
| 7 |
#' \code{vcovCR} returns a sandwich estimate of the variance-covariance matrix |
| 8 |
#' of a set of regression coefficient estimates from a |
| 9 |
#' \code{\link[metafor]{rma.uni}} object. |
| 10 |
#' |
| 11 |
#' @param cluster Expression or vector indicating which observations |
| 12 |
#' belong to the same cluster. Required for \code{rma.uni} objects. |
| 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 diagonal |
| 16 |
#' with entries equal to the estimated marginal variance of the effect sizes. |
| 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 |
#' @export |
| 26 |
#' |
| 27 |
#' @examples |
| 28 |
#' |
| 29 |
#' pkgs_available <- |
| 30 |
#' requireNamespace("metafor", quietly = TRUE) & |
| 31 |
#' requireNamespace("metadat", quietly = TRUE) |
| 32 |
#' |
| 33 |
#' if (pkgs_available) withAutoprint({ |
| 34 |
#' |
| 35 |
#' library(metafor) |
| 36 |
#' data(dat.assink2016, package = "metadat") |
| 37 |
#' |
| 38 |
#' mfor_fit <- rma.uni(yi ~ year + deltype, vi = vi, |
| 39 |
#' data = dat.assink2016) |
| 40 |
#' mfor_fit |
| 41 |
#' |
| 42 |
#' mfor_CR2 <- vcovCR(mfor_fit, type = "CR2", cluster = dat.assink2016$study) |
| 43 |
#' mfor_CR2 |
| 44 |
#' coef_test(mfor_fit, vcov = mfor_CR2, test = c("Satterthwaite", "saddlepoint")) |
| 45 |
#' Wald_test(mfor_fit, constraints = constrain_zero(2:4), vcov = mfor_CR2) |
| 46 |
#' |
| 47 |
#' }) |
| 48 |
#' |
| 49 |
|
| 50 |
|
| 51 |
vcovCR.rma.uni <- function(obj, cluster, type, target, inverse_var, form = "sandwich", ...) { |
| 52 |
|
| 53 |
if (missing(cluster)) stop("You must specify a clustering variable.") |
| 54 |
|
| 55 |
# if (length(cluster) != nrow(model_matrix(obj))) cluster <- droplevels(as.factor(cluster[obj$not.na])) |
| 56 |
# if (length(cluster) != nrow(model_matrix(obj))) stop("Clustering variable must have length equal to nrow(model_matrix(obj)).") |
| 57 |
|
| 58 |
if (missing(target)) { |
| 59 |
target <- NULL |
| 60 |
if (missing(inverse_var)) inverse_var <- is.null(obj$weights) & obj$weighted |
| 61 |
} else { |
| 62 |
if (missing(inverse_var)) inverse_var <- FALSE |
| 63 |
} |
| 64 |
|
| 65 |
vcov_CR(obj, cluster = cluster, type = type, |
| 66 |
target = target, inverse_var = inverse_var, form = form) |
| 67 |
} |
| 68 |
|
| 69 |
#----------------------------------------------- |
| 70 |
# residuals_CS() |
| 71 |
#----------------------------------------------- |
| 72 |
|
| 73 |
#' @export |
| 74 |
|
| 75 |
residuals_CS.rma <- function(obj) { |
| 76 |
res <- residuals(obj) |
| 77 |
not_na <- obj$not.na |
| 78 |
|
| 79 |
if (length(res) == length(not_na)) res <- res[not_na] |
| 80 |
if (!is.null(wts <- weights(obj)) && !all(pos_wts <- wts > 0)) { |
| 81 |
res <- res[pos_wts] |
| 82 |
} |
| 83 |
|
| 84 |
return(res) |
| 85 |
} |
| 86 |
|
| 87 |
#----------------------------------------------- |
| 88 |
# coefficients for location-scale model |
| 89 |
#----------------------------------------------- |
| 90 |
# coef_CS.default() used for rma.uni |
| 91 |
|
| 92 |
|
| 93 |
#' @export |
| 94 |
#' |
| 95 |
|
| 96 |
coef_CS.rma.ls <- function(obj) { |
| 97 |
metafor::coef.rma(obj)$beta |
| 98 |
} |
| 99 |
|
| 100 |
#----------------------------------------------- |
| 101 |
# coefficients for location-scale model |
| 102 |
#----------------------------------------------- |
| 103 |
# coef_CS.default() used for rma.uni |
| 104 |
|
| 105 |
|
| 106 |
#' @export |
| 107 |
#' |
| 108 |
coef_CS.rma.ls <- function(obj) { |
| 109 |
metafor::coef.rma(obj)$beta |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
#----------------------------------------------- |
| 114 |
# Model matrix for location-scale model |
| 115 |
#----------------------------------------------- |
| 116 |
# model_matrix.default() used for rma.uni |
| 117 |
|
| 118 |
|
| 119 |
#' @export |
| 120 |
#' |
| 121 |
model_matrix.rma.ls <- function(obj) { |
| 122 |
metafor::model.matrix.rma(obj)$location |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
# na.action |
| 127 |
|
| 128 |
#' @export |
| 129 |
|
| 130 |
na.action.rma <- function(object, ...) { |
| 131 |
|
| 132 |
if (all(object$not.na)) return(NULL) |
| 133 |
|
| 134 |
res <- which(!object$not.na) |
| 135 |
class(res) <- "omit" |
| 136 |
res |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
#------------------------------------- |
| 141 |
# Get (model-based) working variance matrix |
| 142 |
#------------------------------------- |
| 143 |
|
| 144 |
#' @export |
| 145 |
|
| 146 |
targetVariance.rma.uni <- function(obj, cluster) { |
| 147 |
|
| 148 |
vi <- obj$vi |
| 149 |
|
| 150 |
if (obj$weighted && !is.null(wts <- obj$weights) && !all(pos_wts <- wts > 0)) { |
| 151 |
vi <- vi[pos_wts] |
| 152 |
} |
| 153 |
|
| 154 |
matrix_list(vi + obj$tau2, cluster, "both") |
| 155 |
} |
| 156 |
|
| 157 |
#------------------------------------- |
| 158 |
# Get weighting matrix |
| 159 |
#------------------------------------- |
| 160 |
|
| 161 |
#' @export |
| 162 |
|
| 163 |
weightMatrix.rma.uni <- function(obj, cluster) { |
| 164 |
if (obj$weighted) { |
| 165 |
if (is.null(obj$weights)) { |
| 166 |
wi <- 1 / (obj$vi + obj$tau2) |
| 167 |
} else { |
| 168 |
wi <- obj$weights |
| 169 |
wi <- wi[wi > 0] |
| 170 |
} |
| 171 |
} else { |
| 172 |
wi <- rep(1, obj$k) |
| 173 |
} |
| 174 |
w_scale <- mean(wi) |
| 175 |
wi <- wi / w_scale |
| 176 |
|
| 177 |
W_list <- matrix_list(wi, cluster, "both") |
| 178 |
attr(W_list, "w_scale") <- w_scale |
| 179 |
W_list |
| 180 |
} |
| 181 |
|
| 182 |
#--------------------------------------- |
| 183 |
# Get bread matrix and scaling constant |
| 184 |
#--------------------------------------- |
| 185 |
|
| 186 |
#' @export |
| 187 |
|
| 188 |
bread.rma.uni <- function(x, ...) { |
| 189 |
X_mat <- model_matrix(x) |
| 190 |
if (x$weighted) { |
| 191 |
if (is.null(x$weights)) { |
| 192 |
wi <- 1 / (x$vi + x$tau2) |
| 193 |
} else { |
| 194 |
wi <- x$weights |
| 195 |
wi <- wi[wi > 0] |
| 196 |
} |
| 197 |
XWX <- crossprod(X_mat, wi * X_mat) |
| 198 |
} else { |
| 199 |
XWX <- crossprod(X_mat) |
| 200 |
} |
| 201 |
B <- chol2inv(chol(XWX)) * nobs(x) |
| 202 |
rownames(B) <- colnames(B) <- colnames(X_mat) |
| 203 |
B |
| 204 |
} |
| 205 |
|
| 206 |
#' @export |
| 207 |
|
| 208 |
v_scale.robu <- function(obj) { |
| 209 |
nobs(obj) |
| 210 |
} |