Debian Codegraph @

tests/testthat/test_conf_int.R

raw
1 context("confidence intervals")
2 set.seed(20190513)
3
4 skip_if_not_installed("nlme")
5
6 library(nlme, quietly=TRUE, warn.conflicts=FALSE)
7
8 data(Ovary, package = "nlme")
9
10 Ovary$time_int <- 1:nrow(Ovary)
11
12 gls_fit <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), data = Ovary,
13 correlation = corAR1(form = ~ time_int | Mare),
14 weights = varPower())
15
16 CRs <- paste0("CR", 0:4)
17
18 test_that("vcov arguments work", {
19 VCR <- lapply(CRs, function(t) vcovCR(gls_fit, type = t))
20 CI_A <- lapply(VCR, function(v) conf_int(gls_fit, vcov = v, level = .98))
21 CI_B <- lapply(CRs, function(t) conf_int(gls_fit, vcov = t, level = .98))
22 expect_equal(CI_A, CI_B)
23 })
24
25 test_that("coefs argument works", {
26 which_grid <- expand.grid(rep(list(c(FALSE,TRUE)), length(coef(gls_fit))))
27 tests_all <- conf_int(gls_fit, vcov = "CR0", coefs = "All")
28
29 CI_A <- apply(which_grid[-1,], 1, function(x) tests_all[x,])
30 CI_B <- apply(which_grid[-1,], 1, function(x) conf_int(gls_fit, vcov = "CR0", coefs = x))
31 expect_equal(CI_A, CI_B)
32 })
33
34 test_that("printing works", {
35 CIs <- conf_int(gls_fit, vcov = "CR0")
36 expect_output(print(CIs))
37
38 CIs <- conf_int(gls_fit, vcov = "CR0", p_values = TRUE)
39 expect_output(x <- print(CIs))
40 expect_true(all(c("p-value","Sig.") %in% names(x)))
41 })
42
43 test_that("level checks work", {
44 expect_error(conf_int(gls_fit, vcov = "CR0", level = -0.01))
45 expect_error(conf_int(gls_fit, vcov = "CR0", level = 95))
46 expect_output(print(conf_int(gls_fit, vcov = "CR0", level = runif(1))))
47 })
48
49 test_that("CI boundaries are ordered", {
50 lev <- runif(1)
51 CI_z <- conf_int(gls_fit, vcov = "CR0", test = "z", level = lev)
52 CI_t <- conf_int(gls_fit, vcov = "CR0", test = "naive-t", level = lev)
53 CI_Satt <- conf_int(gls_fit, vcov = "CR0", test = "Satterthwaite", level = lev)
54 expect_true(all(CI_t$CI_L < CI_z$CI_L))
55 expect_true(all(CI_t$CI_U > CI_z$CI_U))
56 expect_true(all(CI_Satt$CI_L < CI_z$CI_L))
57 expect_true(all(CI_Satt$CI_U > CI_z$CI_U))
58 })
59
60 test_that("conf_int() is consistent with coef_test()", {
61
62 lev <- runif(1)
63 CIs <- lapply(CRs, function(v) conf_int(gls_fit, vcov = v, test = "Satterthwaite", level = lev, p_values = TRUE))
64 ttests <- lapply(CRs, function(v) coef_test(gls_fit, vcov = v, test = "Satterthwaite"))
65 CI_L <- lapply(ttests, function(x) x$beta - x$SE * qt(1 - (1 - lev) / 2, df = x$df))
66 CI_U <- lapply(ttests, function(x) x$beta + x$SE * qt(1 - (1 - lev) / 2, df = x$df))
67 expect_equal(lapply(CIs, function(x) x$CI_L), CI_L)
68 expect_equal(lapply(CIs, function(x) x$CI_U), CI_U)
69 expect_equal(lapply(CIs, function(x) x$p_val), lapply(ttests, function(x) x$p_Satt))
70
71 lev <- runif(1)
72 CIs <- lapply(CRs, function(v) conf_int(gls_fit, vcov = v, test = "naive-t", level = lev, p_values = TRUE))
73 ttests <- lapply(CRs, function(v) coef_test(gls_fit, vcov = v, test = "naive-t"))
74 CI_L <- lapply(ttests, function(x) x$beta - x$SE * qt(1 - (1 - lev) / 2, df = x$df))
75 CI_U <- lapply(ttests, function(x) x$beta + x$SE * qt(1 - (1 - lev) / 2, df = x$df))
76 expect_equal(lapply(CIs, function(x) x$CI_L), CI_L)
77 expect_equal(lapply(CIs, function(x) x$CI_U), CI_U)
78 expect_equal(lapply(CIs, function(x) x$p_val), lapply(ttests, function(x) x$p_t))
79
80 lev <- runif(1)
81 CIs <- lapply(CRs, function(v) conf_int(gls_fit, vcov = v, test = "z", level = lev, p_values = TRUE))
82 ttests <- lapply(CRs, function(v) coef_test(gls_fit, vcov = v, test = "z"))
83 CI_L <- lapply(ttests, function(x) x$beta - x$SE * qt(1 - (1 - lev) / 2, df = x$df))
84 CI_U <- lapply(ttests, function(x) x$beta + x$SE * qt(1 - (1 - lev) / 2, df = x$df))
85 expect_equal(lapply(CIs, function(x) x$CI_L), CI_L)
86 expect_equal(lapply(CIs, function(x) x$CI_U), CI_U)
87 expect_equal(lapply(CIs, function(x) x$p_val), lapply(ttests, function(x) x$p_z))
88
89 })
90
91 test_that("conf_int has informative error messages.", {
92 expect_error(
93 conf_int(gls_fit, vcov = "CR0", test = "all")
94 )
95
96 expect_error(
97 conf_int(gls_fit, vcov = "CR0", test = "saddlepoint")
98 )
99 })