| 1 |
#------------------------------------- |
| 2 |
# vcovCR with defaults |
| 3 |
#------------------------------------- |
| 4 |
|
| 5 |
#' Cluster-robust variance-covariance matrix for a robu 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[robumeta]{robu}} object. |
| 10 |
#' |
| 11 |
#' @param cluster Optional expression or vector indicating which observations |
| 12 |
#' belong to the same cluster. If not specified, will be set to the |
| 13 |
#' \code{studynum} used in fitting the \code{\link[robumeta]{robu}} object. |
| 14 |
#' @param target Optional matrix or vector describing the working |
| 15 |
#' variance-covariance model used to calculate the \code{CR2} and \code{CR4} |
| 16 |
#' adjustment matrices. If not specified, the target is taken to be the |
| 17 |
#' inverse of the estimated weights used in fitting the |
| 18 |
#' \code{\link[robumeta]{robu}} object. |
| 19 |
#' @inheritParams vcovCR |
| 20 |
#' |
| 21 |
#' @return An object of class \code{c("vcovCR","clubSandwich")}, which consists |
| 22 |
#' of a matrix of the estimated variance of and covariances between the |
| 23 |
#' regression coefficient estimates. |
| 24 |
#' |
| 25 |
#' @seealso \code{\link{vcovCR}} |
| 26 |
#' |
| 27 |
#' @export |
| 28 |
#' |
| 29 |
#' @examples |
| 30 |
#' |
| 31 |
#' if (requireNamespace("robumeta", quietly = TRUE)) withAutoprint({ |
| 32 |
#' library(robumeta) |
| 33 |
#' data(hierdat) |
| 34 |
#' |
| 35 |
#' robu_fit <- robu(effectsize ~ binge + followup + sreport + age, |
| 36 |
#' data = hierdat, studynum = studyid, |
| 37 |
#' var.eff.size = var, modelweights = "HIER") |
| 38 |
#' robu_fit |
| 39 |
#' |
| 40 |
#' robu_CR2 <- vcovCR(robu_fit, type = "CR2") |
| 41 |
#' robu_CR2 |
| 42 |
#' coef_test(robu_fit, vcov = robu_CR2, test = c("Satterthwaite", "saddlepoint")) |
| 43 |
#' |
| 44 |
#' Wald_test(robu_fit, constraints = constrain_zero(c(2,4)), vcov = robu_CR2) |
| 45 |
#' Wald_test(robu_fit, constraints = constrain_zero(2:5), vcov = robu_CR2) |
| 46 |
#' |
| 47 |
#' }) |
| 48 |
#' |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
vcovCR.robu <- function(obj, cluster, type, target, inverse_var, form = "sandwich", ...) { |
| 53 |
if (missing(cluster)) cluster <- obj$study_orig_id |
| 54 |
if (missing(target)) target <- NULL |
| 55 |
if (missing(inverse_var)) inverse_var <- is.null(target) & (!obj$user_weighting) |
| 56 |
vcov_CR(obj, cluster = cluster, type = type, |
| 57 |
target = target, inverse_var = inverse_var, form = form) |
| 58 |
} |
| 59 |
|
| 60 |
#----------------------------------------------- |
| 61 |
# coefficients |
| 62 |
#----------------------------------------------- |
| 63 |
|
| 64 |
#' @export |
| 65 |
|
| 66 |
coef_CS.robu <- function(obj) { |
| 67 |
beta <- as.vector(obj$b.r) |
| 68 |
labs <- obj$reg_table$labels |
| 69 |
if (is.factor(labs)) labs <- levels(labs)[labs] |
| 70 |
names(beta) <- labs |
| 71 |
beta |
| 72 |
} |
| 73 |
|
| 74 |
#----------------------------------------------- |
| 75 |
# residuals |
| 76 |
#----------------------------------------------- |
| 77 |
|
| 78 |
#' @export |
| 79 |
|
| 80 |
residuals_CS.robu <- function(obj) { |
| 81 |
ord <- order(order(obj$study_orig_id)) |
| 82 |
resid <- obj$data.full$e.r[ord] |
| 83 |
|
| 84 |
if (obj$user_weighting) { |
| 85 |
pos_wts <- obj$data.full$userweights[ord] > 0 |
| 86 |
if (!all(pos_wts)) resid <- resid[pos_wts] |
| 87 |
} |
| 88 |
|
| 89 |
return(resid) |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
#----------------------------------------------- |
| 94 |
# Model matrix |
| 95 |
#----------------------------------------------- |
| 96 |
|
| 97 |
#' @export |
| 98 |
|
| 99 |
model_matrix.robu <- function(obj) { |
| 100 |
ord <- order(order(obj$study_orig_id)) |
| 101 |
model_matrix <- obj$Xreg[ord,,drop=FALSE] |
| 102 |
|
| 103 |
if (obj$user_weighting) { |
| 104 |
pos_wts <- obj$data.full$userweights[ord] > 0 |
| 105 |
if (!all(pos_wts)) model_matrix <- model_matrix[pos_wts,,drop=FALSE] |
| 106 |
} |
| 107 |
|
| 108 |
return(model_matrix) |
| 109 |
} |
| 110 |
|
| 111 |
#------------------------------------- |
| 112 |
# Get (model-based) working variance matrix |
| 113 |
#------------------------------------- |
| 114 |
|
| 115 |
#' @export |
| 116 |
|
| 117 |
targetVariance.robu <- function(obj, cluster) { |
| 118 |
ord <- order(order(obj$study_orig_id)) |
| 119 |
if (obj$user_weighting) { |
| 120 |
pos_wts <- obj$data.full$userweights[ord] > 0 |
| 121 |
V <- obj$data.full$avg.var.eff.size[ord][pos_wts] |
| 122 |
} else { |
| 123 |
V <- mean(obj$data.full$r.weights) / obj$data.full$r.weights[ord] |
| 124 |
} |
| 125 |
matrix_list(V, cluster, "both") |
| 126 |
} |
| 127 |
|
| 128 |
#------------------------------------- |
| 129 |
# Get weighting matrix |
| 130 |
#------------------------------------- |
| 131 |
|
| 132 |
#' @export |
| 133 |
|
| 134 |
weights.robu <- function(object, ...) { |
| 135 |
ord <- order(order(object$study_orig_id)) |
| 136 |
if (object$user_weighting) { |
| 137 |
object$data.full$userweights[ord] |
| 138 |
} else{ |
| 139 |
NULL |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
#' @export |
| 144 |
|
| 145 |
weightMatrix.robu <- function(obj, cluster) { |
| 146 |
ord <- order(order(obj$study_orig_id)) |
| 147 |
if (obj$user_weighting) { |
| 148 |
W <- obj$data.full$userweights[ord] |
| 149 |
W <- W[W > 0] |
| 150 |
} else{ |
| 151 |
W <- obj$data.full$r.weights[ord] |
| 152 |
} |
| 153 |
w_scale <- mean(W) |
| 154 |
W <- W / w_scale |
| 155 |
W_list <- matrix_list(W, cluster, "both") |
| 156 |
attr(W_list, "w_scale") <- w_scale |
| 157 |
W_list |
| 158 |
} |
| 159 |
|
| 160 |
#--------------------------------------- |
| 161 |
# Get bread matrix and scaling constant |
| 162 |
#--------------------------------------- |
| 163 |
|
| 164 |
#' @export |
| 165 |
|
| 166 |
bread.robu <- function(x, ...) { |
| 167 |
if (x$user_weighting) { |
| 168 |
W <- x$data.full$userweights |
| 169 |
} else{ |
| 170 |
W <- x$data.full$r.weights |
| 171 |
} |
| 172 |
x$N * chol2inv(chol(crossprod(x$Xreg, W * x$Xreg))) |
| 173 |
} |
| 174 |
|
| 175 |
#' @export |
| 176 |
|
| 177 |
v_scale.robu <- function(obj) { |
| 178 |
obj$N |
| 179 |
} |