Repository navigation
Expand file tree
/
Copy pathtest_model_weighting.R
More file actions
159 lines (137 loc) · 4.26 KB
/
Copy pathtest_model_weighting.R
File metadata and controls
159 lines (137 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# generate fake data
set.seed(123)
y <- rnorm(50, 0, 1)
sd_sim1 <- abs(rnorm(500, 1.5, 0.1))
sd_sim2 <- abs(rnorm(500, 1.2, 0.1))
sd_sim3 <- abs(rnorm(500, 1, 0.05))
log_lik1 <- log_lik2 <- log_lik3 <- matrix(NA, 500, 50)
for (s in 1:500) {
log_lik1[s, ] <- dnorm(y, -1, sd_sim1[s], log = T)
log_lik2[s, ] <- dnorm(y, 0.7, sd_sim2[s], log = T)
log_lik3[s, ] <- dnorm(y, 1, sd_sim3[s], log = T)
}
ll_list <- list(log_lik1, log_lik2, log_lik3)
r_eff_list <- list(rep(0.9, 50), rep(0.9, 50), rep(0.9, 50))
loo_list <- lapply(1:length(ll_list), function(j) {
loo(ll_list[[j]], r_eff = r_eff_list[[j]])
})
tol <- 0.01 # absolute tolerance of weights
test_that("loo_model_weights throws correct errors and warnings", {
expect_error(
loo_model_weights(log_lik1),
"list of matrices or a list of 'psis_loo' objects"
)
expect_error(loo_model_weights(list(log_lik1)), "At least two models")
expect_error(loo_model_weights(list(loo_list[[1]])), "At least two models")
expect_error(
loo_model_weights(list(log_lik1), method = "pseudobma"),
"At least two models"
)
expect_error(
loo_model_weights(list(log_lik1, log_lik2[-1, ])),
"same dimensions"
)
expect_error(
loo_model_weights(list(log_lik1, log_lik2, log_lik3[, -1])),
"same dimensions"
)
loo_list2 <- loo_list
attr(loo_list2[[3]], "dims") <- c(10, 10)
expect_error(loo_model_weights(loo_list2), "same dimensions")
expect_error(
loo_model_weights(ll_list, r_eff_list = r_eff_list[-1]),
"one component for each model"
)
r_eff_list[[3]] <- rep(0.9, 51)
expect_error(
loo_model_weights(ll_list, r_eff_list = r_eff_list),
"same length as the number of columns"
)
expect_error(
loo_model_weights(list(loo_list[[1]], 2)),
"List elements must all be 'psis_loo' objects or log-likelihood matrices",
fixed = TRUE
)
expect_no_warning(loo_model_weights(ll_list))
})
test_that("loo_model_weights (stacking and pseudo-BMA) gives expected result", {
w1 <- loo_model_weights(ll_list, method = "stacking", r_eff_list = r_eff_list)
expect_type(w1, "double")
expect_s3_class(w1, "stacking_weights")
expect_length(w1, 3)
expect_named(w1, paste0("model", c(1:3)))
expect_snapshot_value(as.numeric(w1), style = "serialize")
expect_snapshot(print(w1))
w1_b <- loo_model_weights(loo_list)
expect_identical(w1, w1_b)
w2 <- loo_model_weights(
ll_list,
r_eff_list = r_eff_list,
method = "pseudobma",
BB = TRUE
)
expect_type(w2, "double")
expect_s3_class(w2, "pseudobma_bb_weights")
expect_length(w2, 3)
expect_named(w2, paste0("model", c(1:3)))
expect_snapshot_value(as.numeric(w2), style = "serialize")
expect_snapshot(print(w2))
w3 <- loo_model_weights(
ll_list,
r_eff_list = r_eff_list,
method = "pseudobma",
BB = FALSE
)
expect_type(w3, "double")
expect_length(w3, 3)
expect_named(w3, paste0("model", c(1:3)))
expect_equal(
as.numeric(w3),
c(5.365279e-05, 9.999436e-01, 2.707028e-06),
tolerance = tol
)
expect_snapshot(print(w3))
w3_b <- loo_model_weights(loo_list, method = "pseudobma", BB = FALSE)
expect_identical(w3, w3_b)
})
test_that("stacking_weights gives expected result", {
lpd_point <- matrix(
c(
-0.2, -0.8, -1.1,
-1.4, -0.3, -0.5,
-0.6, -0.7, -0.1,
-1.0, -1.2, -0.4,
-0.9, -0.5, -0.8
),
ncol = 3,
byrow = TRUE
)
set.seed(0)
actual <- stacking_weights(lpd_point)
expect_s3_class(actual, "stacking_weights")
expect_named(actual, paste0("model", 1:3))
expect_equal(sum(actual), 1)
expect_snapshot_value(as.numeric(actual), style = "deparse")
})
test_that("stacking_weights and pseudobma_weights throw correct errors", {
xx <- cbind(rnorm(10))
expect_error(stacking_weights(xx), "two models are required")
expect_error(pseudobma_weights(xx), "two models are required")
})
test_that("loo_model_weights uses correct names for list of loo objects", {
loo1 <- loo_list[[1]]
loo2 <- loo_list[[2]]
loo3 <- loo_list[[3]]
expect_named(
loo_model_weights(list(loo1, loo2, loo3)),
c("model1", "model2", "model3")
)
expect_named(
loo_model_weights(list("a" = loo1, loo2, "c" = loo3)),
c("a", "model2", "c")
)
expect_named(
loo_model_weights(list(`a` = loo1, `b` = loo2, `c` = loo3)),
c("a", "b", "c")
)
})