| 1 |
#-------------------------------------------------- |
| 2 |
# helper functions for constructing constraint matrices |
| 3 |
#-------------------------------------------------- |
| 4 |
|
| 5 |
#' @name constraint_matrices |
| 6 |
#' @title Create constraint matrices |
| 7 |
#' |
| 8 |
#' @description Helper functions to create common types of constraint matrices, |
| 9 |
#' for use with \code{\link{Wald_test}} to conduct Wald-type tests of linear |
| 10 |
#' contrasts from a fitted regression model. |
| 11 |
#' |
| 12 |
#' @param constraints Set of constraints to test. Can be logical (using |
| 13 |
#' \code{TRUE} to specify which coefficients to constrain), integer (specify |
| 14 |
#' the index of coefficients to constrain), character (specify the names of |
| 15 |
#' the coefficients to constrain), or a regular expression. |
| 16 |
#' @param coefs Vector of coefficient estimates, used to determine the column |
| 17 |
#' dimension of the constraint matrix. Can be omitted if the function is |
| 18 |
#' called inside \code{Wald_test()}. |
| 19 |
#' @param reg_ex Logical indicating whether \code{constraints} should be |
| 20 |
#' interpreted as a regular expression. Defaults to \code{FALSE}. |
| 21 |
#' @param with_zero Logical indicating whether coefficients should also be |
| 22 |
#' compared to zero. Defaults to \code{FALSE}. |
| 23 |
#' |
| 24 |
#' @details Constraints can be specified as character vectors, regular |
| 25 |
#' expressions (with \code{reg_ex = TRUE}), integer vectors, or logical |
| 26 |
#' vectors. |
| 27 |
#' |
| 28 |
#' \code{constrain_zero()} Creates a matrix that constrains a specified set of |
| 29 |
#' coefficients to all be equal to zero. |
| 30 |
#' |
| 31 |
#' \code{constrain_equal()} Creates a matrix that constrains a specified set |
| 32 |
#' of coefficients to all be equal. |
| 33 |
#' |
| 34 |
#' \code{constrain_pairwise()} Creates a list of constraint matrices |
| 35 |
#' consisting of all pairwise comparisons between a specified set of |
| 36 |
#' coefficients. If \code{with_zero = TRUE}, then the list will also include a |
| 37 |
#' set of constraint matrices comparing each coefficient to zero. |
| 38 |
#' |
| 39 |
#' @return A matrix or list of matrices encoding the specified set of |
| 40 |
#' constraints. |
| 41 |
#' |
| 42 |
#' @seealso \code{\link{Wald_test}} |
| 43 |
#' |
| 44 |
#' @examples |
| 45 |
#' |
| 46 |
#' if (requireNamespace("carData", quietly = TRUE)) withAutoprint({ |
| 47 |
#' |
| 48 |
#' data(Duncan, package = "carData") |
| 49 |
#' Duncan$cluster <- sample(LETTERS[1:8], size = nrow(Duncan), replace = TRUE) |
| 50 |
#' |
| 51 |
#' Duncan_fit <- lm(prestige ~ 0 + type + income + type:income + type:education, data=Duncan) |
| 52 |
#' # Note that type:income terms are interactions because main effect of income is included |
| 53 |
#' # but type:education terms are separate slopes for each unique level of type |
| 54 |
#' |
| 55 |
#' Duncan_coefs <- coef(Duncan_fit) |
| 56 |
#' |
| 57 |
#' # The following are all equivalent |
| 58 |
#' constrain_zero(constraints = c("typeprof:income","typewc:income"), |
| 59 |
#' coefs = Duncan_coefs) |
| 60 |
#' constrain_zero(constraints = ":income", coefs = Duncan_coefs, |
| 61 |
#' reg_ex = TRUE) |
| 62 |
#' constrain_zero(constraints = 5:6, coefs = Duncan_coefs) |
| 63 |
#' constrain_zero(constraints = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE), |
| 64 |
#' coefs = Duncan_coefs) |
| 65 |
#' |
| 66 |
#' # The following are all equivalent |
| 67 |
#' constrain_equal(c("typebc:education","typeprof:education","typewc:education"), |
| 68 |
#' Duncan_coefs) |
| 69 |
#' constrain_equal(":education", Duncan_coefs, reg_ex = TRUE) |
| 70 |
#' constrain_equal(7:9, Duncan_coefs) |
| 71 |
#' constrain_equal(c(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE), |
| 72 |
#' Duncan_coefs) |
| 73 |
#' |
| 74 |
#' # Test pairwise equality of the education slopes |
| 75 |
#' constrain_pairwise(":education", Duncan_coefs, |
| 76 |
#' reg_ex = TRUE) |
| 77 |
#' |
| 78 |
#' # Test pairwise equality of the income slopes, plus compare against zero |
| 79 |
#' constrain_pairwise(":income", Duncan_coefs, |
| 80 |
#' reg_ex = TRUE, with_zero = TRUE) |
| 81 |
#' |
| 82 |
#' }) |
| 83 |
#' |
| 84 |
#' @rdname constraint_matrices |
| 85 |
#' @export |
| 86 |
|
| 87 |
|
| 88 |
constrain_zero <- function(constraints, coefs, reg_ex = FALSE) { |
| 89 |
|
| 90 |
if (missing(coefs)) { |
| 91 |
f <- function(coefs) constrain_zero(constraints = constraints, |
| 92 |
coefs = coefs, |
| 93 |
reg_ex = reg_ex) |
| 94 |
return(f) |
| 95 |
} |
| 96 |
|
| 97 |
if (is.list(constraints)) { |
| 98 |
constraint_list <- lapply(constraints, constrain_zero, |
| 99 |
coefs = coefs, reg_ex = reg_ex) |
| 100 |
return(constraint_list) |
| 101 |
} |
| 102 |
|
| 103 |
p <- length(coefs) |
| 104 |
|
| 105 |
if (reg_ex) { |
| 106 |
if (!inherits(constraints, "character")) stop("When reg_ex = TRUE, constraints must be a regular expression.") |
| 107 |
constraints <- grepl(constraints, names(coefs)) |
| 108 |
} |
| 109 |
|
| 110 |
if ((inherits(constraints, "logical") & sum(as.logical(constraints)) < 1L) | length(constraints) < 1L) stop("You must specify at least one constraint.") |
| 111 |
|
| 112 |
if (inherits(constraints, "logical")) { |
| 113 |
if (length(constraints) != p) stop(paste0("Constraint logicals must be of length ",p,".")) |
| 114 |
C_mat <- diag(1L, nrow = p)[constraints,,drop=FALSE] |
| 115 |
} |
| 116 |
|
| 117 |
if (inherits(constraints, "numeric") | inherits(constraints, "integer")) { |
| 118 |
if (any(!(constraints %in% 1:p))) stop(paste0("Constraint indices must be less than or equal to ",p,".")) |
| 119 |
C_mat <- diag(1L, nrow = p)[constraints,,drop=FALSE] |
| 120 |
} |
| 121 |
|
| 122 |
if (inherits(constraints, "character")) { |
| 123 |
term_names <- names(coefs) |
| 124 |
if (any(!constraints %in% term_names)) stop("Constraint names not in model specification.") |
| 125 |
C_mat <- diag(1L, nrow = p)[term_names %in% constraints,,drop=FALSE] |
| 126 |
} |
| 127 |
|
| 128 |
coef_NA <- is.na(coefs) |
| 129 |
C_mat[,!coef_NA,drop=FALSE] |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
#' @rdname constraint_matrices |
| 134 |
#' @export |
| 135 |
|
| 136 |
constrain_equal <- function(constraints, coefs, reg_ex = FALSE) { |
| 137 |
|
| 138 |
if (missing(coefs)) { |
| 139 |
f <- function(coefs) constrain_equal(constraints = constraints, coefs = coefs, reg_ex = reg_ex) |
| 140 |
return(f) |
| 141 |
} |
| 142 |
|
| 143 |
if (is.list(constraints)) { |
| 144 |
constraint_list <- lapply(constraints, constrain_equal, |
| 145 |
coefs = coefs, reg_ex = reg_ex) |
| 146 |
return(constraint_list) |
| 147 |
} |
| 148 |
|
| 149 |
if (reg_ex) { |
| 150 |
if (!inherits(constraints, "character")) stop("When reg_ex = TRUE, constraints must be a regular expression.") |
| 151 |
constraints <- grepl(constraints, names(coefs)) |
| 152 |
} |
| 153 |
|
| 154 |
if ((inherits(constraints, "logical") & sum(as.logical(constraints)) < 2L) | length(constraints) < 2L) stop("You must specify at least two constraints.") |
| 155 |
|
| 156 |
C_mat <- constrain_zero(constraints = constraints, coefs = coefs) |
| 157 |
|
| 158 |
first_constraint <- which(C_mat[1,] > 0) |
| 159 |
C_mat[,first_constraint] <- -1L |
| 160 |
C_mat[-1,,drop=FALSE] |
| 161 |
} |
| 162 |
|
| 163 |
#' @rdname constraint_matrices |
| 164 |
#' @export |
| 165 |
|
| 166 |
constrain_pairwise <- function(constraints, coefs, reg_ex = FALSE, with_zero = FALSE) { |
| 167 |
|
| 168 |
if (missing(coefs)) { |
| 169 |
f <- function(coefs) constrain_pairwise(constraints = constraints, |
| 170 |
coefs = coefs, |
| 171 |
reg_ex = reg_ex, |
| 172 |
with_zero = with_zero) |
| 173 |
return(f) |
| 174 |
} |
| 175 |
|
| 176 |
if (is.list(constraints)) { |
| 177 |
constraint_list <- lapply(constraints, constrain_pairwise, |
| 178 |
coefs = coefs, reg_ex = reg_ex, with_zero = with_zero) |
| 179 |
constraint_list <- unlist(constraint_list, recursive = FALSE) |
| 180 |
return(constraint_list) |
| 181 |
} |
| 182 |
|
| 183 |
p <- length(coefs) |
| 184 |
term_names <- names(coefs) |
| 185 |
|
| 186 |
if (reg_ex) { |
| 187 |
if (!inherits(constraints, "character")) stop("When reg_ex = TRUE, constraints must be a regular expression.") |
| 188 |
constraints <- grepl(constraints, names(coefs)) |
| 189 |
} |
| 190 |
|
| 191 |
if ((inherits(constraints, "logical") & sum(as.logical(constraints)) < 2L) | length(constraints) < 2L) stop("You must specify at least two constraints.") |
| 192 |
|
| 193 |
if (inherits(constraints, "logical")) { |
| 194 |
if (length(constraints) != p) stop(paste0("Constraint logicals must be of length ",p,".")) |
| 195 |
constraint_indices <- which(constraints) |
| 196 |
} |
| 197 |
|
| 198 |
if (inherits(constraints, "numeric") | inherits(constraints, "integer")) { |
| 199 |
if (any(!(constraints %in% 1:p))) stop(paste0("Constraint indices must be less than or equal to ",p,".")) |
| 200 |
constraint_indices <- as.integer(constraints) |
| 201 |
} |
| 202 |
|
| 203 |
if (inherits(constraints, "character")) { |
| 204 |
if (!all(constraints %in% term_names)) stop("Constraint names not in model specification.") |
| 205 |
constraint_indices <- which(term_names %in% constraints) |
| 206 |
} |
| 207 |
|
| 208 |
zero_mat <- matrix(0L, nrow = 1, ncol = p) |
| 209 |
|
| 210 |
constraint_pairs <- utils::combn(constraint_indices, 2, simplify = FALSE) |
| 211 |
|
| 212 |
names(constraint_pairs) <- sapply(constraint_pairs, function(x) paste(term_names[rev(x)], collapse = " - ")) |
| 213 |
|
| 214 |
C_mats <- lapply(constraint_pairs, function(x) { |
| 215 |
zero_mat[,x] <- c(-1L, 1L) |
| 216 |
zero_mat |
| 217 |
}) |
| 218 |
|
| 219 |
if (with_zero) { |
| 220 |
|
| 221 |
names(constraint_indices) <- term_names[constraint_indices] |
| 222 |
C_to_zero <- lapply(constraint_indices, function(x) { |
| 223 |
zero_mat[,x] <- 1L |
| 224 |
zero_mat |
| 225 |
}) |
| 226 |
|
| 227 |
C_mats <- c(C_to_zero, C_mats) |
| 228 |
} |
| 229 |
|
| 230 |
return(C_mats) |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
#--------------------------------------------- |
| 236 |
# Wald-type tests |
| 237 |
#--------------------------------------------- |
| 238 |
|
| 239 |
#' Test parameter constraints in a fitted linear regression model |
| 240 |
#' |
| 241 |
#' \code{Wald_test} reports Wald-type tests of linear contrasts from a fitted |
| 242 |
#' linear regression model, using a sandwich estimator for the |
| 243 |
#' variance-covariance matrix and a small sample correction for the p-value. |
| 244 |
#' Several different small-sample corrections are available. |
| 245 |
#' |
| 246 |
#' @param obj Fitted model for which to calculate Wald tests. |
| 247 |
#' @param constraints constraint or list of multiple constraints to test. See |
| 248 |
#' details and examples. |
| 249 |
#' @param vcov Variance covariance matrix estimated using \code{vcovCR} or a |
| 250 |
#' character string specifying which small-sample adjustment should be used to |
| 251 |
#' calculate the variance-covariance. |
| 252 |
#' @param null_constant vector of null values or list of such vectors for each |
| 253 |
#' set of constraints to test. For a single \code{constraint}, the null values |
| 254 |
#' must have length equal to the number of rows in the constraint. For lists |
| 255 |
#' of null values, each entry must have length equal to the number of rows in |
| 256 |
#' the corresponding entry of \code{constraints}. Default is \code{0}, in |
| 257 |
#' which case the null values are taken to be zero (for every entry, if |
| 258 |
#' \code{constraints} is a list). |
| 259 |
#' @param test Character vector specifying which small-sample correction(s) to |
| 260 |
#' calculate. The following corrections are available: \code{"chi-sq"}, |
| 261 |
#' \code{"Naive-F"}, \code{"Naive-Fp"}, \code{"HTA"}, \code{"HTB"}, |
| 262 |
#' \code{"HTZ"}, \code{"EDF"}, \code{"EDT"}. Default is \code{"HTZ"}. |
| 263 |
#' @param tidy Logical value controlling whether to tidy the test results. If |
| 264 |
#' \code{constraints} is a list with multiple constraints, the result will be |
| 265 |
#' coerced into a data frame when \code{tidy = TRUE}. |
| 266 |
#' @param adjustment_method A character string indicating a multiple comparisons |
| 267 |
#' correction to apply to p-values in instances where multiple tests are run. |
| 268 |
#' Possible options are from \code{\link[stats]{p.adjust.methods}}, which is |
| 269 |
#' passed to \code{\link[stats]{p.adjust}} to correct p-values for multiple |
| 270 |
#' comparisons. Defaults to \code{"none"}. |
| 271 |
#' @param ... Further arguments passed to \code{\link{vcovCR}}, which are only |
| 272 |
#' needed if \code{vcov} is a character string. |
| 273 |
#' |
| 274 |
#' @details Constraints can be specified directly as q X p matrices or |
| 275 |
#' indirectly through \code{\link{constrain_equal}}, |
| 276 |
#' \code{\link{constrain_zero}}, or \code{\link{constrain_pairwise}}. By |
| 277 |
#' default, each constraint will be tested against the null hypothesis that it |
| 278 |
#' equal to a zero vector. Non-zero values for null-hypotheses can be |
| 279 |
#' specified using the \code{null_constant} argument. |
| 280 |
#' |
| 281 |
#' @return A list of test results. |
| 282 |
#' |
| 283 |
#' @seealso \code{\link{vcovCR}}, \code{\link{constrain_equal}}, |
| 284 |
#' \code{\link{constrain_zero}}, \code{\link{constrain_pairwise}} |
| 285 |
#' |
| 286 |
#' @examples |
| 287 |
#' |
| 288 |
#' |
| 289 |
#' if (requireNamespace("carData", quietly = TRUE)) withAutoprint({ |
| 290 |
#' |
| 291 |
#' data(Duncan, package = "carData") |
| 292 |
#' Duncan$cluster <- sample(LETTERS[1:8], size = nrow(Duncan), replace = TRUE) |
| 293 |
#' |
| 294 |
#' Duncan_fit <- lm(prestige ~ 0 + type + income + type:income + type:education, data=Duncan) |
| 295 |
#' # Note that type:income terms are interactions because main effect of income is included |
| 296 |
#' # but type:education terms are separate slopes for each unique level of type |
| 297 |
#' |
| 298 |
#' # Test equality of intercepts |
| 299 |
#' Wald_test(Duncan_fit, |
| 300 |
#' constraints = constrain_equal(1:3), |
| 301 |
#' vcov = "CR2", cluster = Duncan$cluster) |
| 302 |
#' |
| 303 |
#' # Test equality of type-by-education slopes |
| 304 |
#' Wald_test(Duncan_fit, |
| 305 |
#' constraints = constrain_equal(":education", reg_ex = TRUE), |
| 306 |
#' vcov = "CR2", cluster = Duncan$cluster) |
| 307 |
#' |
| 308 |
#' # Pairwise comparisons of type-by-education slopes |
| 309 |
#' Wald_test(Duncan_fit, |
| 310 |
#' constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 311 |
#' vcov = "CR2", cluster = Duncan$cluster) |
| 312 |
#' |
| 313 |
#' # Test type-by-income interactions |
| 314 |
#' Wald_test(Duncan_fit, |
| 315 |
#' constraints = constrain_zero(":income", reg_ex = TRUE), |
| 316 |
#' vcov = "CR2", cluster = Duncan$cluster) |
| 317 |
#' |
| 318 |
#' # Pairwise comparisons of type-by-income interactions |
| 319 |
#' Wald_test(Duncan_fit, |
| 320 |
#' constraints = constrain_pairwise(":income", reg_ex = TRUE, with_zero = TRUE), |
| 321 |
#' vcov = "CR2", cluster = Duncan$cluster) |
| 322 |
#' |
| 323 |
#' # Pairwise comparisons of type-by-education slopes, with two tests and |
| 324 |
#' # multiple comparisons p-value adjustment |
| 325 |
#' Wald_test(Duncan_fit, |
| 326 |
#' constraints = constrain_pairwise(":education", reg_ex = TRUE), |
| 327 |
#' vcov = "CR2", |
| 328 |
#' cluster = Duncan$cluster, |
| 329 |
#' test = c("HTZ","chi-sq"), |
| 330 |
#' adjustment_method = "holm") |
| 331 |
#' |
| 332 |
#' }) |
| 333 |
#' |
| 334 |
#' @export |
| 335 |
|
| 336 |
|
| 337 |
Wald_test <- function( |
| 338 |
obj, |
| 339 |
constraints, |
| 340 |
vcov, |
| 341 |
null_constant = 0, |
| 342 |
test = "HTZ", |
| 343 |
tidy = FALSE, |
| 344 |
adjustment_method = "none", |
| 345 |
... |
| 346 |
) { |
| 347 |
|
| 348 |
if (is.character(vcov)) vcov <- vcovCR(obj, type = vcov, ...) |
| 349 |
if (!inherits(vcov, "clubSandwich")) stop("Variance-covariance matrix must be a clubSandwich.") |
| 350 |
|
| 351 |
all_tests <- c("chi-sq","Naive-F","Naive-Fp","HTA","HTB","HTZ","EDF","EDT") |
| 352 |
if (all(test == "All")) test <- all_tests |
| 353 |
test <- match.arg(test, all_tests, several.ok = TRUE) |
| 354 |
|
| 355 |
beta <- na.omit(coef_CS(obj)) |
| 356 |
p <- length(beta) |
| 357 |
|
| 358 |
GH <- get_GH(obj, vcov) |
| 359 |
|
| 360 |
# Evaluate constrain_*() functions if used |
| 361 |
if (inherits(constraints, "function")) { |
| 362 |
constraints <- constraints(coef_CS(obj)) |
| 363 |
} |
| 364 |
|
| 365 |
if (is.list(constraints)) { |
| 366 |
|
| 367 |
constraints <- lapply(constraints, function(x) { |
| 368 |
if (inherits(x, "function")) x(coef_CS(obj)) else x |
| 369 |
}) |
| 370 |
|
| 371 |
# List of constraints |
| 372 |
if (!all(sapply(constraints, inherits, "matrix") & sapply(constraints, ncol) == p)) { |
| 373 |
stop(paste0("Constraints must be a q X ", p," matrix, a list of such matrices, or a call to a constrain_*() function.")) |
| 374 |
} |
| 375 |
|
| 376 |
q_constraints <- sapply(constraints, nrow) |
| 377 |
|
| 378 |
null_consts_error_txt <- "Each null_constant must be a numeric vector with length equal to the number of rows in the corresponding constraint matrix." |
| 379 |
|
| 380 |
if (is.list(null_constant)) { |
| 381 |
if (length(null_constant) != length(constraints)) stop("null_constant must be a single vector or a list with the same number of entries as the constraint list.") |
| 382 |
check_numeric <- sapply(null_constant, is.numeric) |
| 383 |
if (!all(check_numeric)) stop(null_consts_error_txt) |
| 384 |
} else if (is.numeric(null_constant)) { |
| 385 |
if (length(null_constant) > 1L) { |
| 386 |
null_constant <- rep(list(null_constant), length(constraints)) |
| 387 |
} else { |
| 388 |
null_constant <- lapply(q_constraints, function(x) rep(null_constant, x)) |
| 389 |
} |
| 390 |
} else { |
| 391 |
stop(null_consts_error_txt) |
| 392 |
} |
| 393 |
|
| 394 |
q_null <- lengths(null_constant) |
| 395 |
mismatches <- q_constraints != q_null |
| 396 |
if (any(mismatches)) { |
| 397 |
which_mis <- which(mismatches) |
| 398 |
mismatch_msg <- c( |
| 399 |
null_consts_error_txt, |
| 400 |
paste0("Constraint ", which_mis, " has ", q_constraints[mismatches], " rows; null constant ", which_mis, " is length ", q_null[mismatches],".") |
| 401 |
) |
| 402 |
stop(paste(mismatch_msg, collapse = " ")) |
| 403 |
} |
| 404 |
|
| 405 |
results <- mapply( |
| 406 |
Wald_testing, |
| 407 |
C_mat = constraints, null_constant = null_constant, |
| 408 |
MoreArgs = list(beta = beta, vcov = vcov, test = test, p = p, GH = GH, stop_on_NPD = FALSE), |
| 409 |
SIMPLIFY = FALSE |
| 410 |
) |
| 411 |
|
| 412 |
if (tidy) { |
| 413 |
results <- mapply( |
| 414 |
function(x, nm) cbind(hypothesis = rep(nm, nrow(x)), x, stringsAsFactors = FALSE), |
| 415 |
x = results, nm = names(results), SIMPLIFY = FALSE |
| 416 |
) |
| 417 |
results <- do.call(rbind, c(results, make.row.names = FALSE)) |
| 418 |
class(results) <- c("Wald_test_clubSandwich",class(results)) |
| 419 |
} |
| 420 |
|
| 421 |
# p-value adjustment |
| 422 |
|
| 423 |
adjustment_method <- match.arg(adjustment_method, p.adjust.methods, several.ok = FALSE) |
| 424 |
|
| 425 |
if (adjustment_method != "none" & length(results) == 1) { |
| 426 |
warning("Only one p-value is available. No p-value adjustment will be performed.") |
| 427 |
} else if (adjustment_method != "none") { # skip if adjustment_method == "none" |
| 428 |
if (tidy) { |
| 429 |
results$p_val <- with(results, ave(p_val, test, FUN = function(p) p.adjust(p, method = adjustment_method))) |
| 430 |
} else { |
| 431 |
# Extract p-values |
| 432 |
p_values <- lapply(results, function(x) x$p_val) |
| 433 |
p_values <- matrix(unlist(p_values), ncol = length(results)) |
| 434 |
|
| 435 |
# Perform p-value adjustment |
| 436 |
p_values <- apply(p_values, MARGIN = 1, p.adjust, adjustment_method, simplify = TRUE) |
| 437 |
|
| 438 |
# Apply adjusted p-values to results |
| 439 |
for (i in seq_along(results)) { |
| 440 |
results[[i]]$p_val <- p_values[i,] |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
} else { |
| 446 |
|
| 447 |
|
| 448 |
if (!inherits(constraints, "matrix") | ncol(constraints) != p) { |
| 449 |
stop(paste0("Constraints must be a q X ", p," matrix, a list of such matrices, or a call to a constrain_*() function.")) |
| 450 |
} |
| 451 |
|
| 452 |
null_consts_error_txt <- "null_constant must be a numeric vector with length equal to the number of rows in the constraint matrix." |
| 453 |
|
| 454 |
if (is.numeric(null_constant)) { |
| 455 |
if (length(null_constant) == 1L) { |
| 456 |
null_constant <- rep(null_constant, nrow(constraints)) |
| 457 |
} else { |
| 458 |
if (length(null_constant) != nrow(constraints)) { |
| 459 |
stop(null_consts_error_txt) |
| 460 |
} |
| 461 |
} |
| 462 |
} else { |
| 463 |
stop(null_consts_error_txt) |
| 464 |
} |
| 465 |
|
| 466 |
results <- Wald_testing( |
| 467 |
C_mat = constraints, null_constant = null_constant, |
| 468 |
beta = beta, vcov = vcov, test = test, p = p, GH = GH |
| 469 |
) |
| 470 |
} |
| 471 |
|
| 472 |
return(results) |
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
array_multiply <- function(mat, arr) { |
| 478 |
new_mat <- apply(arr, 3, function(s) mat %*% s) |
| 479 |
array(new_mat, dim = c(nrow(mat), dim(arr)[2], dim(arr)[3])) |
| 480 |
} |
| 481 |
|
| 482 |
Wald_testing <- function(C_mat, null_constant, beta, vcov, test, p, GH, stop_on_NPD = TRUE) { |
| 483 |
|
| 484 |
q <- nrow(C_mat) |
| 485 |
dims <- dim(GH$H) |
| 486 |
J <- dims[length(dims)] |
| 487 |
|
| 488 |
if (any(c("HTA","HTB","HTZ","EDF","EDT") %in% test)) { |
| 489 |
GH$G <- lapply(GH$G, function(s) C_mat %*% s) |
| 490 |
if (length(dims)==3) { |
| 491 |
GH$H <- array_multiply(C_mat, GH$H) |
| 492 |
} else { |
| 493 |
H <- array(NA, dim = c(3, q, dims[3:4])) |
| 494 |
for (i in 1:dims[1]) H[i,,,] <- array_multiply(C_mat, GH$H[i,,,]) |
| 495 |
GH$H <- H |
| 496 |
} |
| 497 |
P_array <- get_P_array(GH = GH, all_terms = TRUE) |
| 498 |
Omega <- apply(P_array, 1:2, function(x) sum(diag(x))) |
| 499 |
Omega_nsqrt <- matrix_power(Omega, -1/2) |
| 500 |
} |
| 501 |
|
| 502 |
# Wald statistic |
| 503 |
inverse_vcov <- tryCatch( |
| 504 |
chol2inv(chol(C_mat %*% vcov %*% t(C_mat))), |
| 505 |
error = function(e) e |
| 506 |
) |
| 507 |
|
| 508 |
if (inherits(inverse_vcov, "error")) { |
| 509 |
if (stop_on_NPD) { |
| 510 |
stop("Variance-covariance matrix of the contrast is not positive definite. The test cannot be computed.") |
| 511 |
} else { |
| 512 |
result <- data.frame( |
| 513 |
test = test, |
| 514 |
Fstat = NA_real_, |
| 515 |
delta = NA_real_, |
| 516 |
df_num = q, |
| 517 |
df_denom = NA_real_, |
| 518 |
p_val = NA_real_ |
| 519 |
) |
| 520 |
} |
| 521 |
} else { |
| 522 |
C_beta <- (C_mat %*% beta - matrix(null_constant, ncol = 1L)) |
| 523 |
Q <- as.numeric(t(C_beta) %*% inverse_vcov %*% C_beta) |
| 524 |
|
| 525 |
result <- data.frame() |
| 526 |
|
| 527 |
# chi-square |
| 528 |
if ("chi-sq" %in% test) { |
| 529 |
p_val <- pchisq(Q, df = q, lower.tail = FALSE) |
| 530 |
result <- rbind(result, |
| 531 |
data.frame(test = "chi-sq", Fstat = Q / q, |
| 532 |
delta = 1, df_num = q, df_denom = Inf, p_val = p_val)) |
| 533 |
} |
| 534 |
|
| 535 |
# Naive F |
| 536 |
if ("Naive-F" %in% test) { |
| 537 |
p_val <- pf(Q / q, df1 = q, df2 = J - 1, lower.tail = FALSE) |
| 538 |
result <- rbind(result, |
| 539 |
data.frame(test = "Naive-F", Fstat = Q / q, |
| 540 |
delta = 1, df_num = q, df_denom = J - 1, p_val = p_val)) |
| 541 |
} |
| 542 |
|
| 543 |
# Naive F with J - p degrees of freedom |
| 544 |
if ("Naive-Fp" %in% test) { |
| 545 |
p_val <- pf(Q / q, df1 = q, df2 = J - p, lower.tail = FALSE) |
| 546 |
result <- rbind(result, |
| 547 |
data.frame(test = "Naive-Fp", Fstat = Q / q, |
| 548 |
delta = 1, df_num = q, df_denom = J - p, p_val = p_val)) |
| 549 |
} |
| 550 |
|
| 551 |
# Hotelling's T-squared |
| 552 |
if ("HTA" %in% test | "HTB" %in% test) { |
| 553 |
Cov_arr <- covariance_array(P_array, Omega_nsqrt, q = q) |
| 554 |
|
| 555 |
Var_index <- seq(1,q^4, 1 + q^2) |
| 556 |
Var_mat <- matrix(Cov_arr[Var_index], q, q) |
| 557 |
|
| 558 |
|
| 559 |
if ("HTA" %in% test) { |
| 560 |
nu_A <- 2 * sum(Var_mat) / sum(Cov_arr^2) |
| 561 |
result <- rbind(result, data.frame(test = "HTA", Hotelling_Tsq(Q, q, nu = nu_A))) |
| 562 |
} |
| 563 |
|
| 564 |
if ("HTB" %in% test) { |
| 565 |
lower_mat <- lower.tri(Var_mat, diag = TRUE) |
| 566 |
lower_arr <- array(FALSE, dim = dim(Cov_arr)) |
| 567 |
for (s in 1:q) for (t in 1:s) for (u in 1:s) for (v in 1:(ifelse(u==s,t,u))) lower_arr[s,t,u,v] <- TRUE |
| 568 |
|
| 569 |
nu_B <- 2 * sum(Var_mat[lower_mat]) / sum(Cov_arr[lower_arr]^2) |
| 570 |
result <- rbind(result, data.frame(test = "HTB", Hotelling_Tsq(Q, q, nu = nu_B))) |
| 571 |
} |
| 572 |
} else if ("HTZ" %in% test) { |
| 573 |
Var_mat <- total_variance_mat(P_array, Omega_nsqrt, q = q) |
| 574 |
} |
| 575 |
|
| 576 |
if ("HTZ" %in% test) { |
| 577 |
nu_Z <- q * (q + 1) / sum(Var_mat) |
| 578 |
result <- rbind(result, data.frame(test = "HTZ", Hotelling_Tsq(Q, q, nu = nu_Z))) |
| 579 |
} |
| 580 |
|
| 581 |
# Eigen-decompositions |
| 582 |
|
| 583 |
if ("EDF" %in% test | "EDT" %in% test) { |
| 584 |
spec <- eigen(Omega_nsqrt %*% C_mat %*% vcov %*% t(C_mat) %*% t(Omega_nsqrt)) |
| 585 |
df_eig <- 1 / apply(t(spec$vectors) %*% Omega_nsqrt, 1, |
| 586 |
function(x) sum(apply(P_array, 3:4, |
| 587 |
function(P) (t(x) %*% P %*% x)^2))) |
| 588 |
|
| 589 |
if ("EDF" %in% test) { |
| 590 |
df4 <- pmax(df_eig, 4.1) |
| 591 |
EQ <- sum(df4 / (df4 - 2)) |
| 592 |
VQ <- 2 * sum(df4^2 * (df4 - 1) / ((df4 - 2)^2 * (df4 - 4))) |
| 593 |
delta <- ifelse(q * VQ > 2 * EQ^2, (EQ^2 * (q - 2) + 2 * q * VQ) / (EQ * (VQ + EQ^2)), q / EQ) |
| 594 |
df <- ifelse(q * VQ > 2 * EQ^2, 4 + 2 * EQ^2 * (q + 2) / (q * VQ - 2 * EQ^2), Inf) |
| 595 |
Fstat <- delta * Q / q |
| 596 |
p_val <- pf(Fstat, df1 = q, df2 = df, lower.tail = FALSE) |
| 597 |
result <- rbind(result, |
| 598 |
data.frame(test = "EDF", Fstat = Fstat, |
| 599 |
delta = delta, df_num = q, df_denom = df, p_val = p_val)) |
| 600 |
} |
| 601 |
|
| 602 |
if ("EDT" %in% test) { |
| 603 |
t_j <- t(spec$vectors) %*% Omega_nsqrt %*% C_mat %*% beta / sqrt(spec$values) |
| 604 |
a_j <- df_eig - 1 / 2 |
| 605 |
b_j <- 48 * a_j^2 |
| 606 |
c_j <- sqrt(a_j * log(1 + t_j^2 / df_eig)) |
| 607 |
z_j <- c_j + (c_j^3 + 3 * c_j) / b_j - |
| 608 |
(4 * c_j^7 + 33 * c_j^5 + 240 * c_j^3 + 855 * c_j) / |
| 609 |
(10 * b_j^2 + 8 * b_j * c_j^4 + 1000 * b_j) |
| 610 |
Fstat <- mean(z_j^2) |
| 611 |
p_val <- pf(Fstat, df1 = q, df2 = Inf, lower.tail = FALSE) |
| 612 |
result <- rbind(result, |
| 613 |
data.frame(test = "EDT", Fstat = Fstat, |
| 614 |
delta = 1, df_num = q, df_denom = Inf, p_val = p_val)) |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
class(result) <- c("Wald_test_clubSandwich", class(result)) |
| 620 |
attr(result, "type") <- attr(vcov, "type") |
| 621 |
result |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
#-------------------------------------------------- |
| 626 |
# calculate a covariance array |
| 627 |
#-------------------------------------------------- |
| 628 |
|
| 629 |
covariance_array <- function(P_array, Omega_nsqrt, q = nrow(Omega_nsqrt)) { |
| 630 |
|
| 631 |
B_jk <- array(apply(P_array, 3:4, function(p) Omega_nsqrt %*% p %*% Omega_nsqrt), |
| 632 |
dim = dim(P_array)) |
| 633 |
|
| 634 |
Cov_arr <- array(NA, dim = rep(q, 4)) |
| 635 |
for (s in 1:q) for (t in 1:s) for (u in 1:s) for (v in 1:(ifelse(u==s,t,u))) { |
| 636 |
temp <- sum(B_jk[s,v,,] * B_jk[t,u,,]) + sum(B_jk[s,u,,] * B_jk[t,v,,]) |
| 637 |
Cov_arr[s,t,u,v] <- temp |
| 638 |
Cov_arr[s,t,v,u] <- temp |
| 639 |
Cov_arr[t,s,u,v] <- temp |
| 640 |
Cov_arr[t,s,v,u] <- temp |
| 641 |
Cov_arr[u,v,s,t] <- temp |
| 642 |
Cov_arr[u,v,t,s] <- temp |
| 643 |
Cov_arr[v,u,s,t] <- temp |
| 644 |
Cov_arr[v,u,t,s] <- temp |
| 645 |
} |
| 646 |
Cov_arr |
| 647 |
} |
| 648 |
|
| 649 |
#--------------------------------------------------------- |
| 650 |
# calculate total variance of clubSandwich estimator |
| 651 |
#--------------------------------------------------------- |
| 652 |
|
| 653 |
total_variance_mat <- function(P_array, Omega_nsqrt, q = nrow(Omega_nsqrt)) { |
| 654 |
B_jk <- array(apply(P_array, 3:4, function(p) Omega_nsqrt %*% p %*% Omega_nsqrt), dim = dim(P_array)) |
| 655 |
|
| 656 |
var_mat <- matrix(NA, q, q) |
| 657 |
for (s in 1:q) for (t in 1:s) { |
| 658 |
temp <- sum(B_jk[s,t,,] * B_jk[t,s,,]) + sum(B_jk[s,s,,] * B_jk[t,t,,]) |
| 659 |
var_mat[s,t] <- temp |
| 660 |
var_mat[t,s] <- temp |
| 661 |
} |
| 662 |
var_mat |
| 663 |
} |
| 664 |
|
| 665 |
#-------------------------------------------------- |
| 666 |
# Hotelling's T-squared approximation |
| 667 |
#-------------------------------------------------- |
| 668 |
|
| 669 |
Hotelling_Tsq <- function(Q, q, nu) { |
| 670 |
delta <- pmax((nu - q + 1) / nu, 0) |
| 671 |
df <- nu - q + 1 |
| 672 |
Fstat <- delta * Q / q |
| 673 |
p_val <- ifelse(df > 0, pf(Fstat, df1 = q, df2 = df, lower.tail = FALSE), as.numeric(NA)) |
| 674 |
data.frame(Fstat = Fstat, delta = delta, df_num = q, df_denom = df, p_val = p_val) |
| 675 |
} |
| 676 |
|
| 677 |
#--------------------------------------------- |
| 678 |
# print method for Wald_test |
| 679 |
#--------------------------------------------- |
| 680 |
|
| 681 |
#' @export |
| 682 |
|
| 683 |
print.Wald_test_clubSandwich <- function(x, digits = 3, ...) { |
| 684 |
res <- x |
| 685 |
res$delta <- NULL |
| 686 |
res$p_val <- format.pval(x$p_val, digits = digits, eps = 10^-digits) |
| 687 |
res$sig <- symnum(x$p_val, corr = FALSE, na = FALSE, |
| 688 |
cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1), |
| 689 |
symbols = c("***", "**", "*", ".", " ")) |
| 690 |
|
| 691 |
print(format(res, digits = 3), row.names = FALSE) |
| 692 |
} |