I am trying to make a classification network where I need to predict a categorical/discrete outcome based on a large number of continuous predictor variables. I can predict the class after learning the model, but is there a way to extract the probabilities associated with each predicted class?
Usually when the trained model consists entirely of discrete variables, you can pass pred = TRUE
to predict()
function, but this is not the case with a mixed model. I can't seem to find any helpful documentation on how to accomplish this (or something similar or comparable) with a mixed network.
library(bnlearn)
# Create learning data ----
train <- data.frame(matrix(runif(13600), ncol = 136, nrow = 100))
train <- cbind(train, data.frame(class = rep(c("one","two","three","four"), 25)))
# Create test data ----
test <- data.frame(matrix(runif(1360), ncol = 136, nrow = 16))
test <- cbind(test, data.frame(class = rep(c("one","two","three","four"), 4)))
# m$class = rep(c("one","two","three","four"), 4)
# Create network
train_network <- mmhc(train)
train_fit = bn.fit(train_network, train)
# Make predictions
pred <- predict(train_fit, "class", method = "bayes-lw", data = test)
pred_df <- cbind(pred, data.frame(actual = test[, "class"]))
head(pred_df)
pred actual
1 one one
2 one two
3 two three
4 one four
5 three one
6 four two
Ideally, I would be able to get the predicted probabilities for each observation, e.g.,
pred actual prob
1 one one 0.67
2 one two 0.44
3 two three 0.32
4 one four 0.10
5 three one 0.11
6 four two 0.55