-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_factory.R
136 lines (118 loc) · 3.48 KB
/
test_factory.R
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
context("Factorys of 'compboost'")
test_that("polynomial factory works", {
# Data X and response y:
X.linear = 1:10
X.cubic = X.linear^3
set.seed(pi)
X.test = as.matrix(runif(200))
y = 3 * X.linear + rnorm(10, 0, 2)
# Create and train test baselearner:
linear.factory = PolynomialFactory$new(as.matrix(X.linear), "my_variable_name", 1)
linear.factory$testTrain(y)
cubic.factory = PolynomialFactory$new(as.matrix(X.linear), "my_variable_name", 3)
cubic.factory$testTrain(y)
# lm as benchmark:
mod.linear = lm(y ~ 0 + X.linear)
mod.cubic = lm(y ~ 0 + X.cubic)
# Tests:
# ------
expect_equal(
linear.factory$getData(),
as.matrix(mod.linear$model[["X.linear"]])
)
expect_equal(
linear.factory$testGetParameter(),
as.matrix(unname(mod.linear$coef))
)
expect_equal(
as.numeric(linear.factory$testPredict()),
unname(mod.linear$fitted.values)
)
expect_equal(
as.numeric(linear.factory$testPredictNewdata(X.test)),
unname(predict(mod.linear, data.frame(X.linear = X.test[,1])))
)
expect_equal(
cubic.factory$getData(),
as.matrix(mod.cubic$model[["X.cubic"]])
)
expect_equal(
cubic.factory$testGetParameter(),
as.matrix(unname(mod.cubic$coef))
)
expect_equal(
as.numeric(cubic.factory$testPredict()),
unname(mod.cubic$fitted.values)
)
expect_equal(
as.numeric(cubic.factory$testPredictNewdata(X.test)),
unname(predict(mod.cubic, data.frame(X.cubic = X.test[,1]^3)))
)
})
test_that("custom factory works", {
# Define the custom functions:
instantiateDataFun = function (X) {
return(X)
}
trainFun = function (y, X) {
X = data.frame(y = y, x = as.numeric(X))
return(rpart::rpart(y ~ x, data = X))
}
predictFun = function (model, newdata) {
newdata = data.frame(x = as.numeric(newdata))
return(as.matrix(predict(model, newdata)))
}
extractParameter = function (model) {
return(as.matrix(NA))
}
# Data X and response y:
set.seed(pi)
X = matrix(1:10, ncol = 1)
y = sin(as.numeric(X)) + rnorm(10, 0, 0.6)
X.test = as.matrix(runif(200))
mod.test = trainFun(y, X)
# Create and train test baselearner:
custom.factory = CustomFactory$new(X, "variable_1", instantiateDataFun, trainFun,
predictFun, extractParameter)
custom.factory$testTrain(y)
# Test:
# -----
expect_equal(
custom.factory$getData(),
instantiateDataFun(X)
)
expect_equal(
custom.factory$testGetParameter(),
as.matrix(NA_real_)
)
expect_equal(
as.numeric(custom.factory$testPredict()),
unname(predict(mod.test))
)
expect_equal(
custom.factory$testPredictNewdata(X.test),
predictFun(mod.test, X.test)
)
})
# test_that("custom cpp factory works", {
#
# suppressWarnings(
# Rcpp::sourceCpp("../../external_test_files/custom_cpp_learner.cpp")
# )
#
# set.seed(pi)
# X = matrix(1:10, ncol = 1)
# y = 3 * as.numeric(X) + rnorm(10, 0, 2)
#
# X.test = as.matrix(runif(200))
#
# custom.cpp.factory = CustomCppFactory$new(X, "my_variable_name", dataFunSetter(),
# trainFunSetter(), predictFunSetter())
#
# custom.cpp.factory$testTrain(y)
#
# expect_equal(custom.cpp.factory$getData(), X)
# expect_equal(custom.cpp.factory$testGetParameter(), solve(t(X) %*% X) %*% t(X) %*% y)
# expect_equal(custom.cpp.factory$testPredict(), X %*% solve(t(X) %*% X) %*% t(X) %*% y)
# expect_equal(custom.cpp.factory$testPredictNewdata(X.test), X.test %*% solve(t(X) %*% X) %*% t(X) %*% y)
# })