tests/testthat/test-mlclassificationsvm.R

context("Machine Learning SVM Classification")

# Test fixed model #########################################################
options <- initMlOptions("mlClassificationSvm")
options$holdoutData <- "holdoutManual"
options$modelOptimization <- "manual"
options$modelValid <- "validationManual"
options$predictionsColumn <- ""
options$predictors <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
options$predictors.types <- rep("scale", 4)
options$savePath <- ""
options$setSeed <- TRUE
options$target <- "Species"
options$target.types <- "nominal"
options$testIndicatorColumn <- ""
options$testSetIndicatorVariable <- ""
options$dataSplitPlot <- FALSE
options$confusionTable <- FALSE
set.seed(1)
results <- jaspTools::runAnalysis("mlClassificationSvm", "iris.csv", options)

table <- results[["results"]][["classificationTable"]][["data"]]
jaspTools::expect_equal_tables(table,
		list(1, 30, 120, 0.966666666666667, 26))

# Test optimized model #########################################################
options <- initMlOptions("mlClassificationSvm")
options$addIndicator <- FALSE
options$addPredictions <- FALSE
options$classProportionsTable <- TRUE
options$holdoutData <- "holdoutManual"
options$modelOptimization <- "optimized"
options$modelValid <- "validationManual"
options$noOfFolds <- 5
options$predictionsColumn <- ""
options$predictors <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
options$predictors.types <- rep("scale", 4)
options$saveModel <- FALSE
options$savePath <- ""
options$setSeed <- TRUE
options$supportVectorsTable <- TRUE
options$target <- "Species"
options$target.types <- "nominal"
options$testDataManual <- 0.2
options$testIndicatorColumn <- ""
options$testSetIndicatorVariable <- ""
options$validationDataManual <- 0.2
options$validationMeasures <- TRUE
options$tableShap <- TRUE
options$fromIndex <- 1
options$toIndex <- 5
options$featureImportanceTable <- TRUE
options$optimPlot <- TRUE
set.seed(1)
results <- jaspTools::runAnalysis("mlClassificationSvm", "iris.csv", options)

test_that("Class Proportions table results match", {
	table <- results[["results"]][["classProportionsTable"]][["data"]]
	jaspTools::expect_equal_tables(table,
		list(0.333333333333333, "setosa", 0.333333333333333, 0.3125, 0.416666666666667,
			 0.333333333333333, "versicolor", 0.266666666666667, 0.354166666666667,
			 0.333333333333333, 0.333333333333333, "virginica", 0.4, 0.333333333333333,
			 0.25))
})

test_that("Support Vector Machine Classification table results match", {
	table <- results[["results"]][["classificationTable"]][["data"]]
	jaspTools::expect_equal_tables(table,
		list(0.05, 30, 96, 24, 0.933333333333333, 0.958333333333333, 62))
})

test_that("Confusion Matrix table results match", {
	table <- results[["results"]][["confusionTable"]][["data"]]
	jaspTools::expect_equal_tables(table,
		list("Observed", "setosa", 10, 0, 0, "", "versicolor", 0, 8, 0, "",
			 "virginica", 0, 2, 10))
})

test_that("Feature Importance Metrics table results match", {
	table <- results[["results"]][["featureImportanceTable"]][["data"]]
	jaspTools::expect_equal_tables(table,
		list(79.5671523678437, "Petal.Width", 54.4090873450413, "Petal.Length",
			 25.2306693942983, "Sepal.Length", 23.4800081925243, "Sepal.Width"
			))
})

test_that("Classification Accuracy Plot matches", {
	plotName <- results[["results"]][["optimPlot"]][["data"]]
	testPlot <- results[["state"]][["figures"]][[plotName]][["obj"]]
	jaspTools::expect_equal_plots(testPlot, "classification-accuracy-plot")
})

test_that("Data Split plot matches", {
	plotName <- results[["results"]][["plotDataSplit"]][["data"]]
	testPlot <- results[["state"]][["figures"]][[plotName]][["obj"]]
	jaspTools::expect_equal_plots(testPlot, "data-split")
})

test_that("Support Vectors table results match", {
	table <- results[["results"]][["supportVectorsTable"]][["data"]]
	jaspTools::expect_equal_tables(table,
		list(0.42032557786565, 0.132067294448948, -0.173094066392203, -0.590395133155819,
			 2, 0.42032557786565, 0.394452647658781, -0.535383972794484,
			 -0.131538812050262, 3, 1.32668800992096, 0.919223354078446,
			 1.8798820698874, -0.590395133155819, 5, 1.04344974990368, 1.57518673710303,
			 0.551485746412362, 0.786173830160854, 6, 0.250382621855279,
			 0.132067294448948, -0.293857368526296, -0.819823293708598, 7,
			 -1.33575163424152, -1.31105214820513, -1.74301699413542, -0.36096697260304,
			 8, 0.760211489886394, 0.919223354078446, -0.0523307642581081,
			 -0.819823293708598, 11, 0.646916185879479, 0.394452647658781,
			 1.27606555921693, 0.0978893485025173, 12, 1.15674505391059,
			 1.3128013838932, 0.793012350680549, -0.131538812050262, 13,
			 0.193734969851822, -0.261510735365801, -0.0523307642581081,
			 -0.819823293708598, 14, 0.250382621855279, 0.132067294448948,
			 -0.173094066392203, -0.36096697260304, 15, 0.533620881872565,
			 0.263259971053865, 1.39682886135102, 0.327317509055296, 17,
			 0.760211489886394, 0.919223354078446, -0.0523307642581081, -0.819823293708598,
			 18, 0.760211489886394, 0.525645324263697, 0.189195840010079,
			 -0.819823293708598, 20, -1.33575163424152, -1.31105214820513,
			 -1.13920048346495, -0.131538812050262, 22, 0.42032557786565,
			 0.656838000868614, -1.13920048346495, -1.27867961481416, 24,
			 -1.39239928624498, -1.17985947160021, -1.62225369200133, -1.73753593591971,
			 25, -0.259446246175835, -0.261510735365801, -1.13920048346495,
			 -1.50810777536693, 26, 0.137087317848365, 0.132067294448948,
			 0.309959142144174, -0.590395133155819, 27, 0.476973229869108,
			 0.132067294448948, 0.913775652814644, -0.36096697260304, 28,
			 0.250382621855279, 0.000874617844032097, -0.173094066392203,
			 -0.131538812050262, 30, 0.590268533876022, 0.263259971053865,
			 1.15530225708283, -0.590395133155819, 31, 0.363677925862193,
			 0.000874617844032097, -0.414620670660391, -1.04925145426138,
			 32, -0.0328556381620065, -0.261510735365801, -0.414620670660391,
			 -1.50810777536693, 34, -1.33575163424152, -1.17985947160021,
			 -1.25996378559905, -0.131538812050262, 35, 0.703563837882936,
			 1.05041603068336, -0.173094066392203, -1.27867961481416, 36,
			 0.590268533876022, 0.78803067747353, 0.0684325378759855, 0.327317509055296,
			 37, 0.81685914188985, 1.05041603068336, 0.793012350680549, -0.131538812050262,
			 38, 0.0804396658449078, 0.263259971053865, -0.776910577062673,
			 -0.819823293708598, 39, 0.533620881872565, 0.525645324263697,
			 0.551485746412362, 0.556745669608075, 40, 0.307030273858736,
			 0.132067294448948, 0.672249048546456, -0.36096697260304, 44,
			 -1.16580867823115, -0.917474118390382, -0.897673879196766, 0.556745669608075,
			 47, 0.81685914188985, 1.44399406049811, 1.03453895494874, -0.131538812050262,
			 49, 0.930154445896765, 1.44399406049811, 0.430722444278267,
			 0.786173830160854, 52, 0.986802097900222, 0.78803067747353,
			 0.793012350680549, -0.131538812050262, 53, 0.533620881872565,
			 0.263259971053865, 0.309959142144174, -0.36096697260304, 54,
			 1.15674505391059, 0.78803067747353, 1.03453895494874, -1.27867961481416,
			 55, 1.04344974990368, 1.18160870728828, 0.672249048546456, -0.590395133155819,
			 56, 0.42032557786565, 0.394452647658781, -0.293857368526296,
			 -0.131538812050262, 57, -0.146150942168921, -0.261510735365801,
			 -0.173094066392203, -1.04925145426138, 58, -1.22245633023461,
			 -1.31105214820513, -1.01843718133086, -0.131538812050262, 59,
			 0.42032557786565, 0.394452647658781, 0.189195840010079, -0.36096697260304,
			 61, 0.760211489886394, 1.44399406049811, 1.27606555921693, 0.0978893485025173,
			 62, 0.533620881872565, 0.394452647658781, 1.03453895494874,
			 0.0978893485025173, 63, -0.429389202186207, -0.130318058760884,
			 -0.897673879196766, -1.27867961481416, 64, 0.42032557786565,
			 0.394452647658781, 0.430722444278267, -1.96696409647249, 65,
			 -1.22245633023461, -1.31105214820513, -1.25996378559905, 0.0978893485025173,
			 66, 1.21339270591405, 1.18160870728828, 1.51759216348511, -0.131538812050262,
			 67, 0.703563837882936, 0.656838000868614, 1.03453895494874,
			 -0.131538812050262, 68, 1.43998331392788, 0.78803067747353,
			 1.7591187677533, -0.36096697260304, 69, 0.760211489886394, 1.05041603068336,
			 0.793012350680549, 0.327317509055296, 72, 0.590268533876022,
			 0.78803067747353, 0.430722444278267, -0.590395133155819, 73,
			 0.363677925862193, 0.263259971053865, 1.03453895494874, 0.0978893485025173,
			 78, -1.22245633023461, -0.786281441785466, -1.01843718133086,
			 1.01560199071363, 79, 0.590268533876022, 0.78803067747353, 0.189195840010079,
			 -0.131538812050262, 81, 0.986802097900222, 1.18160870728828,
			 1.15530225708283, -0.131538812050262, 85, -0.259446246175835,
			 -0.261510735365801, -1.01843718133086, -1.73753593591971, 87,
			 0.986802097900222, 0.78803067747353, 0.672249048546456, 0.0978893485025173,
			 88, -1.16580867823115, -1.31105214820513, -0.535383972794484,
			 0.786173830160854, 89, 0.760211489886394, 0.78803067747353,
			 0.0684325378759855, -0.131538812050262, 92, 0.476973229869108,
			 0.394452647658781, 0.793012350680549, -0.590395133155819, 93,
			 0.646916185879479, 0.78803067747353, 0.309959142144174, -0.131538812050262,
			 95))
})

test_that("Additive Explanations for Predictions of Test Set Cases table results match", {
	table <- results[["results"]][["tableShap"]][["data"]]
	jaspTools::expect_equal_tables(table,
		list(0.410373798714459, 0.0698375968431656, 0.115827373190014, 0.0712138813014969,
			 0.309488836826618, 1, "setosa (0.977)", 0.39715301669903, 0.0698375968431656,
			 0.028790478616173, 0.164327459017099, 0.309488836826618, 2,
			 "setosa (0.97)", 0.383403212048268, 0.0698375968431656, 0.14407914562592,
			 0.0602698092333088, 0.309488836826618, 3, "setosa (0.967)",
			 0.396844641778602, 0.0392691328516427, 0.0245282726728435, 0.20315927874228,
			 0.309488836826618, 4, "setosa (0.973)", 0.353792711235671, 0.0538946990906603,
			 0.021710489713461, 0.214062578413077, 0.309488836826618, 5,
			 "setosa (0.953)"))
})

test_that("Model Performance Metrics table results match", {
	table <- results[["results"]][["validationMeasures"]][["data"]]
	jaspTools::expect_equal_tables(table,
		list(1, 1, 1, 0, 0, 0, 0, "setosa", 1, 1, 1, 1, 0.333333333333333,
			 10, 1, "<unicode>", 0.933333333333333, 0.539772727272727, 0.888888888888889,
			 0.2, 0, 0, 0.0909090909090909, "versicolor", 0.852802865422442,
			 1, 0.8, 1, 0.333333333333333, 8, 0.909090909090909, 2, 0.933333333333333,
			 0.916666666666667, 0.909090909090909, 0, 0.166666666666667,
			 0.1, 0, "virginica", 0.866025403784439, 0.9, 1, 0.833333333333333,
			 0.333333333333333, 12, 1, 5, 0.955555555555556, 0.818813131313131,
			 0.934006734006734, 0.0666666666666667, 0.0555555555555556, 0.0333333333333333,
			 0.0303030303030303, "Average / Total", 0.906276089735627, 0.966666666666667,
			 0.946666666666667, 0.933333333333333, 1, 30, 0.96969696969697,
			 "<unicode>"))
})
jasp-stats/jaspMachineLearning documentation built on April 5, 2025, 3:52 p.m.