Skip to content

Commit b3b8bb2

Browse files
authored
Bug fixes for release 0.95 (#461)
1 parent f460ca6 commit b3b8bb2

11 files changed

+364
-338
lines changed

R/commonMachineLearningClassification.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@
455455
plot$dependOn(options = c(.mlClassificationDependencies(options), "decisionBoundary", "pointsShown", "legendShown"))
456456
jaspResults[["decisionBoundary"]] <- plot
457457
if (!ready || length(options[["predictors"]]) < 2) {
458+
if (length(options[["predictors"]]) == 1) {
459+
plot$setError(gettext("Cannot create plot: You need at least two (numeric) features to create the decision boundary matrix. You have currently included only one feature."))
460+
}
458461
return()
459462
}
460463
.classificationFillDecisionBoundary(dataset, options, jaspResults, plot, type)
@@ -520,9 +523,9 @@
520523
x_max <- xBreaks[length(xBreaks)]
521524
y_min <- yBreaks[1]
522525
y_max <- yBreaks[length(yBreaks)]
523-
# Adjust the graining
524-
hs <- min(c(diff(range(xBreaks)), diff(range(yBreaks)))) / 50
525-
grid <- as.data.frame(expand.grid(seq(x_min, x_max, by = hs), seq(y_min, y_max, by = hs)))
526+
xseq <- seq(x_min, x_max, length.out = 100)
527+
yseq <- seq(y_min, y_max, length.out = 100)
528+
grid <- as.data.frame(expand.grid(xseq, yseq))
526529
colnames(grid) <- colnames(predictors)
527530
classificationResult <- jaspResults[["classificationResult"]]$object
528531
if (type == "lda") {

R/commonMachineLearningClustering.R

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@
476476
table$setData(clusterMeans)
477477
}
478478

479-
.mlClusteringPlotDensities <- function(dataset, options, jaspResults, ready, position) {
479+
.mlClusteringPlotDensities <- function(dataset, options, jaspResults, ready, position, type) {
480480
if (!is.null(jaspResults[["clusterDensities"]]) || !options[["clusterDensityPlot"]]) {
481481
return()
482482
}
@@ -488,9 +488,15 @@
488488
return()
489489
}
490490
clusterResult <- jaspResults[["clusterResult"]]$object
491+
predictions <- clusterResult[["pred.values"]]
492+
ncolors <- clusterResult[["clusters"]]
493+
if (type == "densitybased") {
494+
ncolors <- ncolors + 1
495+
predictions[predictions == 0] <- gettext("Noisepoint")
496+
}
497+
clusters <- as.factor(predictions)
491498
if (!options[["clusterDensityPlotSingleFigure"]]) {
492499
for (variable in unlist(options[["predictors"]])) {
493-
clusters <- as.factor(clusterResult[["pred.values"]])
494500
xBreaks <- jaspGraphs::getPrettyAxisBreaks(dataset[[variable]], min.n = 4)
495501
plotData <- data.frame(
496502
cluster = clusters,
@@ -500,7 +506,7 @@
500506
ggplot2::geom_density(mapping = ggplot2::aes(fill = cluster), color = "black", alpha = 0.6) +
501507
ggplot2::scale_x_continuous(name = variable, breaks = xBreaks, limits = range(xBreaks)) +
502508
ggplot2::scale_y_continuous(name = gettext("Density")) +
503-
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(length(levels(clusters)))) +
509+
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) +
504510
jaspGraphs::geom_rangeframe() +
505511
jaspGraphs::themeJaspRaw(legend.position = "right") +
506512
ggplot2::theme(axis.ticks.y = ggplot2::element_blank(), axis.text.y = ggplot2::element_blank())
@@ -509,11 +515,11 @@
509515
}
510516
} else {
511517
dataList <- c(dataset[, options[["predictors"]]])
512-
plotData <- data.frame(value = unlist(dataList), variable = rep(options[["predictors"]], lengths(dataList)), cluster = rep(clusterResult[["pred.values"]], length(options[["predictors"]])))
518+
plotData <- data.frame(value = unlist(dataList), variable = rep(options[["predictors"]], lengths(dataList)), cluster = rep(predictions, length(options[["predictors"]])))
513519
xBreaks <- jaspGraphs::getPrettyAxisBreaks(plotData[["value"]])
514520
p <- ggplot2::ggplot(data = plotData, mapping = ggplot2::aes(x = value, y = factor(variable), height = ..density.., fill = factor(cluster))) +
515521
ggridges::geom_density_ridges(stat = "density", alpha = .6) +
516-
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(length(unique(clusterResult[["pred.values"]])))) +
522+
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) +
517523
ggplot2::scale_x_continuous(name = gettext("Value"), breaks = xBreaks, limits = range(xBreaks)) +
518524
ggplot2::scale_y_discrete(name = gettext("Feature")) +
519525
jaspGraphs::geom_rangeframe(sides = "b") +
@@ -523,7 +529,7 @@
523529
}
524530
}
525531

526-
.mlClusteringPlotMeans <- function(dataset, options, jaspResults, ready, position) {
532+
.mlClusteringPlotMeans <- function(dataset, options, jaspResults, ready, position, type) {
527533
if (!is.null(jaspResults[["clusterMeans"]]) || !options[["clusterMeanPlot"]]) {
528534
return()
529535
}
@@ -536,8 +542,14 @@
536542
}
537543
clusterDataset <- data.frame(dataset[, options[["predictors"]], drop = FALSE])
538544
clusterResult <- jaspResults[["clusterResult"]]$object
545+
predictions <- clusterResult[["pred.values"]]
546+
ncolors <- clusterResult[["clusters"]]
547+
if (type == "densitybased") {
548+
ncolors <- ncolors + 1
549+
predictions[predictions == 0] <- gettext("Noisepoint")
550+
}
551+
clusters <- as.factor(predictions)
539552
if (options[["clusterMeanPlotSingleFigure"]]) {
540-
clusters <- as.factor(clusterResult[["pred.values"]])
541553
xBreaks <- c(1, (as.numeric(levels(clusters)) + 1) * length(options[["predictors"]]))
542554
clusterMeansData <- aggregate(clusterDataset, list(clusters), mean)
543555
clusterSdData <- aggregate(clusterDataset, list(clusters), sd)
@@ -578,14 +590,13 @@
578590
}
579591
p <- p + ggplot2::scale_x_continuous(name = NULL, breaks = xBreaks, labels = xLabels) +
580592
ggplot2::scale_y_continuous(name = gettext("Cluster Mean"), breaks = yBreaks, limits = range(yBreaks)) +
581-
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(length(unique(clusterResult[["pred.values"]])))) +
593+
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) +
582594
jaspGraphs::geom_rangeframe(sides = "l") +
583595
jaspGraphs::themeJaspRaw(legend.position = "right") +
584596
ggplot2::theme(axis.ticks.x = ggplot2::element_blank(), axis.text.x = ggplot2::element_text(angle = 20))
585597
plot[["oneFigure"]] <- createJaspPlot(plot = p, title = gettext("All Features"), height = 400, width = 200 * length(options[["predictors"]]))
586598
} else {
587599
for (variable in unlist(options[["predictors"]])) {
588-
clusters <- as.factor(clusterResult[["pred.values"]])
589600
xBreaks <- as.numeric(levels(clusters))
590601
clusterMeansData <- aggregate(clusterDataset[[variable]], list(clusters), mean)
591602
clusterSdData <- aggregate(clusterDataset[[variable]], list(clusters), sd)
@@ -608,7 +619,7 @@
608619
}
609620
p <- p + ggplot2::scale_x_discrete(name = gettext("Cluster"), breaks = xBreaks) +
610621
ggplot2::scale_y_continuous(name = variable, breaks = yBreaks, limits = range(yBreaks)) +
611-
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(length(unique(clusterResult[["pred.values"]])))) +
622+
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) +
612623
jaspGraphs::geom_rangeframe(sides = "l") +
613624
jaspGraphs::themeJaspRaw() +
614625
ggplot2::theme(axis.ticks.x = ggplot2::element_blank())
@@ -634,7 +645,7 @@
634645
unlist(regmatches(p[[1]], gregexpr("[[:digit:]]+\\.*[[:digit:]]*", p[[1]])))
635646
}
636647

637-
.mlClusteringMatrixPlot <- function(dataset, options, jaspResults, ready, position) {
648+
.mlClusteringMatrixPlot <- function(dataset, options, jaspResults, ready, position, type) {
638649
if (!is.null(jaspResults[["matrixPlot"]]) || !options[["matrixPlot"]]) {
639650
return()
640651
}
@@ -667,17 +678,23 @@
667678
oldFontSize <- jaspGraphs::getGraphOption("fontsize")
668679
jaspGraphs::setGraphOption("fontsize", .85 * oldFontSize)
669680
startProgressbar(length(plotMat) + 1)
681+
ncolors <- clusterResult[["clusters"]]
682+
predictions <- clusterResult[["pred.values"]]
683+
if (type == "densitybased") {
684+
ncolors <- ncolors + 1
685+
predictions[predictions == 0] <- gettext("Noisepoint")
686+
}
670687
for (row in 2:l) {
671688
for (col in 1:(l - 1)) {
672689
if (col < row) {
673690
predictors <- dataset[, variables]
674691
predictors <- predictors[, c(col, row)]
675-
plotData <- data.frame(x = predictors[, 1], y = predictors[, 2], cluster = as.factor(clusterResult[["pred.values"]]))
692+
plotData <- data.frame(x = predictors[, 1], y = predictors[, 2], cluster = as.factor(predictions))
676693
xBreaks <- jaspGraphs::getPrettyAxisBreaks(plotData$x, min.n = 4)
677694
yBreaks <- jaspGraphs::getPrettyAxisBreaks(plotData$y, min.n = 4)
678695
p <- ggplot2::ggplot(data = plotData, mapping = ggplot2::aes(x = x, y = y, fill = cluster)) +
679696
jaspGraphs::geom_point() +
680-
ggplot2::scale_fill_manual(name = NULL, values = .mlColorScheme(clusterResult[["clusters"]])) +
697+
ggplot2::scale_fill_manual(name = NULL, values = .mlColorScheme(ncolors)) +
681698
ggplot2::scale_x_continuous(name = NULL, breaks = xBreaks, limits = range(xBreaks)) +
682699
ggplot2::scale_y_continuous(name = NULL, breaks = yBreaks, limits = range(yBreaks)) +
683700
jaspGraphs::geom_rangeframe() +
@@ -704,21 +721,21 @@
704721
y <- sqrt(lambda1) * sin(theta) * cos(t) + sqrt(lambda2) * cos(theta) * sin(t) + mu_y
705722
ellips <- data.frame(x = x, y = y)
706723
p <- p + ggplot2::geom_path(data = ellips, mapping = ggplot2::aes(x = x, y = y), color = "black", inherit.aes = FALSE, linewidth = 1.5) +
707-
ggplot2::geom_path(data = ellips, mapping = ggplot2::aes(x = x, y = y), color = .mlColorScheme(clusterResult[["clusters"]])[i], inherit.aes = FALSE, linewidth = 0.75)
724+
ggplot2::geom_path(data = ellips, mapping = ggplot2::aes(x = x, y = y), color = .mlColorScheme(ncolors)[i], inherit.aes = FALSE, linewidth = 0.75)
708725
}
709726
}
710727

711728
plotMat[[row - 1, col]] <- p
712729
}
713730
if (l > 2) {
714731
predictors <- dataset[, options[["predictors"]]]
715-
plotData <- data.frame(cluster = as.factor(clusterResult[["pred.values"]]), predictor = predictors[, 1])
732+
plotData <- data.frame(cluster = as.factor(predictions), predictor = predictors[, 1])
716733
p <- ggplot2::ggplot(plotData, ggplot2::aes(y = cluster, x = cluster, show.legend = TRUE)) +
717734
jaspGraphs::geom_point(ggplot2::aes(fill = cluster), alpha = 0) +
718735
ggplot2::xlab(NULL) +
719736
ggplot2::ylab(NULL) +
720737
ggplot2::theme(legend.key = ggplot2::element_blank()) +
721-
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(clusterResult[["clusters"]])) +
738+
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) +
722739
jaspGraphs::geom_rangeframe(sides = "") +
723740
jaspGraphs::themeJaspRaw(legend.position = "left") +
724741
ggplot2::theme(axis.ticks = ggplot2::element_blank(), axis.text.x = ggplot2::element_blank(), axis.text.y = ggplot2::element_blank()) +

R/mlClassificationLda.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,13 @@ mlClassificationLda <- function(jaspResults, dataset, options, ...) {
509509
if (!ready) {
510510
return()
511511
}
512-
result <- mvnormalTest::mardia(dataset[, options[["predictors"]]])
512+
p <- try({
513+
result <- mvnormalTest::mardia(dataset[, options[["predictors"]]])
514+
})
515+
if (isTryError(p)) { # Fail gracefully
516+
table$setError(gettextf("An error occurred when creating this table: %s", jaspBase:::.extractErrorMessage(p)))
517+
return()
518+
}
513519
table[["statistic"]] <- as.numeric(as.character(result[["mv.test"]][1:2, "Statistic"]))
514520
table[["p"]] <- as.numeric(as.character(result[["mv.test"]][1:2, "p-value"]))
515521
}

R/mlClusteringDensityBased.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ mlClusteringDensityBased <- function(jaspResults, dataset, options, ...) {
4646
.mlClusteringPlotTsne(dataset, options, jaspResults, ready, position = 6, type = "densitybased")
4747

4848
# Create the matrix plot
49-
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7)
49+
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7, type = "densitybased")
5050

5151
# Create the cluster means plot
52-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "densitybased")
5353

5454
# Create the cluster densities plot
55-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9, type = "densitybased")
5656
}
5757

5858
.densityBasedClustering <- function(dataset, options, jaspResults) {

R/mlClusteringFuzzyCMeans.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ mlClusteringFuzzyCMeans <- function(jaspResults, dataset, options, ...) {
4646
.mlClusteringPlotTsne(dataset, options, jaspResults, ready, position = 6, type = "cmeans")
4747

4848
# Create the matrix plot
49-
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7)
49+
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7, type = "cmeans")
5050

5151
# Create the cluster means plot
52-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "cmeans")
5353

5454
# Create the cluster densities plot
55-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9, type = "cmeans")
5656
}
5757

5858
.cMeansClustering <- function(dataset, options, jaspResults, ready) {

R/mlClusteringHierarchical.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ mlClusteringHierarchical <- function(jaspResults, dataset, options, ...) {
4646
.mlClusteringPlotTsne(dataset, options, jaspResults, ready, position = 6, type = "hierarchical")
4747

4848
# Create the matrix plot
49-
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7)
49+
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7, type = "hierarchical")
5050

5151
# Create the cluster means plot
52-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "hierarchical")
5353

5454
# Create the cluster densities plot
55-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9, type = "hierarchical")
5656

5757
# Create dendrogram
5858
.mlClusteringHierarchicalDendogram(dataset, options, jaspResults, ready, position = 10)

R/mlClusteringKMeans.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ mlClusteringKMeans <- function(jaspResults, dataset, options, ...) {
4646
.mlClusteringPlotTsne(dataset, options, jaspResults, ready, position = 6, type = "kmeans")
4747

4848
# Create the matrix plot
49-
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7)
49+
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7, type = "kmeans")
5050

5151
# Create the cluster means plot
52-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "kmeans")
5353

5454
# Create the cluster densities plot
55-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9, type = "kmeans")
5656
}
5757

5858
.kMeansClustering <- function(dataset, options, jaspResults, ready) {

R/mlClusteringModelBased.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ mlClusteringModelBased <- function(jaspResults, dataset, options, ...) {
4949
.mlClusteringPlotTsne(dataset, options, jaspResults, ready, position = 7, type = "modelbased")
5050

5151
# Create the matrix plot
52-
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 8, type = "modelbased")
5353

5454
# Create the cluster means plot
55-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9, type = "modelbased")
5656

5757
# Create the cluster densities plot
58-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10)
58+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10, type = "modelbased")
5959
}
6060

6161
emControl <- mclust::emControl

R/mlClusteringRandomForest.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ mlClusteringRandomForest <- function(jaspResults, dataset, options, ...) {
4949
.mlClusteringPlotTsne(dataset, options, jaspResults, ready, position = 7, type = "randomForest")
5050

5151
# Create the matrix plot
52-
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 8, type = "randomForest")
5353

5454
# Create the cluster means plot
55-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9, type = "randomForest")
5656

5757
# Create the cluster densities plot
58-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10)
58+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10, type = "randomForest")
5959
}
6060

6161
.randomForestClustering <- function(dataset, options, jaspResults) {

inst/qml/common/tables/ModelPerformance.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ CheckBox
2424
{
2525
name: "validationMeasures"
2626
text: qsTr("Model performance")
27-
info: qsTr("Displays available model performance metrics. For regression, these metrics include mean squared error (MSE), root mean squared error (RMSE), R<sup>2</sup> and more. For classification, these metrics include precision, recall, the F1-score, support, AUC (area under the ROC curve) and more.")
27+
info: qsTr("Displays available model performance metrics. For regression, these metrics include mean squared error (MSE), root mean squared error (RMSE), R<sup>2</sup> and more. For classification, these metrics include precision, recall, the F1-score, support, AUC (area under the ROC curve) and more. For clustering, these metrics include entropy, Dunn index and more.")
2828
}

0 commit comments

Comments
 (0)