Main Content

predict

Predict responses using generalized additive model (GAM)

    Description

    example

    yFit = predict(Mdl,X) returns a vector of predicted responses for the predictor data in the table or matrix X, based on the generalized additive model Mdl for regression. The trained model can be either full or compact.

    example

    yFit = predict(Mdl,X,'IncludeInteractions',includeInteractions) specifies whether to include interaction terms in computations.

    Examples

    collapse all

    Train a generalized additive model using training samples, and then predict the test sample responses.

    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);

    Randomly partition observations into a training set and a test set. Specify a 10% holdout sample for testing.

    rng('default') % For reproducibility
    cv = cvpartition(size(tbl,1),'HoldOut',0.10);

    Extract the training and test indices.

    trainInds = training(cv);
    testInds = test(cv);

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

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

    Mdl is a RegressionGAM model object.

    Predict responses for the test set.

    yFit = predict(Mdl,tbl(testInds,:));

    Create a table containing the observed response values and the predicted response values.

    table(tbl.Systolic(testInds),yFit, ...
        'VariableNames',{'Observed Value','Predicted Value'})
    ans=10×2 table
        Observed Value    Predicted Value
        ______________    _______________
    
             124              126.58     
             121              123.95     
             130              116.72     
             115              117.35     
             121              117.45     
             116               118.5     
             123              126.16     
             132              124.14     
             125              127.36     
             124              115.99     
    
    

    Predict responses for new observations using a generalized additive model that contains both linear and interaction terms for predictors. Use a memory-efficient model object, and 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;

    Partition the data set into two sets: one containing training data, and the other containing new, unobserved test data. Reserve 10 observations for the new test data set.

    rng('default')
    n = size(X,1);
    newInds = randsample(n,10);
    inds = ~ismember(1:n,newInds);
    XNew = X(newInds,:);
    YNew = Y(newInds);

    Train a GAM that contains all the available linear and interaction terms in X.

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

    Mdl is a RegressionGAM model object.

    Conserve memory by reducing the size of the trained model.

    CMdl = compact(Mdl);
    whos('Mdl','CMdl')
      Name      Size              Bytes  Class                                          Attributes
    
      CMdl      1x1             1228122  classreg.learning.regr.CompactRegressionGAM              
      Mdl       1x1             1262143  RegressionGAM                                            
    

    CMdl is a CompactRegressionGAM 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 = predict(CMdl,XNew);
    yFit_nointeraction = predict(CMdl,XNew,'IncludeInteractions',false);

    Create a table containing the observed response values and the predicted response values.

    t = table(YNew,yFit,yFit_nointeraction, ...
        'VariableNames',{'Observed Response', ...
        'Predicted Response','Predicted Response Without Interactions'})
    t=10×3 table
        Observed Response    Predicted Response    Predicted Response Without Interactions
        _________________    __________________    _______________________________________
    
              27.9                  23.04                          23.649                 
               NaN                 37.163                          35.779                 
               NaN                 25.876                          21.978                 
                13                 12.786                          14.141                 
                36                 28.889                          27.281                 
              19.9                 22.199                          18.451                 
              24.2                 23.995                          24.885                 
                12                 14.247                          13.982                 
                38                 33.797                          33.528                 
                13                 12.225                          11.127                 
    
    

    Input Arguments

    collapse all

    Generalized additive model, specified as a RegressionGAM or a CompactRegressionGAM model object.

    Predictor data, specified as a numeric matrix or table.

    Each row of X corresponds to one observation, and each column corresponds to one variable.

    • For a numeric matrix:

      • The variables that make up the columns of X must have the same order as the predictor variables that trained Mdl.

      • If you trained Mdl using a table, then X can be a numeric matrix if the table contains all numeric predictor variables.

    • For a table:

      • If you trained Mdl using a table (for example, Tbl), then all predictor variables in X must have the same variable names and data types as those in Tbl. However, the column order of X does not need to correspond to the column order of Tbl.

      • If you trained Mdl using a numeric matrix, then the predictor names in Mdl.PredictorNames and the corresponding predictor variable names in X must be the same. To specify predictor names during training, use the 'PredictorNames' name-value argument. All predictor variables in X must be numeric vectors.

      • X can contain additional variables (response variables, observation weights, and so on), but predict ignores them.

      • predict does not support multicolumn variables or cell arrays other than cell arrays of character vectors.

    Data Types: table | double | single

    Flag to include interaction terms of the model, specified as true or false.

    The default includeInteractions 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 X.

    Introduced in R2021a