Skip to content

Commit ddd7783

Browse files
committed
Add test for logistic
1 parent ba3ad28 commit ddd7783

File tree

3 files changed

+144
-3
lines changed

3 files changed

+144
-3
lines changed
Lines changed: 42 additions & 0 deletions
Loading

tests/testthat/_snaps/mlclassificationlogisticmultinomial/data-split.svg renamed to tests/testthat/_snaps/mlclassificationlogisticmultinomial/data-split-2.svg

Lines changed: 1 addition & 1 deletion
Loading

tests/testthat/test-mlclassificationlogisticmultinomial.R

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,105 @@
11
context("Machine Learning Logistic / Multinomial Regression Classification")
22

3-
# Test fixed model #############################################################
3+
# Test logistic regression model ############################################
4+
options <- initMlOptions("mlClassificationLogisticMultinomial")
5+
options$addIndicator <- FALSE
6+
options$addPredictions <- FALSE
7+
options$coefTable <- TRUE
8+
options$coefTableConfInt <- TRUE
9+
options$classProportionsTable <- TRUE
10+
options$holdoutData <- "holdoutManual"
11+
options$modelOptimization <- "manual"
12+
options$modelValid <- "validationManual"
13+
options$predictionsColumn <- ""
14+
options$predictors <- c("x", "y")
15+
options$predictors.types <- rep("scale", 2)
16+
options$saveModel <- FALSE
17+
options$savePath <- ""
18+
options$setSeed <- TRUE
19+
options$target <- "color"
20+
options$target.types <- "nominal"
21+
options$testDataManual <- 0.2
22+
options$testIndicatorColumn <- ""
23+
options$testSetIndicatorVariable <- ""
24+
options$validationDataManual <- 0.2
25+
options$validationMeasures <- TRUE
26+
options$tableShap <- TRUE
27+
options$fromIndex <- 1
28+
options$toIndex <- 5
29+
options$featureImportanceTable <- TRUE
30+
options$seed <- 2
31+
set.seed(1)
32+
results <- jaspTools::runAnalysis("mlClassificationLogisticMultinomial", "spiral.csv", options)
33+
34+
test_that("Class Proportions table results match", {
35+
table <- results[["results"]][["classProportionsTable"]][["data"]]
36+
jaspTools::expect_equal_tables(table,
37+
list(0.5, "Black", 0.575, 0.48125, 0.5, "Red", 0.425, 0.51875))
38+
})
39+
40+
test_that("Model Summary: Logistic Regression Classification table results match", {
41+
table <- results[["results"]][["classificationTable"]][["data"]]
42+
jaspTools::expect_equal_tables(table,
43+
list("Binomial", "Logit", 40, 160, 0.675))
44+
})
45+
46+
test_that("Regression Coefficients table results match", {
47+
table <- results[["results"]][["coefTable"]][["data"]]
48+
jaspTools::expect_equal_tables(table,
49+
list(0.0784909051640228, -0.241691087574101, 0.630847847956443, 0.163341057208965,
50+
0.400211265504598, "(Intercept)", 0.480533838247466, -0.0733280693358763,
51+
-0.389515022216725, 0.647229030486731, 0.160239637348256, 0.241355688318239,
52+
"x", -0.457615047995329, -0.520574613112014, -0.864252780481388,
53+
0.00221841580161717, 0.170160350681228, -0.194437228429846,
54+
"y", -3.0593179376272))
55+
})
56+
57+
test_that("Confusion Matrix table results match", {
58+
table <- results[["results"]][["confusionTable"]][["data"]]
59+
jaspTools::expect_equal_tables(table,
60+
list("Observed", "Black", 14, 9, "", "Red", 4, 13))
61+
})
62+
63+
test_that("Feature Importance Metrics table results match", {
64+
table <- results[["results"]][["featureImportanceTable"]][["data"]]
65+
jaspTools::expect_equal_tables(table,
66+
list(0.49553121577218, "y", 0.367228915662651, "x"))
67+
})
68+
69+
test_that("Data Split plot matches", {
70+
plotName <- results[["results"]][["plotDataSplit"]][["data"]]
71+
testPlot <- results[["state"]][["figures"]][[plotName]][["obj"]]
72+
jaspTools::expect_equal_plots(testPlot, "data-split-1")
73+
})
74+
75+
test_that("Additive Explanations for Predictions of Test Set Cases table results match", {
76+
table <- results[["results"]][["tableShap"]][["data"]]
77+
jaspTools::expect_equal_tables(table,
78+
list(0.518749999999986, 1, "Red (0.511)", -0.0104280206091216, 0.00223442804446772,
79+
0.481250000000014, 2, "Black (0.505)", 0.0113934073181472, 0.0126826432048778,
80+
0.518749999999986, 3, "Red (0.694)", -0.0215827660760237, 0.196894941069013,
81+
0.518749999999986, 4, "Red (0.707)", -0.0148699072618508, 0.203086871619157,
82+
0.481250000000014, 5, "Black (0.584)", 0.00703631455756359,
83+
0.0957186107999987))
84+
})
85+
86+
test_that("Model Performance Metrics table results match", {
87+
table <- results[["results"]][["validationMeasures"]][["data"]]
88+
jaspTools::expect_equal_tables(table,
89+
list(0.675, 0.686700767263427, 0.682926829268293, 0.222222222222222,
90+
0.391304347826087, 0.409090909090909, 0.235294117647059, "Black",
91+
0.371036713180216, 0.590909090909091, 0.777777777777778, 0.608695652173913,
92+
0.45, 23, 0.764705882352941, 0.823529411764706, 0.675, 0.686700767263427,
93+
0.666666666666667, 0.409090909090909, 0.235294117647059, 0.222222222222222,
94+
0.391304347826087, "Red", 0.371036713180216, 0.777777777777778,
95+
0.590909090909091, 0.764705882352941, 0.55, 17, 0.608695652173913,
96+
0.590909090909091, 0.675, 0.686700767263427, 0.676016260162602,
97+
0.315656565656566, 0.313299232736573, 0.315656565656566, 0.313299232736573,
98+
"Average / Total", 0.371036713180216, 0.684343434343434, 0.698358585858586,
99+
0.675, 1, 40, 0.686700767263427, 0.707219251336898))
100+
})
101+
102+
# Test multinomial regression model ############################################
4103
options <- initMlOptions("mlClassificationLogisticMultinomial")
5104
options$addIndicator <- FALSE
6105
options$addPredictions <- FALSE
@@ -88,7 +187,7 @@ test_that("Feature Importance Metrics table results match", {
88187
test_that("Data Split plot matches", {
89188
plotName <- results[["results"]][["plotDataSplit"]][["data"]]
90189
testPlot <- results[["state"]][["figures"]][[plotName]][["obj"]]
91-
jaspTools::expect_equal_plots(testPlot, "data-split")
190+
jaspTools::expect_equal_plots(testPlot, "data-split-2")
92191
})
93192

94193
test_that("Additive Explanations for Predictions of Test Set Cases table results match", {

0 commit comments

Comments
 (0)