Skip to content

Commit 663d96a

Browse files
committed
unit test
1 parent f952252 commit 663d96a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

inst/tinytest/test_learner_stratify.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
simdata <- function(n=1000) {
2+
a <- rbinom(n, 1, 0.5)
3+
x <- rnorm(n)
4+
y <- rbinom(n, 1, plogis(-1 + a + a * x))
5+
data.frame(y, a, x)
6+
}
7+
d <- simdata()
8+
9+
test_learner_stratify <- function() {
10+
lr <- learner_stratify(
11+
y ~ x + stratify(a),
12+
learner_glm,
13+
family=binomial()
14+
)
15+
lr$estimate(d)
16+
pr <- lr$predict(d)
17+
18+
d0 <- subset(d, a==0)
19+
d1 <- subset(d, a==1)
20+
g0 <- glm(y ~ x, family=binomial, data=d0)
21+
g1 <- glm(y ~ x, family=binomial, data=d1)
22+
pr0 <- predict(g0, newdata=d, type="response")
23+
pr1 <- predict(g1, newdata=d, type="response")
24+
pr. <- with(d, a * pr1 + (1-a) * pr0)
25+
26+
expect_true(sum(abs(pr-pr.)) == 0)
27+
28+
# works with only single level in 'a'
29+
lr$estimate(d0)
30+
pr <- lr$predict(d0)
31+
pr0 <- predict(g0, newdata=d0, type="response")
32+
expect_true(sum(abs(pr-pr0)) == 0)
33+
34+
# NA when strata was not seen in estimation data
35+
pr <- lr$predict(d)
36+
expect_equal(is.na(pr), d$a==1)
37+
}
38+
test_learner_stratify()

0 commit comments

Comments
 (0)