Load the fisheriris
sample data set.
The species
column vector contains names of three iris flower species: setosa, versicolor, and virginica. The matrix meas
contains of four types of measurements for the flowers: the length and width of sepals and petals in centimeters.
Divide the species and measurement data into training and test data by using the cvpartition
function. Get the indices of the training data rows by using the training
function.
Create training data by using the indices of the training data rows to create a matrix of measurements and a vector of species labels.
Fit a multinomial regression model using the training data.
MnrModel =
Multinomial regression with nominal responses
Value SE tStat pValue
_______ ______ ________ __________
(Intercept_setosa) 86.305 12.541 6.8817 5.9158e-12
x1_setosa -1.0728 3.5795 -0.29971 0.7644
x2_setosa 23.846 3.1238 7.6336 2.2835e-14
x3_setosa -27.289 3.5009 -7.795 6.4409e-15
x4_setosa -59.58 7.0214 -8.4855 2.1472e-17
(Intercept_versicolor) 42.637 5.2214 8.1659 3.1906e-16
x1_versicolor 2.4652 1.1263 2.1887 0.028619
x2_versicolor 6.6808 1.474 4.5325 5.829e-06
x3_versicolor -9.4292 1.2946 -7.2837 3.248e-13
x4_versicolor -18.286 2.0833 -8.7775 1.671e-18
143 observations, 276 error degrees of freedom
Dispersion: 1
Chi^2-statistic vs. constant model: 302.0378, p-value = 1.5168e-60
MnrModel
is a multinomial regression model object that contains the results of fitting a nominal multinomial regression model to the data. By default, virginica
is the reference category. The table output shows coefficient statistics for each predictor in meas
. The small p-values in the column pValue
indicate that all coefficients, at the 95% confidence level, have a statistically significant effect on MnrModel
. For example, the p-value for the term x3_setosa
indicates that the flower petal length has a statistically significant effect on , where and are category probabilities.
Get the indices of the test data rows, by using the test
function. Create test data by using the indices of the test data rows to create a matrix of measurements and a vector of species labels.
Predict the iris species corresponding to the data point in meas
that is not included in the training data.
speciespredict = 7×1 cell
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'versicolor'}
{'versicolor'}
Compare the predicted iris species with the iris species corresponding to the data point in species
that is not included in the training data.
speciestest = 7×1 cell
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'versicolor'}
{'versicolor'}
The output shows that the model accurately predicts the iris species of the data points that not included in the training data.