| 1 |
context("logit glm objects") |
| 2 |
set.seed(20190513) |
| 3 |
|
| 4 |
m <- 20 |
| 5 |
cluster <- factor(rep(LETTERS[1:m], 3 + rpois(m, 5))) |
| 6 |
n <- length(cluster) |
| 7 |
X1 <- c(rep(-0.5, m / 2), rep(0.5, m / 2))[cluster] |
| 8 |
X2 <- c(rep(-0.3, 0.4 * m), rep(0.7, 0.3 * m), rep(-0.3, 0.4 * m))[cluster] |
| 9 |
X3 <- rnorm(m)[cluster] + rnorm(n) |
| 10 |
X4 <- rnorm(n) |
| 11 |
X <- cbind(X1, X2, X3, X4) |
| 12 |
eta <- -0.4 + X %*% c(0.3, -0.6, 0.15, 0.15) |
| 13 |
p <- 1 / (1 + exp(-eta)) |
| 14 |
summary(p) |
| 15 |
|
| 16 |
w <- sample(1:4, size = n, replace = TRUE) |
| 17 |
y1 <- rbinom(n, size = 1, prob = p) |
| 18 |
y2 <- rbinom(n, size = w, prob = p) |
| 19 |
yp <- y2 / w |
| 20 |
|
| 21 |
dat <- data.frame(y1, y2, yp, X, cluster, w, row = 1:n) |
| 22 |
|
| 23 |
logit_fit <- glm(y1 ~ X1 + X2 + X3 + X4, data = dat, family = "binomial") |
| 24 |
sflogit_fit <- glm(cbind(y2, w - y2) ~ X1 + X2 + X3 + X4, data = dat, family = "binomial") |
| 25 |
plogit_fit <- glm(yp ~ X1 + X2 + X3 + X4, data = dat, weights = w, family = "quasibinomial") |
| 26 |
|
| 27 |
# obj <- logit_fit |
| 28 |
# y <- dat$y1 |
| 29 |
# type <- "CR2" |
| 30 |
# vcov <- vcovCR(obj, cluster = cluster, type = type) |
| 31 |
# target = NULL |
| 32 |
# inverse_var = FALSE |
| 33 |
# |
| 34 |
# cluster <- droplevels(as.factor(cluster)) |
| 35 |
# B <- sandwich::bread(obj) / v_scale(obj) |
| 36 |
# X_list <- matrix_list(model_matrix(obj), cluster, "row") |
| 37 |
# W_list <- weightMatrix(obj, cluster) |
| 38 |
# XWX <- Reduce("+", Map(function(x, w) t(x) %*% w %*% x, x = X_list, w = W_list)) |
| 39 |
# M <- chol2inv(chol(XWX)) |
| 40 |
# attr(M, "dimnames") <- attr(B, "dimnames") |
| 41 |
# |
| 42 |
# M / B |
| 43 |
# diff(range(M / B)) |
| 44 |
|
| 45 |
|
| 46 |
test_that("bread works", { |
| 47 |
|
| 48 |
expect_true(check_bread(logit_fit, cluster = dat$cluster, check_coef = FALSE, tol = 1.5 * 10^-3)) |
| 49 |
glm_vcov <- bread(logit_fit) * summary(logit_fit)$dispersion / v_scale(logit_fit) |
| 50 |
expect_equal(vcov(logit_fit), glm_vcov) |
| 51 |
|
| 52 |
expect_true(check_bread(sflogit_fit, cluster = dat$cluster, check_coef = FALSE, tol = 1.5 * 10^-3)) |
| 53 |
glm_vcov <- bread(sflogit_fit) * summary(sflogit_fit)$dispersion / v_scale(sflogit_fit) |
| 54 |
expect_equal(vcov(sflogit_fit), glm_vcov) |
| 55 |
|
| 56 |
expect_true(check_bread(plogit_fit, cluster = dat$cluster, check_coef = FALSE, tol = 1.5 * 10^-3)) |
| 57 |
glm_vcov <- bread(plogit_fit) * summary(plogit_fit)$dispersion / v_scale(plogit_fit) |
| 58 |
expect_equal(vcov(plogit_fit), glm_vcov) |
| 59 |
|
| 60 |
}) |
| 61 |
|
| 62 |
test_that("vcovCR options work for CR2", { |
| 63 |
|
| 64 |
CR2_iv <- vcovCR(logit_fit, cluster = dat$cluster, type = "CR2") |
| 65 |
expect_equal(vcovCR(logit_fit, cluster = dat$cluster, type = "CR2", |
| 66 |
inverse_var = TRUE), CR2_iv) |
| 67 |
expect_equal(vcovCR(logit_fit, cluster = dat$cluster, type = "CR2", |
| 68 |
target = targetVariance(logit_fit, cluster = dat$cluster), |
| 69 |
inverse_var = TRUE), CR2_iv) |
| 70 |
attr(CR2_iv, "inverse_var") <- FALSE |
| 71 |
expect_equal(vcovCR(logit_fit, cluster = dat$cluster, type = "CR2", |
| 72 |
target = targetVariance(logit_fit, cluster = dat$cluster), |
| 73 |
inverse_var = FALSE), CR2_iv) |
| 74 |
|
| 75 |
CR2_iv <- vcovCR(sflogit_fit, cluster = dat$cluster, type = "CR2") |
| 76 |
expect_equal(vcovCR(sflogit_fit, cluster = dat$cluster, type = "CR2", |
| 77 |
inverse_var = TRUE), CR2_iv) |
| 78 |
expect_equal(vcovCR(sflogit_fit, cluster = dat$cluster, type = "CR2", |
| 79 |
target = targetVariance(sflogit_fit, cluster = dat$cluster), |
| 80 |
inverse_var = TRUE), CR2_iv) |
| 81 |
attr(CR2_iv, "inverse_var") <- FALSE |
| 82 |
expect_equal(vcovCR(sflogit_fit, cluster = dat$cluster, type = "CR2", |
| 83 |
target = targetVariance(sflogit_fit, cluster = dat$cluster), |
| 84 |
inverse_var = FALSE), CR2_iv) |
| 85 |
|
| 86 |
CR2_iv <- vcovCR(plogit_fit, cluster = dat$cluster, type = "CR2") |
| 87 |
expect_equal(vcovCR(plogit_fit, cluster = dat$cluster, type = "CR2", |
| 88 |
inverse_var = TRUE), CR2_iv) |
| 89 |
expect_equal(vcovCR(plogit_fit, cluster = dat$cluster, type = "CR2", |
| 90 |
target = targetVariance(plogit_fit, cluster = dat$cluster), |
| 91 |
inverse_var = TRUE), CR2_iv) |
| 92 |
attr(CR2_iv, "inverse_var") <- FALSE |
| 93 |
expect_equal(vcovCR(plogit_fit, cluster = dat$cluster, type = "CR2", |
| 94 |
target = targetVariance(plogit_fit, cluster = dat$cluster), |
| 95 |
inverse_var = FALSE), CR2_iv) |
| 96 |
}) |
| 97 |
|
| 98 |
test_that("vcovCR options work for CR4", { |
| 99 |
CR4_iv <- vcovCR(logit_fit, cluster = dat$cluster, type = "CR4") |
| 100 |
expect_equal(vcovCR(logit_fit, cluster = dat$cluster, type = "CR4", |
| 101 |
inverse_var = TRUE), CR4_iv) |
| 102 |
expect_equal(vcovCR(logit_fit, cluster = dat$cluster, type = "CR4", |
| 103 |
target = targetVariance(logit_fit, cluster = dat$cluster), |
| 104 |
inverse_var = TRUE), CR4_iv) |
| 105 |
attr(CR4_iv, "inverse_var") <- FALSE |
| 106 |
expect_equal(vcovCR(logit_fit, cluster = dat$cluster, type = "CR4", |
| 107 |
target = targetVariance(logit_fit, cluster = dat$cluster), |
| 108 |
inverse_var = FALSE), CR4_iv) |
| 109 |
|
| 110 |
CR4_iv <- vcovCR(sflogit_fit, cluster = dat$cluster, type = "CR4") |
| 111 |
expect_equal(vcovCR(sflogit_fit, cluster = dat$cluster, type = "CR4", |
| 112 |
inverse_var = TRUE), CR4_iv) |
| 113 |
expect_equal(vcovCR(sflogit_fit, cluster = dat$cluster, type = "CR4", |
| 114 |
target = targetVariance(sflogit_fit, cluster = dat$cluster), |
| 115 |
inverse_var = TRUE), CR4_iv) |
| 116 |
attr(CR4_iv, "inverse_var") <- FALSE |
| 117 |
expect_equal(vcovCR(sflogit_fit, cluster = dat$cluster, type = "CR4", |
| 118 |
target = targetVariance(sflogit_fit, cluster = dat$cluster), |
| 119 |
inverse_var = FALSE), CR4_iv) |
| 120 |
|
| 121 |
CR4_iv <- vcovCR(plogit_fit, cluster = dat$cluster, type = "CR4") |
| 122 |
expect_equal(vcovCR(plogit_fit, cluster = dat$cluster, type = "CR4", |
| 123 |
inverse_var = TRUE), CR4_iv) |
| 124 |
expect_equal(vcovCR(plogit_fit, cluster = dat$cluster, type = "CR4", |
| 125 |
target = targetVariance(plogit_fit, cluster = dat$cluster), |
| 126 |
inverse_var = TRUE), CR4_iv) |
| 127 |
attr(CR4_iv, "inverse_var") <- FALSE |
| 128 |
expect_equal(vcovCR(plogit_fit, cluster = dat$cluster, type = "CR4", |
| 129 |
target = targetVariance(plogit_fit, cluster = dat$cluster), |
| 130 |
inverse_var = FALSE), CR4_iv) |
| 131 |
|
| 132 |
}) |
| 133 |
|
| 134 |
test_that("CR2 and CR4 are target-unbiased", { |
| 135 |
expect_true(check_CR(logit_fit, vcov = "CR2", cluster = dat$cluster)) |
| 136 |
expect_true(check_CR(sflogit_fit, vcov = "CR2", cluster = dat$cluster)) |
| 137 |
expect_true(check_CR(plogit_fit, vcov = "CR2", cluster = dat$cluster)) |
| 138 |
expect_true(check_CR(logit_fit, vcov = "CR4", cluster = dat$cluster)) |
| 139 |
expect_true(check_CR(sflogit_fit, vcov = "CR4", cluster = dat$cluster)) |
| 140 |
expect_true(check_CR(plogit_fit, vcov = "CR4", cluster = dat$cluster)) |
| 141 |
}) |
| 142 |
|
| 143 |
test_that("vcovCR is equivalent to vcovHC when clusters are all of size 1", { |
| 144 |
library(sandwich, quietly=TRUE) |
| 145 |
|
| 146 |
HC_types <- paste0("HC", 0:3) |
| 147 |
HC_list <- lapply(HC_types, function(t) vcovHC(logit_fit, type = t)) |
| 148 |
|
| 149 |
CR_types <- paste0("CR", 0:3) |
| 150 |
CR_types[2] <- "CR1S" |
| 151 |
CR_list <- lapply(CR_types, function(t) as.matrix(vcovCR(logit_fit, cluster = dat$row, type = t))) |
| 152 |
expect_equal(HC_list, CR_list, tol = 4 * 10^-4) |
| 153 |
|
| 154 |
}) |
| 155 |
|
| 156 |
CR_types <- paste0("CR", 0:4) |
| 157 |
|
| 158 |
test_that("Order doesn't matter.",{ |
| 159 |
check_sort_order(logit_fit, dat = dat, cluster = "cluster") |
| 160 |
}) |
| 161 |
|
| 162 |
|
| 163 |
test_that("clubSandwich works with dropped observations", { |
| 164 |
dat_miss <- dat |
| 165 |
dat_miss$X1[sample.int(n, size = round(n / 10))] <- NA |
| 166 |
logit_dropped <- update(logit_fit, data = dat_miss) |
| 167 |
dat_complete <- subset(dat_miss, !is.na(X1)) |
| 168 |
logit_complete <- update(logit_fit, data = dat_complete) |
| 169 |
|
| 170 |
CR_drop <- lapply(CR_types, function(x) vcovCR(logit_dropped, cluster = dat_miss$cluster, type = x)) |
| 171 |
CR_complete <- lapply(CR_types, function(x) vcovCR(logit_complete, cluster = dat_complete$cluster, type = x)) |
| 172 |
expect_equal(CR_drop, CR_complete) |
| 173 |
|
| 174 |
test_drop <- lapply(CR_types, function(x) coef_test(logit_dropped, vcov = x, cluster = dat_miss$cluster, test = "All", p_values = FALSE)) |
| 175 |
test_complete <- lapply(CR_types, function(x) coef_test(logit_complete, vcov = x, cluster = dat_complete$cluster, test = "All", p_values = FALSE)) |
| 176 |
compare_ttests(test_drop, test_complete) |
| 177 |
}) |
| 178 |
|
| 179 |
|
| 180 |
test_that("clubSandwich works with aliased predictors", { |
| 181 |
data(npk, package = "datasets") |
| 182 |
npk_alias <- glm(yield ~ block + N*P*K, data = npk) |
| 183 |
npk_drop <- glm(yield ~ block + N + P + K + N:P + N:K + P:K, data = npk) |
| 184 |
|
| 185 |
CR_alias <- lapply(CR_types[-4], function(x) vcovCR(npk_alias, cluster = npk$block, type = x)) |
| 186 |
CR_drop <- lapply(CR_types[-4], function(x) vcovCR(npk_drop, cluster = npk$block, type = x)) |
| 187 |
expect_equal(CR_alias, CR_drop) |
| 188 |
|
| 189 |
test_drop <- lapply(CR_types[-4], function(x) coef_test(npk_alias, vcov = x, cluster = npk$block, test = c("z","naive-t","Satterthwaite"), coefs = 7:12, p_values = FALSE)) |
| 190 |
test_complete <- lapply(CR_types[-4], function(x) coef_test(npk_drop, vcov = x, cluster = npk$block, test = c("z","naive-t","Satterthwaite"), coefs = 7:12, p_values = FALSE)) |
| 191 |
expect_equal(test_drop, test_complete) |
| 192 |
}) |
| 193 |
|
| 194 |
|
| 195 |
test_that("clubSandwich results are equivalent to geepack", { |
| 196 |
|
| 197 |
skip_if_not_installed("geepack") |
| 198 |
library(geepack) |
| 199 |
|
| 200 |
# check CR0 with logit |
| 201 |
logit_gee <- geeglm(y1 ~ X1 + X2 + X3 + X4, id = cluster, |
| 202 |
data = dat, family = "binomial") |
| 203 |
logit_refit <- update(logit_fit, start = coef(logit_gee)) |
| 204 |
expect_equal(coef(logit_refit), coef(logit_gee)) |
| 205 |
|
| 206 |
V_gee0 <- summary(logit_gee)$cov.scaled |
| 207 |
V_CR0 <- as.matrix(vcovCR(logit_refit, cluster = dat$cluster, type = "CR0")) |
| 208 |
attr(V_gee0, "dimnames") <- attr(V_CR0, "dimnames") |
| 209 |
expect_equal(V_gee0, V_CR0) |
| 210 |
|
| 211 |
# check CR3 with logit |
| 212 |
logit_gee <- geeglm(y1 ~ X1 + X2 + X3 + X4, id = cluster, |
| 213 |
data = dat, family = "binomial", |
| 214 |
std.err = "jack") |
| 215 |
V_gee3 <- summary(logit_gee)$cov.scaled |
| 216 |
V_CR3 <- as.matrix(vcovCR(logit_refit, cluster = dat$cluster, type = "CR3")) |
| 217 |
attr(V_gee3, "dimnames") <- attr(V_CR3, "dimnames") |
| 218 |
expect_equal(V_gee3 * m / (m - 6), V_CR3) |
| 219 |
|
| 220 |
# check CR0 with plogit |
| 221 |
plogit_gee <- geeglm(yp ~ X1 + X2 + X3 + X4, id = cluster, |
| 222 |
data = dat, weights = w, family = "binomial") |
| 223 |
plogit_refit <- update(plogit_fit, start = coef(plogit_gee)) |
| 224 |
expect_equal(coef(plogit_refit), coef(plogit_gee)) |
| 225 |
|
| 226 |
V_gee0 <- summary(plogit_gee)$cov.scaled |
| 227 |
V_CR0 <- as.matrix(vcovCR(plogit_refit, cluster = dat$cluster, type = "CR0")) |
| 228 |
attr(V_gee0, "dimnames") <- attr(V_CR0, "dimnames") |
| 229 |
expect_equal(V_gee0, V_CR0) |
| 230 |
|
| 231 |
}) |