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) # Fit multinomial logistic regression model model <- multinom (Species ~ Petal.Length + Petal.Width + Sepal.Length + Sepal.Width, data = iris) # Predict flower species for new data 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.
Multinomial Logistic Regression in R
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.