|
a |
|
b/tests/testthat/test-postestimation.R |
|
|
1 |
test_that("log_lik", |
|
|
2 |
{ |
|
|
3 |
out <- log_lik(hs.gauss) |
|
|
4 |
expect_equal(nrow(out), iters) |
|
|
5 |
expect_equal(ncol(out), N) |
|
|
6 |
|
|
|
7 |
expect_equal(log_lik(hs.binom), |
|
|
8 |
log_lik(hs.binom, newdata=df)) |
|
|
9 |
|
|
|
10 |
expect_equal(log_lik(cv.gauss$fits[[1]]), |
|
|
11 |
log_lik(cv.gauss$fits[[1]], newdata=df[folds == 2, ])) |
|
|
12 |
expect_equal(log_lik(cv.binom$fits[[2]]), |
|
|
13 |
log_lik(cv.binom$fits[[2]], newdata=df[folds == 1, ])) |
|
|
14 |
}) |
|
|
15 |
|
|
|
16 |
test_that("posterior_interval", |
|
|
17 |
{ |
|
|
18 |
expect_error(posterior_interval(hs.gauss, pars=1:3), |
|
|
19 |
"'pars' must be a character vector") |
|
|
20 |
expect_error(posterior_interval(hs.gauss, pars="zzz"), |
|
|
21 |
"No pattern in 'pars' matches parameter names") |
|
|
22 |
expect_error(posterior_interval(hs.gauss, prob=0), |
|
|
23 |
"'prob' must be a single value between 0 and 1") |
|
|
24 |
|
|
|
25 |
out <- posterior_interval(hs.gauss) |
|
|
26 |
expect_is(out, "matrix") |
|
|
27 |
expect_equal(nrow(out), |
|
|
28 |
P + 1 + 1) # intercept and extra factor level for X1 |
|
|
29 |
expect_equal(colnames(out), c("2.5%", "97.5%")) |
|
|
30 |
|
|
|
31 |
out <- posterior_interval(hs.gauss, pars="X1", prob=0.5) |
|
|
32 |
expect_equal(nrow(out), |
|
|
33 |
3) # X1b, X1c, X10 |
|
|
34 |
expect_equal(colnames(out), c("25%", "75%")) |
|
|
35 |
}) |
|
|
36 |
|
|
|
37 |
test_that("posterior_linpred", |
|
|
38 |
{ |
|
|
39 |
expect_equal(posterior_linpred(hs.gauss), |
|
|
40 |
posterior_linpred(hs.gauss, newdata=df)) |
|
|
41 |
expect_equal(posterior_linpred(hs.binom, transform=TRUE), |
|
|
42 |
posterior_linpred(hs.binom, transform=TRUE, newdata=df)) |
|
|
43 |
}) |
|
|
44 |
|
|
|
45 |
test_that("posterior_predict", |
|
|
46 |
{ |
|
|
47 |
expect_error(posterior_predict(hs.gauss, nsamples=0), |
|
|
48 |
"'nsamples' must be a positive integer") |
|
|
49 |
expect_error(posterior_predict(hs.gauss, nsamples=-1), |
|
|
50 |
"'nsamples' must be a positive integer") |
|
|
51 |
expect_error(posterior_predict(hs.gauss, nsamples=1.5), |
|
|
52 |
"'nsamples' must be a positive integer") |
|
|
53 |
expect_equal(posterior_predict(hs.gauss, seed=1), |
|
|
54 |
posterior_predict(hs.gauss, seed=1, newdata=df)) |
|
|
55 |
expect_equal(posterior_predict(hs.binom, seed=1), |
|
|
56 |
posterior_predict(hs.binom, seed=1, newdata=df)) |
|
|
57 |
}) |
|
|
58 |
|
|
|
59 |
test_that("posterior_performance", |
|
|
60 |
{ |
|
|
61 |
expect_error(posterior_performance(x), |
|
|
62 |
"Not an 'hsstan' or 'kfold' object") |
|
|
63 |
expect_error(posterior_performance(cv.nofit), |
|
|
64 |
"No fitted models found, run 'kfold' with store.fits=TRUE") |
|
|
65 |
expect_error(posterior_performance(cv.gauss, sub.idx=letters), |
|
|
66 |
"'sub.idx' must be an integer vector") |
|
|
67 |
expect_error(posterior_performance(cv.gauss, sub.idx=c(1, 2, 3.2)), |
|
|
68 |
"'sub.idx' must be an integer vector") |
|
|
69 |
expect_error(posterior_performance(cv.gauss, sub.idx=matrix(1:9, 3, 3)), |
|
|
70 |
"'sub.idx' must be an integer vector") |
|
|
71 |
expect_error(posterior_performance(cv.gauss, sub.idx=1), |
|
|
72 |
"'sub.idx' must contain at least two elements") |
|
|
73 |
expect_error(posterior_performance(cv.gauss, sub.idx=c(0, 10)), |
|
|
74 |
"'sub.idx' contains out of bounds indices") |
|
|
75 |
expect_error(posterior_performance(cv.gauss, sub.idx=c(1, 5, 1000)), |
|
|
76 |
"'sub.idx' contains out of bounds indices") |
|
|
77 |
expect_error(posterior_performance(cv.gauss, sub.idx=c(1, 2, 1, 4)), |
|
|
78 |
"'sub.idx' contains duplicate indices") |
|
|
79 |
expect_error(posterior_performance(cv.binom, sub.idx=1:2), |
|
|
80 |
"'sub.idx' must contain both outcome classes") |
|
|
81 |
|
|
|
82 |
out <- posterior_performance(cv.gauss) |
|
|
83 |
expect_is(out, |
|
|
84 |
"matrix") |
|
|
85 |
expect_equal(rownames(out), |
|
|
86 |
c("r2", "llk")) |
|
|
87 |
expect_equal(colnames(out), |
|
|
88 |
c("mean", "sd", "2.5%", "97.5%")) |
|
|
89 |
expect_named(attributes(out), |
|
|
90 |
c("dim", "dimnames", "type")) |
|
|
91 |
expect_equal(attributes(out)$type, |
|
|
92 |
"cross-validated") |
|
|
93 |
expect_equivalent(out["r2", ], |
|
|
94 |
c(0.00573753, 0.01640424, 0.00000000, 0.04763979), |
|
|
95 |
tolerance=tol) |
|
|
96 |
expect_equivalent(out["llk", ], |
|
|
97 |
c(-141.8687757, 20.2069346, -194.7961530, -119.0098033), |
|
|
98 |
tolerance=tol) |
|
|
99 |
expect_equal(out, |
|
|
100 |
posterior_performance(cv.gauss, sub.idx=1:N)) |
|
|
101 |
|
|
|
102 |
out <- posterior_performance(cv.gauss, sub.idx=sample(which(df$X1 == "b"))) |
|
|
103 |
expect_named(attributes(out), |
|
|
104 |
c("dim", "dimnames", "type", "subset")) |
|
|
105 |
expect_equal(attributes(out)$subset, |
|
|
106 |
which(df$X1 == "b")) |
|
|
107 |
|
|
|
108 |
out <- posterior_performance(hs.binom, prob=0.89) |
|
|
109 |
expect_equal(rownames(out), |
|
|
110 |
c("auc", "llk")) |
|
|
111 |
expect_equal(colnames(out), |
|
|
112 |
c("mean", "sd", "5.5%", "94.5%")) |
|
|
113 |
expect_equal(attributes(out)$type, |
|
|
114 |
"non cross-validated") |
|
|
115 |
expect_equivalent(out["auc", ], |
|
|
116 |
c(0.64088960, 0.07803343, 0.52711200, 0.77760000), |
|
|
117 |
tolerance=tol) |
|
|
118 |
expect_equivalent(out["llk", ], |
|
|
119 |
c(-33.987399, 2.847330, -38.134992, -28.816855), |
|
|
120 |
tolerance=tol) |
|
|
121 |
|
|
|
122 |
out <- posterior_performance(cv.binom, summary=FALSE) |
|
|
123 |
expect_equal(nrow(out), |
|
|
124 |
nsamples(cv.binom$fits[[1]])) |
|
|
125 |
expect_equal(ncol(out), 2) |
|
|
126 |
expect_equal(attributes(out)$type, |
|
|
127 |
"cross-validated") |
|
|
128 |
expect_equivalent(posterior_summary(out), |
|
|
129 |
posterior_performance(cv.binom)) |
|
|
130 |
}) |
|
|
131 |
|
|
|
132 |
test_that("loo", |
|
|
133 |
{ |
|
|
134 |
out <- loo(hs.gauss) |
|
|
135 |
expect_s3_class(out, "loo") |
|
|
136 |
expect_equivalent(out$estimates[1:2, "Estimate"], |
|
|
137 |
c(-113.489222, 6.700909), |
|
|
138 |
tolerance=tol) |
|
|
139 |
|
|
|
140 |
out <- loo(hs.binom) |
|
|
141 |
expect_equivalent(out$estimates[1:2, "Estimate"], |
|
|
142 |
c(-38.891008, 8.315188), |
|
|
143 |
tolerance=tol) |
|
|
144 |
}) |
|
|
145 |
|
|
|
146 |
test_that("waic", |
|
|
147 |
{ |
|
|
148 |
out <- waic(hs.gauss) |
|
|
149 |
expect_s3_class(out, c("waic", "loo")) |
|
|
150 |
expect_equivalent(out$estimates[1:2, "Estimate"], |
|
|
151 |
c(-113.336708, 6.548394), |
|
|
152 |
tolerance=tol) |
|
|
153 |
|
|
|
154 |
out <- waic(hs.binom) |
|
|
155 |
expect_equivalent(out$estimates[1:2, "Estimate"], |
|
|
156 |
c(-38.561844, 7.986024), |
|
|
157 |
tolerance=tol) |
|
|
158 |
}) |
|
|
159 |
|
|
|
160 |
test_that("bayes_R2", |
|
|
161 |
{ |
|
|
162 |
expect_error(bayes_R2(hs.gauss, prob=1), |
|
|
163 |
"'prob' must be a single value between 0 and 1") |
|
|
164 |
expect_error(bayes_R2(hs.gauss, prob=c(0.2, 0.5)), |
|
|
165 |
"'prob' must be a single value between 0 and 1") |
|
|
166 |
|
|
|
167 |
out <- bayes_R2(hs.gauss) |
|
|
168 |
expect_is(out, "numeric") |
|
|
169 |
expect_named(out, |
|
|
170 |
c("mean", "sd", "2.5%", "97.5%")) |
|
|
171 |
|
|
|
172 |
out <- bayes_R2(hs.binom, summary=FALSE) |
|
|
173 |
expect_is(out, "numeric") |
|
|
174 |
expect_length(out, iters * chains / 2) |
|
|
175 |
}) |
|
|
176 |
|
|
|
177 |
test_that("loo_R2", |
|
|
178 |
{ |
|
|
179 |
out <- loo_R2(hs.gauss, summary=FALSE) |
|
|
180 |
expect_is(out, "numeric") |
|
|
181 |
expect_length(out, iters * chains / 2) |
|
|
182 |
|
|
|
183 |
out <- loo_R2(hs.binom) |
|
|
184 |
expect_is(out, "numeric") |
|
|
185 |
expect_named(out, |
|
|
186 |
c("mean", "sd", "2.5%", "97.5%")) |
|
|
187 |
}) |