Skip to content

Commit ba3ad28

Browse files
committed
coef table for multinomial
1 parent 5326f95 commit ba3ad28

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

R/mlClassificationLogisticMultinomial.R

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ mlClassificationLogisticMultinomial <- function(jaspResults, dataset, options, .
147147
table$addColumnInfo(name = "var", title = "", type = "string")
148148
table$addColumnInfo(name = "coefs", title = gettextf("Coefficient (%s)", "\u03B2"), type = "number")
149149
table$addColumnInfo(name = "se", title = gettext("Standard Error"), type = "number")
150-
table$addColumnInfo(name = "t", title = gettext("t"), type = "number")
150+
table$addColumnInfo(name = "z", title = gettext("z"), type = "number")
151151
table$addColumnInfo(name = "p", title = gettext("p"), type = "pvalue")
152152
if (options[["coefTableConfInt"]]) {
153153
overtitle <- gettextf("%1$s%% Confidence interval", round(options[["coefTableConfIntLevel"]] * 100, 3))
@@ -169,10 +169,8 @@ mlClassificationLogisticMultinomial <- function(jaspResults, dataset, options, .
169169
classificationResult <- jaspResults[["classificationResult"]]$object
170170
model <- classificationResult[["model"]]
171171
if (classificationResult[["family"]] == "binomial") {
172-
coefs <- summary(model)$coefficients
173-
conf_int <- confint(model, level = options[["coefTableConfIntLevel"]])
174-
coefs <- cbind(coefs, lower = conf_int[, 1], upper = conf_int[, 2])
175-
colnames(coefs) <- c("est", "se", "t", "p", "lower", "upper")
172+
coefs <- cbind(coef(summary(model)), confint(model, level = options[["coefTableConfIntLevel"]]))
173+
colnames(coefs) <- c("est", "se", "z", "p", "lower", "upper")
176174
vars <- rownames(coefs)
177175
for (i in seq_along(vars)) {
178176
if (!(vars[i] %in% options[["predictors"]]) && vars[i] != "(Intercept)") {
@@ -184,8 +182,8 @@ mlClassificationLogisticMultinomial <- function(jaspResults, dataset, options, .
184182
}
185183
rownames(coefs) <- vars
186184
} else {
187-
coefs <- cbind(model$coefficients, confint(model[["original"]], level = options[["coefTableConfIntLevel"]]))
188-
colnames(coefs) <- c("est", "lower", "upper")
185+
coefs <- cbind(VGAM::coef(VGAM::summaryvglm(model[["original"]])), confint(model[["original"]], level = options[["coefTableConfIntLevel"]]))
186+
colnames(coefs) <- c("est", "se", "z", "p", "lower", "upper")
189187
vars <- rownames(coefs)
190188
for (i in seq_along(vars)) {
191189
for (j in c("(Intercept)", options[["predictors"]])) {
@@ -206,16 +204,9 @@ mlClassificationLogisticMultinomial <- function(jaspResults, dataset, options, .
206204
}
207205
table[["var"]] <- rownames(coefs)
208206
table[["coefs"]] <- as.numeric(coefs[, "est"])
209-
if (classificationResult[["family"]] == "binomial") {
210-
table[["se"]] <- as.numeric(coefs[, "se"])
211-
table[["t"]] <- as.numeric(coefs[, "t"])
212-
table[["p"]] <- as.numeric(coefs[, "p"])
213-
} else {
214-
table[["se"]] <- rep(".", nrow(coefs))
215-
table[["t"]] <- rep(".", nrow(coefs))
216-
table[["p"]] <- rep(".", nrow(coefs))
217-
table$addFootnote(gettext("Standard errors, t-values and p-values are not available in multinomial regression."))
218-
}
207+
table[["se"]] <- as.numeric(coefs[, "se"])
208+
table[["z"]] <- as.numeric(coefs[, "z"])
209+
table[["p"]] <- as.numeric(coefs[, "p"])
219210
if (options[["coefTableConfInt"]]) {
220211
table[["lower"]] <- coefs[, "lower"]
221212
table[["upper"]] <- coefs[, "upper"]

tests/testthat/test-mlclassificationlogisticmultinomial.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ context("Machine Learning Logistic / Multinomial Regression Classification")
44
options <- initMlOptions("mlClassificationLogisticMultinomial")
55
options$addIndicator <- FALSE
66
options$addPredictions <- FALSE
7+
options$coefTable <- TRUE
8+
options$coefTableConfInt <- TRUE
79
options$classProportionsTable <- TRUE
810
options$holdoutData <- "holdoutManual"
911
options$modelOptimization <- "manual"
@@ -42,6 +44,32 @@ test_that("Model Summary: Multinomial Regression Classification table results ma
4244
list("Multinomial", "Logit", 30, 120, 1))
4345
})
4446

47+
test_that("Regression Coefficients table results match", {
48+
table <- results[["results"]][["coefTable"]][["data"]]
49+
jaspTools::expect_equal_tables(table,
50+
list(-0.42267961226173, -4222.39134720741, 0.999843438690878, 2154.10522892129,
51+
4221.54598798288, "(Intercept) : setosa", -0.000196220503337896,
52+
17.8212683047785, 0.307905475852188, 0.0461059069011903, 8.93555339132221,
53+
35.3346311337047, "(Intercept) : versicolor", 1.99442245200903,
54+
7.95554619079237, -9967.35637439074, 0.998752813237481, 5089.53837890162,
55+
9983.26746677233, "Sepal.Length : setosa", 0.00156311743787445,
56+
1.89635371704686, -2.10497557567811, 0.352947362657274, 2.04153205073509,
57+
5.89768300977184, "Sepal.Length : versicolor", 0.928887555972511,
58+
4.70317443290632, -2533.61853897752, 0.997102445459767, 1295.08589618604,
59+
2543.02488784333, "Sepal.Width : setosa", 0.00363155405116905,
60+
2.30296713825466, -1.92502652612214, 0.285708816748704, 2.15717926335722,
61+
6.53096080263146, "Sepal.Width : versicolor", 1.0675826424692,
62+
-38.7099474348317, -15129.1258351686, 0.995988491696961, 7699.33325651139,
63+
15051.705940299, "Petal.Length : setosa", -0.00502770124959773,
64+
-14.4906493502573, -31.1295653321292, 0.087838572245474, 8.48939884258976,
65+
2.14826663161466, "Petal.Length : versicolor", -1.7069111275065,
66+
-24.205993975534, -10348.1025945441, 0.996333380037363, 5267.39097350877,
67+
10299.690606593, "Petal.Width : setosa", -0.00459544280978437,
68+
-12.7574125117412, -27.3435062790441, 0.0864846111196134, 7.44202132404277,
69+
1.82868125556169, "Petal.Width : versicolor", -1.7142402522452
70+
))
71+
})
72+
4573
test_that("Confusion Matrix table results match", {
4674
table <- results[["results"]][["confusionTable"]][["data"]]
4775
jaspTools::expect_equal_tables(table,

0 commit comments

Comments
 (0)