| 1 |
context("t-tests") |
| 2 |
set.seed(20190513) |
| 3 |
|
| 4 |
balanced_dat <- function(m, n) { |
| 5 |
cluster <- factor(rep(LETTERS[1:m], each = n)) |
| 6 |
N <- length(cluster) |
| 7 |
m1 <- sample(3:(m-7), size = 1) |
| 8 |
m2 <- sample((m1 + 3):(m-3), size = 1) - m1 |
| 9 |
m3 <- m - m1 - m2 |
| 10 |
c(m1, m2, m3) |
| 11 |
X_btw <- rep(rep(LETTERS[1:3], c(m1, m2, m3)), each = n) |
| 12 |
X_wth <- rep(rep(c(0,1), each = n / 2), m) |
| 13 |
nu <- rnorm(m)[cluster] |
| 14 |
e <- rnorm(n * m) |
| 15 |
y <- nu + e |
| 16 |
data.frame(y, X_btw, X_wth, cluster, row = 1:N) |
| 17 |
} |
| 18 |
|
| 19 |
CRs <- paste0("CR", 0:4) |
| 20 |
|
| 21 |
test_that("vcov arguments work", { |
| 22 |
dat <- balanced_dat(m = 15, n = 8) |
| 23 |
lm_fit <- lm(y ~ X_btw + X_wth, data = dat) |
| 24 |
VCR <- lapply(CRs, function(t) vcovCR(lm_fit, cluster = dat$cluster, type = t)) |
| 25 |
test_A <- lapply(VCR, function(v) coef_test(lm_fit, vcov = v, test = "All", p_values = FALSE)) |
| 26 |
test_B <- lapply(CRs, function(t) coef_test(lm_fit, vcov = t, cluster = dat$cluster, test = "All", p_values = FALSE)) |
| 27 |
test_C <- lapply(CRs, function(t) coef_test(lm_fit, vcov = t, null_constant = 0, cluster = dat$cluster, test = "All", p_values = FALSE)) |
| 28 |
compare_ttests(test_A, test_B) |
| 29 |
compare_ttests(test_A, test_C) |
| 30 |
}) |
| 31 |
|
| 32 |
test_that("get_which_coef() works", { |
| 33 |
f <- 6 |
| 34 |
beta <- 1:f |
| 35 |
beta_names <- letters[1:f] |
| 36 |
names(beta) <- beta_names |
| 37 |
|
| 38 |
which_grid <- as.matrix(expand.grid(rep(list(c(FALSE,TRUE)), f))) |
| 39 |
dimnames(which_grid) <- NULL |
| 40 |
name_list <- apply(which_grid, 1, function(x) beta_names[x]) |
| 41 |
int_list <- apply(which_grid, 1, which) |
| 42 |
|
| 43 |
which_log <- apply(which_grid[-1,], 1, get_which_coef, beta = beta) |
| 44 |
which_char <- sapply(name_list[-1], get_which_coef, beta = beta) |
| 45 |
which_int <- sapply(int_list[-1], get_which_coef, beta = beta) |
| 46 |
|
| 47 |
expect_identical(get_which_coef(beta, coefs = "All"), rep(TRUE, f)) |
| 48 |
expect_error(get_which_coef(beta, coefs = which_grid[1,])) |
| 49 |
expect_error(get_which_coef(beta, coefs = name_list[[1]])) |
| 50 |
expect_error(get_which_coef(beta, coefs = int_list[[1]])) |
| 51 |
|
| 52 |
expect_identical(which_log, which_char) |
| 53 |
expect_identical(which_log, which_int) |
| 54 |
expect_identical(which_char, which_int) |
| 55 |
}) |
| 56 |
|
| 57 |
test_that("coefs argument works.", { |
| 58 |
dat <- balanced_dat(m = 15, n = 8) |
| 59 |
lm_fit <- lm(y ~ X_btw + X_wth, data = dat) |
| 60 |
which_grid <- expand.grid(rep(list(c(FALSE,TRUE)), length(coef(lm_fit)))) |
| 61 |
tests_all <- coef_test(lm_fit, vcov = "CR0", cluster = dat$cluster, test = "All", coefs = "All", p_values = FALSE) |
| 62 |
|
| 63 |
tests_A <- apply(which_grid[-1,], 1, function(x) tests_all[x,]) |
| 64 |
tests_B <- apply(which_grid[-1,], 1, function(x) coef_test(lm_fit, vcov = "CR0", cluster = dat$cluster, test = "All", coefs = x, p_values = FALSE)) |
| 65 |
expect_equal(tests_A, tests_B, check.attributes = FALSE) |
| 66 |
}) |
| 67 |
|
| 68 |
test_that("coefs argument works with null_constants.", { |
| 69 |
dat <- balanced_dat(m = 15, n = 8) |
| 70 |
lm_fit <- lm(y ~ X_btw + X_wth, data = dat) |
| 71 |
which_grid <- expand.grid(rep(list(c(FALSE,TRUE)), length(coef(lm_fit)))) |
| 72 |
|
| 73 |
tests_all <- coef_test(lm_fit, vcov = "CR0", null_constants = 0.1, cluster = dat$cluster, test = "All", coefs = "All", p_values = FALSE) |
| 74 |
tests_A <- apply(which_grid[-1,], 1, function(x) tests_all[x,]) |
| 75 |
tests_B <- apply(which_grid[-1,], 1, function(x) coef_test(lm_fit, vcov = "CR0", cluster = dat$cluster, test = "All", coefs = x, null_constants = 0.1, p_values = FALSE)) |
| 76 |
expect_equal(tests_A, tests_B, check.attributes = FALSE) |
| 77 |
|
| 78 |
null_consts <- rnorm(4) |
| 79 |
tests_all <- coef_test(lm_fit, vcov = "CR0", null_constants = null_consts, cluster = dat$cluster, test = "All", coefs = "All", p_values = FALSE) |
| 80 |
tests_A <- apply(which_grid[-1,], 1, function(x) tests_all[x,]) |
| 81 |
tests_B <- apply(which_grid[-1,], 1, function(x) coef_test(lm_fit, vcov = "CR0", cluster = dat$cluster, test = "All", coefs = x, null_constants = null_consts[x], p_values = FALSE)) |
| 82 |
expect_equal(tests_A, tests_B, check.attributes = FALSE) |
| 83 |
|
| 84 |
}) |
| 85 |
|
| 86 |
test_that("printing works", { |
| 87 |
dat <- balanced_dat(m = 15, n = 8) |
| 88 |
lm_fit <- lm(y ~ X_btw + X_wth, data = dat) |
| 89 |
t_tests <- coef_test(lm_fit, vcov = "CR2", cluster = dat$cluster, test = "All") |
| 90 |
expect_output(x <- print(t_tests)) |
| 91 |
|
| 92 |
expect_equal(t_tests$df_z, rep(Inf, 4L)) |
| 93 |
expect_equal(t_tests$df_t, rep(14L, 4L)) |
| 94 |
expect_true(all(t_tests$df_t >= round(t_tests$df_Satt,1))) |
| 95 |
|
| 96 |
expect_identical(names(x), |
| 97 |
c("Coef.","Estimate","SE","Null value","t-stat", |
| 98 |
"d.f. (z)", "p-val (z)", "Sig.", |
| 99 |
"d.f. (naive-t)", "p-val (naive-t)","Sig.", |
| 100 |
"d.f. (naive-tp)", "p-val (naive-tp)","Sig.", |
| 101 |
"d.f. (Satt)", "p-val (Satt)", "Sig.", |
| 102 |
"s.p.", "p-val (Saddle)", "Sig.")) |
| 103 |
}) |
| 104 |
|
| 105 |
test_that("p-values are ordered", { |
| 106 |
dat <- balanced_dat(m = 15, n = 8) |
| 107 |
lm_fit <- lm(y ~ X_btw + X_wth, data = dat) |
| 108 |
test_results <- lapply(CRs, function(t) coef_test(lm_fit, vcov = t, cluster = dat$cluster, test = "All")) |
| 109 |
test_results <- do.call(rbind, test_results) |
| 110 |
expect_true(with(test_results, all(p_z < p_t))) |
| 111 |
expect_true(with(test_results, all(p_z < p_Satt))) |
| 112 |
}) |
| 113 |
|
| 114 |
test_that("Satterthwaite df work for special cases", { |
| 115 |
m <- sample(12:26, size = 1) |
| 116 |
n <- sample(seq(4,12,2), size = 1) |
| 117 |
dat <- balanced_dat(m, n) |
| 118 |
lm_fit <- lm(y ~ X_btw + X_wth, data = dat) |
| 119 |
t_tests <- coef_test(lm_fit, vcov = "CR2", cluster = dat$cluster, test = "Satterthwaite") |
| 120 |
expect_equal(t_tests$df[4], m - 1) |
| 121 |
mg <- table(dat$X_btw) / n |
| 122 |
df <- apply(cbind(mg[1], mg[-1]), 1, function(x) sum(x)^2 * prod(x - 1) / sum(x^2 * (x - 1))) |
| 123 |
expect_equivalent(t_tests$df[2:3], df) |
| 124 |
lm_fit <- lm(y ~ 0 + cluster + X_wth, data = dat) |
| 125 |
t_tests <- coef_test(lm_fit, vcov = "CR2", cluster = dat$cluster, test = "Satterthwaite") |
| 126 |
expect_equal(t_tests$df[m + 1], m - 1) |
| 127 |
}) |
| 128 |
|
| 129 |
test_that("null_constants error messages appear as appropriate.", { |
| 130 |
dat <- balanced_dat(m = 15, n = 8) |
| 131 |
lm_fit <- lm(y ~ X_btw + X_wth, data = dat) |
| 132 |
vcr <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR0") |
| 133 |
expect_error( |
| 134 |
coef_test(lm_fit, vcov = vcr, null_constants = 1:3) |
| 135 |
) |
| 136 |
expect_error( |
| 137 |
coef_test(lm_fit, vcov = vcr, coefs = 1:2, null_constants = rep(0,3)) |
| 138 |
) |
| 139 |
expect_error( |
| 140 |
coef_test(lm_fit, vcov = vcr, null_constants = "none") |
| 141 |
) |
| 142 |
}) |
| 143 |
|
| 144 |
|
| 145 |
test_that("alternative hypothesis options give coherent results.", { |
| 146 |
dat <- balanced_dat(m = 15, n = 8) |
| 147 |
lm_fit <- lm(y ~ X_btw + X_wth, data = dat) |
| 148 |
vcr <- vcovCR(lm_fit, cluster = dat$cluster, type = "CR0") |
| 149 |
p_two <- coef_test(lm_fit, vcov = vcr, alternative = "two-sided", test = "All") |
| 150 |
p_cols <- grepl("p_",names(p_two)) |
| 151 |
p_two <- as.matrix(p_two[p_cols]) |
| 152 |
p_gt <- as.matrix(coef_test(lm_fit, vcov = vcr, alternative = "greater", test = "All")[p_cols]) |
| 153 |
p_ls <- as.matrix(coef_test(lm_fit, vcov = vcr, alternative = "less", test = "All")[p_cols]) |
| 154 |
expect_equal(p_two, 1 - abs(1 - 2 * p_gt)) |
| 155 |
expect_equal(p_two, 1 - abs(1 - 2 * p_ls)) |
| 156 |
|
| 157 |
null_consts <- median(coef(lm_fit)) |
| 158 |
p_two <- as.matrix(coef_test(lm_fit, vcov = vcr, alternative = "two-sided", null_constants = null_consts, test = "All")[p_cols]) |
| 159 |
p_gt <- as.matrix(coef_test(lm_fit, vcov = vcr, alternative = "greater", null_constants = null_consts, test = "All")[p_cols]) |
| 160 |
p_ls <- as.matrix(coef_test(lm_fit, vcov = vcr, alternative = "less", null_constants = null_consts, test = "All")[p_cols]) |
| 161 |
expect_equal(p_two, 1 - abs(1 - 2 * p_gt)) |
| 162 |
expect_equal(p_two, 1 - abs(1 - 2 * p_ls)) |
| 163 |
|
| 164 |
p_two <- as.matrix(coef_test(lm_fit, vcov = vcr, alternative = "two-sided", null_constants = coef(lm_fit), test = "All")[p_cols]) |
| 165 |
p_gt <- as.matrix(coef_test(lm_fit, vcov = vcr, alternative = "greater", null_constants = coef(lm_fit), test = "All")[p_cols]) |
| 166 |
p_ls <- as.matrix(coef_test(lm_fit, vcov = vcr, alternative = "less", null_constants = coef(lm_fit), test = "All")[p_cols]) |
| 167 |
p_ones <- matrix(1, nrow = nrow(p_two), ncol = ncol(p_two), dimnames = dimnames(p_two)) |
| 168 |
p_halfs <- matrix(0.5, nrow = nrow(p_gt), ncol = ncol(p_gt), dimnames = dimnames(p_gt)) |
| 169 |
expect_equal(p_two, p_ones) |
| 170 |
expect_equal(p_gt, p_halfs) |
| 171 |
expect_equal(p_ls, p_halfs) |
| 172 |
}) |