Skip to content

Commit 7cfa89a

Browse files
committed
Use percentage formatting for validation measures
1 parent 628cfba commit 7cfa89a

9 files changed

+38
-31
lines changed

R/commonMachineLearningRegression.R

+22-12
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,14 @@
527527
table <- createJaspTable(title = gettext("Model Performance Metrics"))
528528
table$position <- position
529529
table$dependOn(options = c(.mlRegressionDependencies(options), "validationMeasures"))
530-
table$addColumnInfo(name = "measures", title = "", type = "string")
531-
table$addColumnInfo(name = "values", title = gettext("Value"), type = "string")
532-
measures <- c("MSE", gettext("MSE(scaled)"), "RMSE", "MAE / MAD", "MAPE", "R\u00B2")
533-
table[["measures"]] <- measures
530+
table$addColumnInfo(name = "colTitle", title = "", type = "string")
531+
table$addColumnInfo(name = "mse", title = "MSE", type = "number")
532+
table$addColumnInfo(name = "mse_scaled", title = gettext("MSE(scaled)"), type = "number")
533+
table$addColumnInfo(name = "rmse", title = "RMSE", type = "number")
534+
table$addColumnInfo(name = "mae", title = "MAE / MAD", type = "number")
535+
table$addColumnInfo(name = "mape", title = "MAPE", type = "number", format = "pc")
536+
table$addColumnInfo(name = "r_squared", title = "R\u00B2", type = "number")
537+
table$transpose <- TRUE
534538
jaspResults[["validationMeasures"]] <- table
535539
if (!ready) {
536540
return()
@@ -540,16 +544,22 @@
540544
predDat <- predDat[complete.cases(predDat), ]
541545
obs <- predDat[["obs"]]
542546
pred <- predDat[["pred"]]
543-
mse <- round(regressionResult[["testMSE"]], 3)
547+
mse <- regressionResult[["testMSE"]]
544548
obs_scaled <- (obs - mean(obs)) / sd(obs)
545549
pred_scaled <- (pred - mean(pred)) / sd(pred)
546-
mse_scaled <- round(mean((obs_scaled - pred_scaled)^2), 3)
547-
rmse <- round(sqrt(mse), 3)
548-
mae <- round(mean(abs(obs - pred)), 3)
549-
mape <- paste0(round(mean(abs((obs - pred) / obs)) * 100, 2), "%")
550-
r_squared <- round(cor(obs, pred)^2, 3)
551-
values <- c(mse, mse_scaled, rmse, mae, mape, r_squared)
552-
table[["values"]] <- values
550+
mse_scaled <- mean((obs_scaled - pred_scaled)^2)
551+
rmse <- sqrt(mse)
552+
mae <- mean(abs(obs - pred))
553+
mape <- mean(abs((obs - pred) / obs))
554+
r_squared <- cor(obs, pred)^2
555+
table[["colTitle"]] <- gettext("Values")
556+
table[["mse"]] <- mse
557+
table[["mse_scaled"]] <- mse_scaled
558+
table[["mae"]] <- mae
559+
table[["mape"]] <- mape
560+
table[["rmse"]] <- rmse
561+
table[["mape"]] <- mape
562+
table[["r_squared"]] <- r_squared
553563
if (is.na(r_squared)) {
554564
table$addFootnote(gettextf("R%s cannot be computed due to lack of variance in the predictions.</i>", "\u00B2"))
555565
}

tests/testthat/test-mlregressionboosting.R

+2-5
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ options$predictors <- list("Malic", "Ash", "Alcalinity", "Magnesium", "Phenols",
5353
"Nonflavanoids", "Proanthocyanins", "Color", "Hue", "Dilution",
5454
"Proline")
5555
options$predictors.types <- rep("scale", length(options$predictors))
56-
options$predictors.types <- rep("scale", length(options$predictors))
57-
options$predictors.types <- rep("scale", length(options$predictors))
58-
options$predictors.types <- rep("scale", length(options$predictors))
5956
options$setSeed <- TRUE
6057
options$target <- "Alcohol"
6158
options$target.types <- "scale"
@@ -122,8 +119,8 @@ test_that("Boosting Regression table results match", {
122119
test_that("Evaluation Metrics table results match", {
123120
table <- results[["results"]][["validationMeasures"]][["data"]]
124121
jaspTools::expect_equal_tables(table,
125-
list("MSE", 0.28, "MSE(scaled)", 0.374, "RMSE", 0.529, "MAE / MAD", 0.425, "MAPE", "3.33%",
126-
"R<unicode><unicode>", 0.652))
122+
list("Values", 0.425372298267314, 0.0332803311026787, 0.280491345224988,
123+
0.373676054903572, 0.652325557162448, 0.529614336309911))
127124
})
128125

129126
test_that("Feature Contributions to Predictions for Test Set Cases table results match", {

tests/testthat/test-mlregressiondecisiontree.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,6 @@ test_that("Additive Explanations for Predictions of Test Set Cases table results
107107
test_that("Model Performance Metrics table results match", {
108108
table <- results[["results"]][["validationMeasures"]][["data"]]
109109
jaspTools::expect_equal_tables(table,
110-
list("MSE", 0.18, "MSE(scaled)", 0.349, "RMSE", 0.424, "MAE / MAD", 0.354, "MAPE", "6.01%",
111-
"R<unicode>", 0.671))
110+
list("Values", 0.354130485527544, 0.0600871978449155, 0.180120741111441,
111+
0.349303919563206, 0.671294371379323, 0.424406339622114))
112112
})

tests/testthat/test-mlregressionknn.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ test_that("K-Nearest Neighbors Regression table results match", {
9595
test_that("Model Performance Metrics table results match", {
9696
table <- results[["results"]][["validationMeasures"]][["data"]]
9797
jaspTools::expect_equal_tables(table,
98-
list("MSE", 0.379, "MSE(scaled)", 0.583, "RMSE", 0.616, "MAE / MAD", 0.512, "MAPE", "3.98%",
99-
"R<unicode>", 0.49))
98+
list("Values", 0.5115, 0.0397883922098345, 0.37930375, 0.582791945895268,
99+
0.490046981289794, 0.615876408056032))
100100
})
101101

102102
test_that("Feature Importance Metrics table results match", {

tests/testthat/test-mlregressionlinear.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ test_that("Additive Explanations for Predictions of Test Set Cases table results
7878
test_that("Model Performance Metrics table results match", {
7979
table <- results[["results"]][["validationMeasures"]][["data"]]
8080
jaspTools::expect_equal_tables(table,
81-
list("MSE", 0.079, "MSE(scaled)", 0.023, "RMSE", 0.281, "MAE / MAD", 0.228, "MAPE", "8.08%",
82-
"R<unicode>", 0.976))
81+
list("Values", 0.228083959876927, 0.0808497679064431, 0.0792006506573426,
82+
0.0231629075719563, 0.976181911387651, 0.281426101592128))
8383
})

tests/testthat/test-mlregressionneuralnetwork.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ test_that("Neural Network Regression table results match", {
5454
test_that("Model Performance Metrics table results match", {
5555
table <- results[["results"]][["validationMeasures"]][["data"]]
5656
jaspTools::expect_equal_tables(table,
57-
list("MSE", 0.089, "MSE(scaled)", 0.156, "RMSE", 0.298, "MAE / MAD", 0.243, "MAPE", "4.18%",
58-
"R<unicode>", 0.845))
57+
list("Values", 0.243447601838486, 0.0418476947998267, 0.0887985623673364,
58+
0.156456043905222, 0.844697863114405, 0.297990876315595))
5959
})
6060

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

tests/testthat/test-mlregressionrandomforest.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ test_that("Evaluation Metrics table results match", {
174174
testthat::skip_on_os(c("windows", "linux"))
175175
table <- results[["results"]][["validationMeasures"]][["data"]]
176176
jaspTools::expect_equal_tables(table,
177-
list("MSE", 0.355, "MSE(scaled)", 0.531, "RMSE", 0.596, "MAE / MAD", 0.474, "MAPE", "3.72%",
178-
"R<unicode><unicode>", 0.528))
177+
list("Values", 0.474173308270677, 0.0371687545468009, 0.355223330180166,
178+
0.530948953827784, 0.528118252477076, 0.59600614944828))
179179
})
180180

181181
test_that("Feature Importance Metrics table results match", {

tests/testthat/test-mlregressionregularized.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ test_that("Regularized Linear Regression table results match", {
9797
test_that("Model Performance Metrics table results match", {
9898
table <- results[["results"]][["validationMeasures"]][["data"]]
9999
jaspTools::expect_equal_tables(table,
100-
list("MSE", 0.316, "MSE(scaled)", 0.504, "RMSE", 0.562, "MAE / MAD", 0.428, "MAPE", "3.34%",
101-
"R<unicode>", 0.549))
100+
list("Values", 0.427706057315286, 0.0333852369095772, 0.315722256934242,
101+
0.503930362151054, 0.548524025364545, 0.561891677224571))
102102
})
103103

104104
test_that("Variable Trace Plot matches", {

tests/testthat/test-mlregressionsvm.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,6 @@ test_that("Additive Explanations for Predictions of Test Set Cases table results
198198
test_that("Model Performance Metrics table results match", {
199199
table <- results[["results"]][["validationMeasures"]][["data"]]
200200
jaspTools::expect_equal_tables(table,
201-
list("MSE", 0.079, "MSE(scaled)", 0.149, "RMSE", 0.281, "MAE / MAD", 0.234, "MAPE", "4.05%",
202-
"R<unicode>", 0.852))
201+
list("Values", 0.233880681803325, 0.0405178555867029, 0.0785792724199296,
202+
0.14900759463868, 0.851794443398157, 0.280319946525269))
203203
})

0 commit comments

Comments
 (0)