Debian Codegraph @

R/coef_test.R

raw
1
2 #---------------------------------------------
3 # Satterthwaite approximation
4 #---------------------------------------------
5
6 Satterthwaite_df <- function(P_array) {
7
8 V_coef <- 2 * apply(P_array, 3, function(x) sum(x^2))
9 E_coef <- apply(P_array, 3, function(x) sum(diag(x)))
10
11 2 * E_coef^2 / V_coef
12 }
13
14 #---------------------------------------------
15 # Saddlepoint approximation
16 #---------------------------------------------
17
18 saddlepoint_pval <- function(t, Q, eps = 1e-10) {
19 if (abs(t) < eps) {
20 return(c(s = NA, p_val = 1))
21 }
22
23 eig <- pmax(0, eigen(Q, symmetric = TRUE, only.values=TRUE)$values)
24 g <- c(1, -t^2 * eig / sum(eig))
25 s_eq <- function(s) sum(g / (1 - 2 * g * s))
26 s_range <- if (t^2 < 1) c(1 / (2 * min(g)), 0) else c(0, 1 / (2 * max(g)))
27 s <- uniroot(s_eq, s_range)$root
28 if (abs(s) > .01) {
29 r <- sign(s) * sqrt(sum(log(1 - 2 * g * s)))
30 q <- s * sqrt(2 * sum(g^2 / (1 - 2 * g * s)^2))
31 p_val <- 1 - pnorm(r) - dnorm(r) * (1 / r - 1 / q)
32 } else {
33 p_val <- 0.5 - sum(g^3) / (3 * sqrt(pi) * sum(g^2)^(3/2))
34 }
35 c(s = s, p_val = p_val)
36 }
37
38 saddlepoint <- function(t_stats, P_array) {
39 saddles <- sapply(1:length(t_stats), function(i) saddlepoint_pval(t = t_stats[i], Q = P_array[,,i]))
40 data.frame(saddlepoint = saddles["s",], p_saddle = saddles["p_val",])
41 }
42
43 #---------------------------------------------
44 # find which coefficients to test
45 #---------------------------------------------
46
47 get_which_coef <- function(beta, coefs) {
48
49 p <- length(beta)
50
51 if (identical(coefs,"All")) return(rep(TRUE, p))
52
53 switch(class(coefs),
54 character = {
55 term_names <- names(beta)
56 if (length(coefs) == 0) stop("You must specify at least one coefficient to test.")
57 if (any(!coefs %in% term_names)) stop("Coefficient names not in model specification.")
58 term_names %in% coefs
59 },
60 logical = {
61 if (sum(coefs) == 0) stop("You must specify at least one coefficient to test.")
62 if (length(coefs) != p) stop(paste0("Coefficient vector must be of length ",p, "."))
63 coefs
64 },
65 numeric = {
66 if (any(!(coefs %in% 1:p))) stop(paste0("Coefficient indices must be less than or equal to ",p,"."))
67 if (length(coefs) == 0) stop("You must specify at least one coefficient to test.")
68 (1:p) %in% coefs
69 },
70 integer = {
71 if (any(!(coefs %in% 1:p))) stop(paste0("Coefficient indices must be less than or equal to ",p,"."))
72 if (length(coefs) == 0) stop("You must specify at least one coefficient to test.")
73 (1:p) %in% coefs
74 }
75 )
76 }
77
78
79 calc_pval <- function(tstat, df, alternative) {
80 switch(
81 alternative,
82 `two-sided` = 2 * pt(abs(tstat), df = df, lower.tail = FALSE),
83 `greater` = pt(tstat, df = df, lower.tail = FALSE),
84 `less` = pt(tstat, df = df, lower.tail = TRUE)
85 )
86 }
87
88 #---------------------------------------------
89 # coeftest for all model coefficients
90 #---------------------------------------------
91
92 #' Test all or selected regression coefficients in a fitted model
93 #'
94 #' \code{coef_test} reports one- or two-sided t-tests for each coefficient
95 #' estimate in a fitted linear regression model, using a sandwich estimator for
96 #' the standard errors and (optionally) a small sample correction for the
97 #' p-value. Available small-sample corrections include Satterthwaite
98 #' approximation or a saddlepoint approximation. Coefficients can be tested
99 #' against non-zero null values by specifying \code{null_constants}.
100 #'
101 #' @param obj Fitted model for which to calculate t-tests.
102 #' @param vcov Variance covariance matrix estimated using \code{vcovCR} or a
103 #' character string specifying which small-sample adjustment should be used to
104 #' calculate the variance-covariance.
105 #' @param test Character vector specifying which small-sample corrections to
106 #' calculate. \code{"z"} returns a z test (i.e., using a standard normal
107 #' reference distribution). \code{"naive-t"} returns a t test with \code{m -
108 #' 1} degrees of freedom, where \code{m} is the number of unique clusters.
109 #' \code{"naive-tp"} returns a t test with \code{m - p} degrees of freedom,
110 #' where \code{p} is the number of regression coefficients in \code{obj}.
111 #' \code{"Satterthwaite"} returns a Satterthwaite correction.
112 #' \code{"saddlepoint"} returns a saddlepoint correction. Default is
113 #' \code{"Satterthwaite"}.
114 #' @param alternative Character string specifying the alternative hypothesis,
115 #' with options "two-sided" (the default), "greater" or "less".
116 #' @param coefs Character, integer, or logical vector specifying which
117 #' coefficients should be tested. The default value \code{"All"} will test all
118 #' estimated coefficients.
119 #' @param null_constants vector of null values for each coefficient to test.
120 #' Must have length equal to the number of coefficients specified in
121 #' \code{coefs}. Default is \code{0}, in which case the null values are taken
122 #' to be zero.
123 #' @param p_values Logical indicating whether to report p-values. The default
124 #' value is \code{TRUE}.
125 #' @param ... Further arguments passed to \code{\link{vcovCR}}, which are only
126 #' needed if \code{vcov} is a character string.
127 #'
128 #' @return A data frame containing estimated regression coefficients, standard
129 #' errors, specified values of null hypotheses, and test results. For the
130 #' Satterthwaite approximation, degrees of freedom and a p-value are reported.
131 #' For the saddlepoint approximation, the saddlepoint and a p-value are
132 #' reported.
133 #'
134 #' @seealso \code{\link{vcovCR}}
135 #'
136 #' @examples
137 #'
138 #' data("ChickWeight", package = "datasets")
139 #' lm_fit <- lm(weight ~ Diet * Time, data = ChickWeight)
140 #' diet_index <- grepl("Diet.:Time", names(coef(lm_fit)))
141 #' coef_test(lm_fit, vcov = "CR2", cluster = ChickWeight$Chick, coefs = diet_index)
142 #'
143 #' V_CR2 <- vcovCR(lm_fit, cluster = ChickWeight$Chick, type = "CR2")
144 #' coef_test(lm_fit, vcov = V_CR2, coefs = diet_index)
145 #'
146 #' # non-inferiority test whether time-by-diet interaction effects are 2 or greater
147 #' coef_test(lm_fit, vcov = V_CR2, coefs = diet_index, null_constants = 2, alternative = "greater")
148 #'
149 #' @export
150
151 coef_test <- function(
152 obj,
153 vcov,
154 test = "Satterthwaite",
155 alternative = c("two-sided", "greater", "less"),
156 coefs = "All",
157 null_constants = 0,
158 p_values = TRUE,
159 ...
160 ) {
161
162 alternative <- match.arg(alternative)
163 beta_full <- coef_CS(obj)
164 beta_NA <- is.na(beta_full)
165 p <- sum(!beta_NA)
166
167 which_beta <- get_which_coef(beta_full, coefs)
168
169 beta <- beta_full[which_beta & !beta_NA]
170
171 if (length(null_constants) == 1L) {
172 null_constants <- rep(null_constants, length.out = length(beta))
173 }
174 if (!is.numeric(null_constants) || length(null_constants) != length(beta)) {
175 stop("null_constants must be a numeric vector with length equal to the number of coefficients to be tested.")
176 }
177
178 if (is.character(vcov)) vcov <- vcovCR(obj, type = vcov, ...)
179 if (!inherits(vcov, "clubSandwich")) stop("Variance-covariance matrix must be a clubSandwich.")
180
181 all_tests <- c("z","naive-t","naive-tp","Satterthwaite","saddlepoint")
182 if (all(test == "All")) test <- all_tests
183 test <- match.arg(test, all_tests, several.ok = TRUE)
184
185 SE <- sqrt(diag(vcov))[which_beta[!beta_NA]]
186
187 if (any(c("Satterthwaite","saddlepoint") %in% test)) {
188 P_array <- get_P_array(get_GH(obj, vcov))[,,which_beta[!beta_NA],drop=FALSE]
189 }
190
191
192 result <- data.frame(Coef = names(beta), beta = as.numeric(beta))
193 result$SE <- SE
194 result$null_value <- null_constants
195 result$tstat <- (beta - null_constants) / SE
196 row.names(result) <- result$Coef
197
198 if ("z" %in% test) {
199 result$df_z <- Inf
200 result$p_z <- calc_pval(result$tstat, df = Inf, alternative = alternative)
201 }
202 if ("naive-t" %in% test) {
203 J <- nlevels(attr(vcov, "cluster"))
204 result$df_t <- J - 1
205 result$p_t <- calc_pval(result$tstat, df = J - 1, alternative = alternative)
206 }
207 if ("naive-tp" %in% test) {
208 J <- nlevels(attr(vcov, "cluster"))
209 result$df_tp <- J - p
210 result$p_tp <- calc_pval(result$tstat, df = J - p, alternative = alternative)
211 }
212 if ("Satterthwaite" %in% test) {
213 result$df_Satt <- Satterthwaite_df(P_array = P_array)
214 result$p_Satt <- calc_pval(result$tstat, df = result$df_Satt, alternative = alternative)
215 }
216 if ("saddlepoint" %in% test) {
217 saddle <- saddlepoint(t_stats = result$tstat, P_array = P_array)
218 result$saddlepoint <- saddle$saddlepoint
219 result$p_saddle <- switch(
220 alternative,
221 `two-sided` = saddle$p_saddle,
222 `greater` = ifelse(result$tstat > 0, saddle$p_saddle / 2, 1 - saddle$p_saddle / 2),
223 `less` = ifelse(result$tstat > 0, 1 - saddle$p_saddle / 2, saddle$p_saddle / 2)
224 )
225 }
226
227 class(result) <- c("coef_test_clubSandwich", class(result))
228 attr(result, "type") <- attr(vcov, "type")
229 attr(result, "alternative") <- alternative
230
231 if (p_values) {
232 result
233 } else {
234 which_vars <- !grepl("p_", names(result))
235 result[which_vars]
236 }
237
238 }
239
240 #---------------------------------------------
241 # print method for coef_test
242 #---------------------------------------------
243
244 #' @export
245
246 print.coef_test_clubSandwich <- function(x, digits = 3, ...) {
247
248 res <- data.frame(
249 `Coef.` = x$Coef,
250 `Estimate` = x$beta,
251 `SE` = x$SE
252 )
253
254 res$`Null value` <- x$null_value
255 res$`t-stat` <- x$tstat
256
257
258 if ("p_z" %in% names(x)) {
259 p_z <- format.pval(x$p_z, digits = digits, eps = 10^-digits)
260 Sig_z <- cut(x$p_z, breaks = c(0, 0.001, 0.01, 0.05, 0.1, 1),
261 labels = c("***", "**", "*", ".", " "), include.lowest = TRUE)
262 res <- cbind(res, "d.f. (z)" = x$df_z,"p-val (z)" = p_z, "Sig." = Sig_z)
263 }
264
265 if ("p_t" %in% names(x)) {
266 p_t <- format.pval(x$p_t, digits = digits, eps = 10^-digits)
267 Sig_t <- cut(x$p_t, breaks = c(0, 0.001, 0.01, 0.05, 0.1, 1),
268 labels = c("***", "**", "*", ".", " "), include.lowest = TRUE)
269 res <- cbind(res, "d.f. (naive-t)" = x$df_t, "p-val (naive-t)" = p_t, "Sig." = Sig_t)
270 }
271
272 if ("p_tp" %in% names(x)) {
273 p_tp <- format.pval(x$p_tp, digits = digits, eps = 10^-digits)
274 Sig_tp <- cut(x$p_tp, breaks = c(0, 0.001, 0.01, 0.05, 0.1, 1),
275 labels = c("***", "**", "*", ".", " "), include.lowest = TRUE)
276 res <- cbind(res, "d.f. (naive-tp)" = x$df_tp, "p-val (naive-tp)" = p_tp, "Sig." = Sig_tp)
277 }
278
279 if ("p_Satt" %in% names(x)) {
280 p_Satt <- format.pval(x$p_Satt, digits = digits, eps = 10^-digits)
281 Sig_Satt <- cut(x$p_Satt, breaks = c(0, 0.001, 0.01, 0.05, 0.1, 1),
282 labels = c("***", "**", "*", ".", " "), include.lowest = TRUE)
283 res <- cbind(res, "d.f. (Satt)" = x$df_Satt, "p-val (Satt)" = p_Satt, "Sig." = Sig_Satt)
284 }
285
286 if ("p_saddle" %in% names(x)) {
287 p_saddle <- format.pval(x$p_saddle, digits = digits, eps = 10^-digits)
288 Sig_saddle <- cut(x$p_saddle, breaks = c(0, 0.001, 0.01, 0.05, 0.1, 1),
289 labels = c("***", "**", "*", ".", " "), include.lowest = TRUE)
290 res <- cbind(res, "s.p." = x$saddlepoint, "p-val (Saddle)" = p_saddle, "Sig." = Sig_saddle)
291 }
292
293 cat("Alternative hypothesis:", attr(x, "alternative"), "\n")
294 print(format(res, digits = 3), row.names = FALSE)
295
296 }