Main Content

resubPredict

Predict responses for training data using trained regression model

    Description

    example

    yFit = resubPredict(Mdl) returns a vector of predicted responses for the trained regression model Mdl using the predictor data stored in Mdl.X.

    example

    yFit = resubPredict(Mdl,'IncludeInteractions',includeInteractions) specifies whether to include interaction terms in computations. This syntax applies only to generalized additive models.

    Examples

    collapse all

    Train a generalized additive model (GAM), then predict responses for the training data.

    Load the patients data set.

    load patients

    Create a table that contains the predictor variables (Age, Diastolic, Smoker, Weight, Gender, SelfAssessedHealthStatus) and the response variable (Systolic).

    tbl = table(Age,Diastolic,Smoker,Weight,Gender,SelfAssessedHealthStatus,Systolic);

    Train a univariate GAM that contains the linear terms for the predictors in tbl.

    Mdl = fitrgam(tbl,"Systolic")
    Mdl = 
      RegressionGAM
               PredictorNames: {1x6 cell}
                 ResponseName: 'Systolic'
        CategoricalPredictors: [3 5 6]
            ResponseTransform: 'none'
                    Intercept: 122.7800
              NumObservations: 100
    
    
      Properties, Methods
    
    

    Mdl is a RegressionGAM model object.

    Predict responses for the training set.

    yFit = resubPredict(Mdl);

    Create a table containing the observed response values and the predicted response values. Display the first eight rows of the table.

    t = table(tbl.Systolic,yFit, ...
        'VariableNames',{'Observed Value','Predicted Value'});
    head(t)
    ans=8×2 table
        Observed Value    Predicted Value
        ______________    _______________
    
             124              124.75     
             109              109.48     
             125              122.89     
             117              115.87     
             122              121.61     
             121              122.02     
             130              126.39     
             115              115.95     
    
    

    Predict responses for a training data set using a generalized additive model (GAM) that contains both linear and interaction terms for predictors. Specify whether to include interaction terms when predicting responses.

    Load the carbig data set, which contains measurements of cars made in the 1970s and early 1980s.

    load carbig

    Specify Acceleration, Displacement, Horsepower, and Weight as the predictor variables (X) and MPG as the response variable (Y).

    X = [Acceleration,Displacement,Horsepower,Weight];
    Y = MPG;

    Train a generalized additive model that contains all the available linear and interaction terms in X.

    Mdl = fitrgam(X,Y,'Interactions','all');

    Mdl is a RegressionGAM model object.

    Predict the responses using both linear and interaction terms, and then using only linear terms. To exclude interaction terms, specify 'IncludeInteractions',false.

    yFit = resubPredict(Mdl);
    yFit_nointeraction = resubPredict(Mdl,'IncludeInteractions',false);

    Create a table containing the observed response values and the predicted response values. Display the first eight rows of the table.

    t = table(Mdl.Y,yFit,yFit_nointeraction, ...
        'VariableNames',{'Observed Response', ...
        'Predicted Response','Predicted Response Without Interactions'});
    head(t)
    ans=8×3 table
        Observed Response    Predicted Response    Predicted Response Without Interactions
        _________________    __________________    _______________________________________
    
               18                  18.026                           17.22                 
               15                  15.003                          15.791                 
               18                  17.663                           16.18                 
               16                  16.178                          15.536                 
               17                  17.107                          17.361                 
               15                  14.943                          14.424                 
               14                  14.119                          14.981                 
               14                  13.864                          13.498                 
    
    

    Input Arguments

    collapse all

    Regression machine learning model, specified as a full regression model object, as given in the following table of supported models.

    ModelRegression Model Object
    Generalized additive modelRegressionGAM
    Neural network modelRegressionNeuralNetwork

    Flag to include interaction terms of the model, specified as true or false. This argument is valid only for a generalized additive model (GAM). That is, you can specify this argument only when Mdl is RegressionGAM.

    The default value is true if Mdl contains interaction terms. The value must be false if the model does not contain interaction terms.

    Data Types: logical

    Output Arguments

    collapse all

    Predicted responses, returned as a vector of length n, where n is the number of observations in the predictor data (Mdl.X).

    Algorithms

    resubPredict predicts responses according to the corresponding predict function of the object (Mdl). For a model-specific description, see the predict function reference pages in the following table.

    ModelRegression Model Object (Mdl)predict Object Function
    Generalized additive modelRegressionGAMpredict
    Neural network modelRegressionNeuralNetworkpredict

    See Also

    Introduced in R2021a