![]() The method = "multinom" fits penalized (regularized) multinomial regression models, this ties into the relationship between multinomial regression and neural networks and isn’t something we will concern ourselves with.The argument trace = FALSE is used to suppress superfluous output (try running train without it), but this argument doesn’t exist for some methods.# (entries are percentual average cell counts across resamples) confusionMatrix(fit) # Cross-Validated (5 fold, repeated 10 times) Confusion Matrix # The final value used for the model was decay = 1e-04. # Accuracy was used to select the optimal model using the largest value. # Resampling results across tuning parameters: # Resampling: Cross-Validated (5 fold, repeated 10 times) # 3 classes: 'setosa', 'versicolor', 'virginica' In the example we use the argument method = "multi" to fit a multinomial logistic regression model to the iris dataset.įit.control <- trainControl(method = "repeatedcv", number = 5, repeats = 10)įit <- train(Species ~ Sepal.Length, data = iris, method = "multinom", trControl = fit.control, trace = FALSE) Fitting and evaluating our specified model using the train function.In the example below we use this function to specify that we’d like repeated cross-validation (more specifically 10 repeats of 5-fold cross-validation). Setting up the training control options via the trainControl function.Recall that there are two key steps in this process: We can see that our multinomial logistic regression model that uses Sepal.Length as a predictor achieves approximately 75% accuracy.Ĭross-validating models with nominal categorical outcomes using caret is no different than cross-validating a model with a binary outcome (with the expectation that the “twoClassSummary” option is no longer valid). Sum(pred_cats = iris$Species)/nrow(iris) # Calculate the fraction of correct predictions (ie: accuracy) # 0.7466667 Pred_cats <- levels(iris$Species) # Map the maximums (either 1, 2, or 3) to category labels most_likely <- apply(mfit$fitted.values, 1, which.max) # Applies the which.max function to each row of the matrix of predicted probabilities The code below demonstrates how this can be done using the matrix of predicted probabilities stored as fitted.values in the fitted model from multinom. To generalize this idea to an outcome variable with three categories we’ll need to consider at least two different relative comparisons, for example: :2.500īinary logistic regression modeled the log-odds of a “positive” outcome occurring (relative to that outcome not occurring). Summary(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width ![]() A numeric summary of these data can be found below: data("iris") This dataset contains measurements on 3 different types of iris flowers. To introduce multinomial logistic regression, an extension of binary logistic regression, we’ll use the famous iris dataset.
0 Comments
Leave a Reply. |