Main Content

feval

Predict responses of multinomial regression model using one input for each predictor

Since R2023a

    Description

    example

    Ypred = feval(mdl,X1,X2,...,Xp) returns the predicted response of mdl to the new input predictors X1,X2,...,Xp where p is the number of predictor variables used to fit mdl.

    Ypred = feval(mdl,XNew) uses the data in XNew to predict the response.

    Examples

    collapse all

    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 ln(πsetosaπvirginica), where πsetosa and πvirginica 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

    collapse all

    Multinomial regression model object, specified as a MultinomialRegression model object created with the fitmnr function.

    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 the Xi 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

    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 fit mdl. You can find the predictor names in the mdl.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 the mdl.NumPredictors property. You can specify XNew as a matrix only when all names in mdl.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

    collapse all

    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

    • predict returns the same predictions as feval by using a single input argument containing all predictor variables, rather than multiple input arguments with one input for each predictor variable. predict also gives confidence intervals on predictions.

    • random predicts responses with added noise.

    Version History

    Introduced in R2023a