Multinomial Logistic Regression in R
Last Updated :
09 Jun, 2023
In this article, we will learn about Multinomial Logistic Regression which can be used when we have more than two categories in the target column. Let’s first start with a little bit brief explanation about the multinomial logistic regression and after this we will move on to the code implementation part by using different packages which are available in R.
What is Multinomial Logistic Regression?
Multinomial logistic regression is used when we have a categorical dependent variable with more than two categories. In R, we can perform multinomial logistic regression using the multinom() function from the nnet package.
- Multinomial logistic regression is a type of regression analysis used to predict the nominal or categorical dependent variable with two or more levels.
- the multinom() function from the nnet package can be used to perform multinomial logistic regression.
VGAM Package in R for Multinomial Logistic Regression
The VGAM (Vector Generalized Linear and Additive Models) package in R Programming Language provides a suite of functions for fitting a variety of regression models. The vglm() function is one of the most commonly used functions in the package and can be used for multinomial logistic regression.
R
library (VGAM)
data (iris)
iris$Species <- as.factor (iris$Species)
fit <- vglm (Species ~ Sepal.Length
+ Sepal.Width
+ Petal.Length
+ Petal.Width,
data = iris,
family = multinomial)
summary (fit)
|
Output:
Estimate Std. Error z value Pr(>|z|)
(Intercept):1 35.490 22666.953 NA NA
(Intercept):2 42.638 25.708 1.659 0.0972 .
Sepal.Length:1 9.495 6729.217 NA NA
Sepal.Length:2 2.465 2.394 1.030 0.3032
Sepal.Width:1 12.300 3143.611 NA NA
Sepal.Width:2 6.681 4.480 1.491 0.1359
Petal.Length:1 -22.975 4799.227 -0.005 0.9962
Petal.Length:2 -9.429 4.737 NA NA
Petal.Width:1 -33.843 7583.502 NA NA
Petal.Width:2 -18.286 9.743 NA NA
nnet Packgae in R for Multinomial Logistic Regression
We can also define a neural network using nnet package in R and in the neural net we can have more than two output categories by including that many numbers of nodes in the output layer of the neural network.
R
library (nnet)
data (iris)
model <- multinom (Species ~ Petal.Length
+ Petal.Width
+ Sepal.Length
+ Sepal.Width,
data = iris)
new_data <- data.frame (Petal.Length = 1.5,
Petal.Width = 0.3,
Sepal.Length = 4.5,
Sepal.Width = 3.1)
predict (model, newdata = new_data, type = "class" )
|
Output:
# weights: 18 (10 variable)
initial value 164.791843
iter 10 value 16.177348
iter 20 value 7.111438
iter 30 value 6.182999
iter 40 value 5.984028
iter 50 value 5.961278
iter 60 value 5.954900
iter 70 value 5.951851
iter 80 value 5.950343
iter 90 value 5.949904
iter 100 value 5.949867
final value 5.949867
stopped after 100 iterations
setosa
Levels:
'setosa''versicolor''virginica'
The iris dataset contains measurements of petal and sepal length and width for three different species of flowers (setosa, versicolor, and virginica). We fit the multinomial logistic regression model to predict flower species based on the four measurements. We then use the predict function to predict the species of new flowers with the given measurements.
Share your thoughts in the comments
Please Login to comment...