resubPredict - Predict responses for training data using trained regression model - MATLAB (original) (raw)
Predict responses for training data using trained regression model
Syntax
Description
[yFit](#mw%5F690cef21-f4cf-4d5b-ae12-b1f79d934fb1) = resubPredict([Mdl](#mw%5Fbc0098ff-3830-4e32-b828-d6ea0172e6a6%5Fsep%5Fshared-Mdl))
returns predicted responses for the trained regression model Mdl
using the predictor data stored in Mdl.X
.
[yFit](#mw%5F690cef21-f4cf-4d5b-ae12-b1f79d934fb1) = resubPredict([Mdl](#mw%5Fbc0098ff-3830-4e32-b828-d6ea0172e6a6%5Fsep%5Fshared-Mdl),[Name=Value](#namevaluepairarguments))
specifies options using one or more name-value arguments. For example,IncludeInteractions=true
specifies to include interaction terms in computations for generalized additive models.
[[yFit](#mw%5F690cef21-f4cf-4d5b-ae12-b1f79d934fb1),[ySD](#mw%5F80012e5b-6f1a-4e5d-ae32-bb80e8d49447),[yInt](#mw%5Fb88bb827-fdcb-4522-b5f8-46f8baef5999)] = resubPredict(___)
also returns the standard deviations and prediction intervals of the response variable, evaluated at each observation in the predictor data Mdl.X
, using any of the input argument combinations in the previous syntaxes. This syntax applies only to generalized additive models for which IsStandardDeviationFit is true
, and to Gaussian process regression models for which the PredictMethod is not'bcd'
.
Examples
Train a generalized additive model (GAM), then predict responses for the training data.
Load the patients
data set.
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: {'Age' 'Diastolic' 'Smoker' 'Weight' 'Gender' 'SelfAssessedHealthStatus'} ResponseName: 'Systolic' CategoricalPredictors: [3 5 6] ResponseTransform: 'none' Intercept: 122.7800 IsStandardDeviationFit: 0 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)
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
Train a Gaussian process regression (GPR) model by using the fitrgp
function. Then predict responses for the training data and estimate prediction intervals of the responses at each observation in the training data by using the resubPredict
function.
Generate a training data set.
rng(1) % For reproducibility n = 100000; X = linspace(0,1,n)'; X = [X,X.^2]; y = 1 + X*[1;2] + sin(20X[1;-2]) + 0.2*randn(n,1);
Train a GPR model using the squared exponential kernel function. Estimate parameters by using the subset of regressors ('sr'
) approximation method, and make predictions using the subset of data ('sd'
) method. Use 50 points in the active set, and specify 'sgma'
(sparse greedy matrix approximation) method for active set selection. Because the scales of the first and second predictors are different, standardize the data set.
gprMdl = fitrgp(X,y,'KernelFunction','squaredExponential', ... 'FitMethod','sr','PredictMethod','sd', ... 'ActiveSetSize',50,'ActiveSetMethod','sgma','Standardize',true);
fitrgp accepts any combination of fitting, prediction, and active set selection methods. However, if you train a model using the block coordinate descent prediction method ('PredictMethod','bcd'
), you cannot use the model to compute the standard deviations of the predicted responses; therefore, you also cannot use the model to compute the prediction intervals. For more details, see Tips.
Use the trained model to predict responses for the training data and to estimate the prediction intervals of the predicted responses.
[ypred,~,yci] = resubPredict(gprMdl);
Plot the true responses, predicted responses, and prediction intervals.
figure plot(y,'r') hold on plot(ypred,'b') plot(yci(:,1),'k--') plot(yci(:,2),'k--') legend('True responses','GPR predictions','95% prediction limits','Location','Best') xlabel('X') ylabel('y') hold off
Compute the mean squared error loss on the training data using the trained GPR model.
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.
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)
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
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: Alpha=0.01,IncludeInteractions=false
specifies the confidence level as 99% and excludes interaction terms from computations for a generalized additive model.
Significance level for the confidence level of the prediction intervalsyInt, specified as a numeric scalar in the range[0,1]
. The confidence level of yInt
is equal to 100(1 – Alpha)%
.
This argument is valid only for a generalized additive model object that includes the standard deviation fit, or a Gaussian process regression model that does not use the block coordinate descent method for prediction. That is, you can specify this argument only in one of these situations:
- Mdl is RegressionGAM and the IsStandardDeviationFit property of
Mdl
istrue
. Mdl
is RegressionGP and the PredictMethod property ofMdl
is not'bcd'
.
Example: Alpha=0.01
Data Types: single
| double
Flag to include interaction terms of the model, specified as true
orfalse
. This argument is valid only for a generalized additive model. That is, you can specify this argument only whenMdl 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
Since R2024b
Output type for the predicted responses yFit, specified as"matrix"
or "table"
. This argument is valid only for a neural network model with multiple response variables. That is, you can specify this argument only when Mdl is a RegressionNeuralNetwork object, where Mdl.Y
contains data for multiple response variables.
Example: OutputType="table"
Data Types: char
| string
Since R2023b
Predicted response value to use for observations with missing predictor values, specified as "median"
, "mean"
, or a numeric scalar. This argument is valid only for a Gaussian process regression or neural network model. That is, you can specify this argument only whenMdl is a RegressionGP or RegressionNeuralNetwork object.
Value | Description |
---|---|
"median" | resubPredict uses the median of the observed response values in the training data as the predicted response value for observations with missing predictor values.This value is the default when Mdl is a RegressionGP orRegressionNeuralNetwork object. |
"mean" | resubPredict uses the mean of the observed response values in the training data as the predicted response value for observations with missing predictor values. |
Numeric scalar | resubPredict uses this value as the predicted response value for observations with missing predictor values. |
Example: PredictionForMissingValue="mean"
Example: PredictionForMissingValue=NaN
Data Types: single
| double
| char
| string
Output Arguments
Predicted responses, returned as a numeric vector, matrix, or table.
- If
yFit
is a vector, then it has length_n_, where n is the number of observations in the predictor data (Mdl.X
). - If
yFit
is a matrix or table, then it has_n_ rows, where n is the number of observations in the predictor data.yFit
is a matrix or table only when Mdl is a multiresponse regression neural network model.
Standard deviations of the response variable, evaluated at each observation in the predictor data [Mdl](#mw%5Fbc0098ff-3830-4e32-b828-d6ea0172e6a6%5Fsep%5Fshared-Mdl).X
, returned as a column vector of length n, where n is the number of observations in `Mdl`.X
. Thei
th element ySD(i)
contains the standard deviation of the i
th response for the i
th observationMdl.X(i,:)
, estimated using the trained standard deviation model inMdl
.
This argument is valid only for a generalized additive model object that includes the standard deviation fit, or a Gaussian process regression model that does not use the block coordinate descent method for prediction. That is,resubPredict
can return this argument only in one of these situations:
Mdl
is RegressionGAM and the IsStandardDeviationFit property ofMdl
istrue
.Mdl
is RegressionGP and the PredictMethod property ofMdl
is not'bcd'
.
Prediction intervals of the response variable, evaluated at each observation in the predictor data [Mdl](#mw%5Fbc0098ff-3830-4e32-b828-d6ea0172e6a6%5Fsep%5Fshared-Mdl).X
, returned as an_n_-by-2 matrix, where n is the number of observations in `Mdl`.X
. Thei
th row yInt(i,:)
contains the100(1 – [Alpha](#mw%5F5e46816d-0b29-4de1-8f64-67403ea806ed))%
prediction interval of the i
th response for the i
th observation Mdl.X(i,:)
. The Alpha
value is the probability that the prediction interval does not contain the true response valueMdl.Y(i)
. The first column of yInt
contains the lower limits of the prediction intervals, and the second column contains the upper limits.
This argument is valid only for a generalized additive model object that includes the standard deviation fit, or a Gaussian process regression model that does not use the block coordinate descent method for prediction. That is,resubPredict
can return this argument only in one of these situations:
Mdl
is RegressionGAM and the IsStandardDeviationFit property ofMdl
istrue
.Mdl
is RegressionGP and the PredictMethod property ofMdl
is not'bcd'
.
Algorithms
resubPredict
predicts responses according to the correspondingpredict
function of the object (Mdl). For a model-specific description, see the predict
function reference pages in the following table.
Alternative Functionality
To compute the predicted responses for new predictor data, use the correspondingpredict
function of the object (Mdl
).
Extended Capabilities
Version History
Introduced in R2015b
You can create a neural network regression model with multiple response variables by using the fitrnet function. Regardless of the number of response variables, the function returns aRegressionNeuralNetwork
object. You can use theresubPredict
object function to predict the responses for the training data.
In the call to resubPredict
, you can specify whether to return the predicted response values as a matrix or table by using the OutputType name-value argument.
Starting in R2023b, when you predict or compute the loss, some regression models allow you to specify the predicted response value for observations with missing predictor values. Specify the PredictionForMissingValue
name-value argument to use a numeric scalar, the training set median, or the training set mean as the predicted value. When computing the loss, you can also specify to omit observations with missing predictor values.
This table lists the object functions that support thePredictionForMissingValue
name-value argument. By default, the functions use the training set median as the predicted response value for observations with missing predictor values.
Model Type | Model Objects | Object Functions |
---|---|---|
Gaussian process regression (GPR) model | RegressionGP, CompactRegressionGP | loss, predict, resubLoss, resubPredict |
RegressionPartitionedGP | kfoldLoss, kfoldPredict | |
Gaussian kernel regression model | RegressionKernel | loss, predict |
RegressionPartitionedKernel | kfoldLoss, kfoldPredict | |
Linear regression model | RegressionLinear | loss, predict |
RegressionPartitionedLinear | kfoldLoss, kfoldPredict | |
Neural network regression model | RegressionNeuralNetwork, CompactRegressionNeuralNetwork | loss, predict, resubLoss, resubPredict |
RegressionPartitionedNeuralNetwork | kfoldLoss, kfoldPredict | |
Support vector machine (SVM) regression model | RegressionSVM, CompactRegressionSVM | loss, predict, resubLoss, resubPredict |
RegressionPartitionedSVM | kfoldLoss, kfoldPredict |
In previous releases, the regression model loss
and predict
functions listed above used NaN
predicted response values for observations with missing predictor values. The software omitted observations with missing predictor values from the resubstitution ("resub") and cross-validation ("kfold") computations for prediction and loss.