feval
Predict responses of multinomial regression model using one input for each predictor
Since R2023a
Description
returns the predicted response of Ypred
= feval(mdl
,X1,X2,...,Xp
)mdl
to the new input predictors
X1,X2,...,Xp
where p is the number of predictor variables used to fit
mdl
.
Examples
Predict Responses Using One Input for Each Predictor
Load the fisheriris
sample data set.
load fisheriris
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.
n = length(species);
partition = cvpartition(n,'Holdout',0.05);
idx_train = training(partition);
Create training data by using the indices of the training data rows to create a matrix of measurements and a vector of species labels.
meastrain = meas(idx_train,:); speciestrain = species(idx_train,:);
Fit a multinomial regression model using the training data.
MnrModel = fitmnr(meastrain,speciestrain)
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.
idx_test = test(partition); meastest = meas(idx_test,:); speciestest = species(idx_test,:);
Predict the iris species corresponding to the data point in meas
that is not included in the training data.
speciespredict = feval(MnrModel,meastest(:,1),meastest(:,2),meastest(:,3),meastest(:,4))
speciespredict = 7x1 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
speciestest = 7x1 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.
Input Arguments
mdl
— Multinomial regression model object
MultinomialRegression
model object
Multinomial regression model object, specified as a MultinomialRegression
model object created with the fitmnr
function.
X1,X2,...,Xp
— New predictor input values
scalars | vectors
New predictor input values, specified as p scalars or vectors,
where p is the number of predictor variables used to fit
mdl
.
If you pass
X1,X2,...,Xp
as a collection of vectors, then each vector must have the same size. Each row of theXi
corresponds to an observation.If you pass
X1,X2,...,Xp
as a mixture of vectors and scalars,feval
expands each scalar argument into a constant vector of the same size as the vector arguments.
Example: feval(mdl,[0.1; 0.2],[0.3; 0.4])
evaluates the
two-predictor model mdl
at the points p1 = [0.1
0.3]
and p2 = [0.2 0.4]
.
Data Types: single
| double
XNew
— New predictor input values
table | n-by-p matrix
New predictor input values, specified
as a table or an n-by-p matrix, where n
is the number of observations to predict, and p is the number of predictor
variables used to fit mdl
.
If
XNew
is a table, it must contain all the names of the predictors used to fitmdl
. You can find the predictor names in themdl.PredictorNames
property.If
XNew
is a matrix, it must have the same number of columns as the number of estimated coefficients. You can find the number of estimated coefficients in themdl.NumPredictors
property. You can specifyXNew
as a matrix only when all names inmdl.PredictorNames
refer to numeric predictors.
Example: feval(mdl,[6.2 3.4; 5.9 3.0])
evaluates the two-predictor
model mdl
at the points p1 = [6.2 3.4]
and
p2 = [5.9 3.0]
.
Data Types: single
| double
| string
| cell
| categorical
Output Arguments
Ypred
— Predicted response categories for input data
categorical array | character array | logical vector | numeric vector | cell array of character vectors
Predicted response categories, returned as an n-by-1 categorical
or character array, logical or numeric vector, or cell array of character vectors, where
n is the number of observations to predict.
Ypred
has the same data type as
mdl.ClassNames
.
Alternative Functionality
Version History
Introduced in R2023a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)