Debian Codegraph @

R/utilities.R

raw
1 #-----------------------------------------------------
2 # check that bread can be re-constructed from X and W
3 #-----------------------------------------------------
4
5 check_bread <- function(obj, cluster, y, check_coef = TRUE, tol = 10^-6) {
6
7 cluster <- droplevels(as.factor(cluster))
8 B <- sandwich::bread(obj) / v_scale(obj)
9 X_list <- matrix_list(model_matrix(obj), cluster, "row")
10 W_list <- weightMatrix(obj, cluster)
11 XWX <- Reduce("+", Map(function(x, w) t(x) %*% w %*% x, x = X_list, w = W_list))
12 M <- chol2inv(chol(XWX))
13 attr(M, "dimnames") <- attr(B, "dimnames")
14
15 eq_bread <- diff(range((B / M)[abs(B) > 1e-12])) < tol
16
17 if (check_coef) {
18 coef <- coef_CS(obj)
19 y_list <- split(y, cluster)
20 XWy <- Reduce("+", Map(function(x, w, y) t(x) %*% w %*% y, x = X_list, w = W_list, y = y_list))
21 beta <- as.vector(solve(XWX,XWy))
22 names(beta) <- names(coef)
23
24 eq_coef <- all.equal(beta, coef, tol = tol)
25 if (all(c(eq_coef, eq_bread) == TRUE)) TRUE else list(M = M, B = B, beta = beta, coef = coef)
26 } else {
27 if (eq_bread) TRUE else list(M = M, B = B)
28 }
29 }
30
31 #----------------------------------------------
32 # check that CR2 and CR4 are target-unbiased
33 #----------------------------------------------
34
35 check_CR <- function(obj, vcov, ..., tol = .Machine$double.eps^0.5) {
36
37 if (is.character(vcov)) vcov <- vcovCR(obj, type = vcov, ...)
38 if (!("clubSandwich" %in% class(vcov))) stop("Variance-covariance matrix must be a clubSandwich.")
39
40 # calculate E(V^CRj)
41 cluster <- attr(vcov, "cluster")
42 S_array <- get_S_array(obj, vcov)
43 if (dim(S_array)[1] == 1L) {
44 E_CRj <- lapply(1:nlevels(cluster), function(j) crossprod(S_array[1,,j]))
45 } else {
46 E_CRj <- lapply(1:nlevels(cluster), function(j) tcrossprod(S_array[,,j]))
47 }
48
49
50 # calculate target
51 Theta_list <- attr(vcov, "target")
52 X <- model_matrix(obj)
53 alias <- is.na(coef_CS(obj))
54 if (any(alias)) X <- X[, !alias, drop = FALSE]
55 p <- NCOL(X)
56 N <- length(cluster)
57 J <- nlevels(cluster)
58
59 X_list <- matrix_list(X, cluster, "row")
60 W_list <- weightMatrix(obj, cluster)
61 XW_list <- Map(function(x, w) as.matrix(t(x) %*% w), x = X_list, w = W_list)
62 M <- attr(vcov, "bread") / attr(vcov, "v_scale")
63 attr(M, "dimnames") <- NULL
64
65 MXWTWXM <- Map(function(xw, theta) M %*% as.matrix(xw %*% theta %*% t(xw)) %*% M,
66 xw = XW_list, theta = Theta_list)
67 eq <- all.equal(E_CRj, MXWTWXM, tolerance = tol)
68 if (all(eq==TRUE)) TRUE else list(E_CRj = E_CRj, target = MXWTWXM)
69 }
70
71
72 check_sort_order <- function(obj, dat, cluster = NULL, arrange = NULL,
73 CR_types = paste0("CR",0:3),
74 tol = 10^-6, tol2 = tol, tol3 = tol,
75 seed = NULL) {
76
77 if (!is.null(seed)) set.seed(seed)
78
79 re_order <- sample(nrow(dat))
80 dat_scramble <- dat[re_order,]
81 if (!is.null(arrange)) dat_scramble <- dat_scramble[order(dat_scramble[[arrange]]),]
82
83 obj_scramble <- update(obj, data = dat_scramble)
84
85 constraints <- utils::combn(length(coef_CS(obj)), 2, simplify = FALSE)
86 constraint_mats <- lapply(constraints, constrain_zero, coefs = coef_CS(obj))
87
88 if (is.null(cluster)) {
89 CR_fit <- lapply(CR_types, function(x) vcovCR(obj, type = x))
90 CR_scramble <- lapply(CR_types, function(x) vcovCR(obj_scramble, type = x))
91 test_fit <- lapply(CR_types, function(x) coef_test(obj, vcov = x, test = "All", p_values = FALSE))
92 test_scramble <- lapply(CR_types, function(x) coef_test(obj_scramble, vcov = x, test = "All", p_values = FALSE))
93 Wald_fit <- Wald_test(obj, constraints = constraint_mats, vcov = "CR2", test = "All")
94 Wald_scramble <- Wald_test(obj_scramble, constraints = constraint_mats, vcov = "CR2", test = "All")
95
96 } else {
97 CR_fit <- lapply(CR_types, function(x) vcovCR(obj, cluster = dat[[cluster]], type = x))
98 CR_scramble <- lapply(CR_types, function(x) vcovCR(obj_scramble, cluster = dat_scramble[[cluster]], type = x))
99 test_fit <- lapply(CR_types, function(x) coef_test(obj, vcov = x, cluster = dat[[cluster]], test = "All", p_values = FALSE))
100 test_scramble <- lapply(CR_types, function(x) coef_test(obj_scramble, vcov = x, cluster = dat_scramble[[cluster]], test = "All", p_values = FALSE))
101 Wald_fit <- Wald_test(obj, constraints = constraint_mats, vcov = "CR2",
102 cluster = dat[[cluster]], test = "All")
103 Wald_scramble <- Wald_test(obj_scramble, constraints = constraint_mats, vcov = "CR2",
104 cluster = dat_scramble[[cluster]], test = "All")
105
106 }
107
108 testthat::expect_equivalent(CR_fit, CR_scramble, tolerance = tol)
109 compare_ttests(test_fit, test_scramble, tol = tol2)
110 compare_Waldtests(Wald_fit, Wald_scramble, tol = tol3)
111
112 }
113
114 compare_ttests <- function(a, b, tol = 10^-6) {
115
116 if (!inherits(a,"data.frame")) a <- do.call(rbind, a)
117 if (!inherits(b,"data.frame")) b <- do.call(rbind, b)
118
119 testthat::expect_equal(a$beta, b$beta, tolerance = tol)
120 testthat::expect_equal(a$SE, b$SE, tolerance = tol)
121 testthat::expect_equal(a$df, b$df, tolerance = tol)
122 testthat::expect_equal(a$saddlepoint, b$saddlepoint, tolerance = tol)
123 }
124
125 compare_Waldtests <- function(a, b, tol = 10^-4) {
126
127 if (!inherits(a,"data.frame")) a <- do.call(rbind, a)
128 if (!inherits(b,"data.frame")) b <- do.call(rbind, b)
129
130 testthat::expect_equal(a$Fstat, b$Fstat, tolerance = tol)
131 testthat::expect_equal(a$df, b$df, tolerance = tol)
132
133 }
134
135 #---------------------------------------
136 # Testing utility for mmrm objects
137 #---------------------------------------
138
139 compare_mmrm_vcov <- function(obj, tol = 1e-5) {
140
141 ff <- mmrm::component(obj, "full_frame")
142 subject_var <- mmrm::component(obj, "subject_var")
143 cluster <- droplevels(as.factor(ff[[subject_var]]))
144
145 X <- matrix_list(model_matrix(obj), fac = cluster, dim = "row")
146 W <- weightMatrix(obj, cluster = cluster)
147 e <- split(residuals_CS(obj), cluster)
148 XWe <- do.call(rbind, args = Map(function(Xj, Wj, ej) t(ej) %*% Wj %*% Xj, Xj = X, Wj = W, ej = e))
149
150 # CR0
151 dat <- obj$data
152 mod_CR0 <- update(obj, data = dat, control = mmrm::mmrm_control(vcov = "Empirical"))
153 CR0_scores <- mmrm::component(mod_CR0, "score_per_subject")
154 vcov_CR0 <- vcov(mod_CR0)
155 CR0 <- vcovCR(obj, type = "CR0")
156
157 testthat::expect_equivalent(XWe, CR0_scores)
158 testthat::expect_equal(vcov_CR0, as.matrix(CR0), tol = tol)
159
160 # CR2
161 mod_CR2 <- update(obj, data = dat, control = mmrm::mmrm_control(vcov = "Empirical-Bias-Reduced"))
162 vcov_CR2 <- vcov(mod_CR2)
163 CR4 <- vcovCR(obj, type = "CR4")
164 summary_Satt <- summary(mod_CR2)$coefficients
165 coef_test_Satt <- coef_test(obj, vcov = CR4)
166
167 testthat::expect_equal(vcov_CR2, as.matrix(CR4), tol = tol)
168 testthat::expect_equivalent(summary_Satt[,"Std. Error"], coef_test_Satt$SE, tol = tol)
169 testthat::expect_equivalent(summary_Satt[,"df"], coef_test_Satt$df, tol = tol)
170
171 # CR3
172 mod_CR3 <- update(obj, data = dat, control = mmrm::mmrm_control(vcov = "Empirical-Jackknife"))
173 vcov_CR3 <- vcov(mod_CR3)
174 CR3 <- vcovCR(obj, type = "CR3")
175
176 testthat::expect_equal(vcov_CR3, as.matrix(CR3), tol = tol)
177
178 }