Skip to content

Commit 712bb5d

Browse files
committed
Formula for multinomial
1 parent d9a3a59 commit 712bb5d

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

R/mlClassificationLogisticMultinomial.R

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,40 @@ mlClassificationLogisticMultinomial <- function(jaspResults, dataset, options, .
217217
table[["upper"]] <- coefs[, "upper"]
218218
}
219219
if (options[["formula"]]) { # TODO FOR MULTINOMIAL
220-
one_cat <- levels(factor(classificationResult[["train"]][[options[["target"]]]]))[2]
221-
if (options[["intercept"]]) {
222-
regform <- paste0("logit(p<sub>", options[["target"]], " = ", one_cat, "</sub>) = ", round(as.numeric(coefs[, 1])[1], 3))
223-
start <- 2
220+
if (classificationResult[["family"]] == "binomial") {
221+
one_cat <- levels(factor(classificationResult[["train"]][[options[["target"]]]]))[2]
222+
if (options[["intercept"]]) {
223+
regform <- paste0("logit(p<sub>", options[["target"]], " = ", one_cat, "</sub>) = ", round(as.numeric(coefs[, 1])[1], 3))
224+
start <- 2
225+
} else {
226+
regform <- paste0("logit(p<sub>", options[["target"]], " = ", one_cat, "</sub>) = ")
227+
start <- 1
228+
}
229+
for (i in start:nrow(coefs)) {
230+
regform <- paste0(regform, if (round(as.numeric(coefs[, 1])[i], 3) < 0) " - " else " + ", abs(round(as.numeric(coefs[, 1])[i], 3)), " x ", rownames(coefs)[i])
231+
}
224232
} else {
225-
regform <- paste0("logit(p<sub>", options[["target"]], " = ", one_cat, "</sub>) = ")
226-
start <- 1
227-
}
228-
for (i in start:nrow(coefs)) {
229-
regform <- paste0(regform, if (round(as.numeric(coefs[, 1])[i], 3) < 0) " - " else " + ", abs(round(as.numeric(coefs[, 1])[i], 3)), " x ", rownames(coefs)[i])
233+
regform <- NULL
234+
nlevs <- nlevels(classificationResult[["train"]][[options[["target"]]]])
235+
baseline_cat <- levels(classificationResult[["train"]][[options[["target"]]]])[nlevs]
236+
for (i in seq_len(nlevs - 1)) {
237+
current_cat <- levels(classificationResult[["train"]][[options[["target"]]]])[i]
238+
if (options[["intercept"]]) {
239+
part <- paste0("log(p<sub>", options[["target"]], " = ", current_cat, "</sub> / p<sub>", options[["target"]], " = ", baseline_cat, "</sub>) = ", round(as.numeric(coefs[, 1])[i], 3))
240+
start <- nlevs - 1 + i
241+
} else {
242+
part <- paste0("log(p<sub>", options[["target"]], " = ", current_cat, "</sub> / p<sub>", options[["target"]], " = ", baseline_cat, "</sub>) = ")
243+
start <- i
244+
}
245+
for (j in seq(start, nrow(coefs), by = nlevs - 1)) {
246+
part <- paste0(part, if (round(as.numeric(coefs[, 1])[j], 3) < 0) " - " else " + ", abs(round(as.numeric(coefs[, 1])[j], 3)), " x ", strsplit(rownames(coefs)[j], " : ")[[1]][1])
247+
}
248+
if (i == 1) {
249+
regform <- paste0(regform, part, "\n\n")
250+
} else {
251+
regform <- paste0(regform, part)
252+
}
253+
}
230254
}
231255
formula <- createJaspHtml(gettextf("<b>Regression equation:</b>\n%1$s", regform), "p")
232256
formula$position <- position + 1

0 commit comments

Comments
 (0)