loss
Description
L = loss(Mdl,Tbl,ResponseVarName)L), a scalar representing how well the
        generalized additive model Mdl predicts the predictor data in
          Tbl compared to the true response values in
          Tbl.ResponseVarName.
The interpretation of L depends on the loss function
          ('LossFun') and weighting scheme ('Weights'). In
        general, better models yield smaller loss values. The default 'LossFun'
        value is 'mse' (mean squared error).
L = loss(___,Name,Value)
Examples
Determine the test sample regression loss (mean squared error) of a generalized additive model. When you compare the same type of loss among many models, a lower loss indicates a better predictive model.
Load the patients data set. 
load patientsCreate 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");Determine how well the algorithm generalizes by estimating the test sample regression loss. By default, the loss function of RegressionGAM estimates the mean squared error.
L = loss(Mdl,tbl(testInds,:))
L = 35.7540
Train a generalized additive model (GAM) that contains both linear and interaction terms for predictors, and estimate the regression loss (mean squared error, MSE) with and without interaction terms for the training data and test data. Specify whether to include interaction terms when estimating the regression loss.
Load the carbig data set, which contains measurements of cars made in the 1970s and early 1980s.
load carbigSpecify 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') % For reproducibility n = size(X,1); newInds = randsample(n,10); inds = ~ismember(1:n,newInds); XNew = X(newInds,:); YNew = Y(newInds);
Train a generalized additive model 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.
Compute the resubstitution MSEs (that is, the in-sample MSEs) both with and without interaction terms in Mdl. To exclude interaction terms, specify 'IncludeInteractions',false.
resubl = resubLoss(Mdl)
resubl = 0.0292
resubl_nointeraction = resubLoss(Mdl,'IncludeInteractions',false)resubl_nointeraction = 4.7330
Compute the regression MSEs both with and without interaction terms for the test data set. Use a memory-efficient model object for the computation.
CMdl = compact(Mdl);
CMdl is a CompactRegressionGAM model object.
l = loss(CMdl,XNew,YNew)
l = 12.8604
l_nointeraction = loss(CMdl,XNew,YNew,'IncludeInteractions',false)l_nointeraction = 15.6741
Including interaction terms achieves a smaller error for the training data set and test data set.
Input Arguments
Generalized additive model, specified as a RegressionGAM
            or CompactRegressionGAM model object.
Sample data, specified as a table. Each row of Tbl corresponds
            to one observation, and each column corresponds to one predictor variable. Multicolumn
            variables and cell arrays other than cell arrays of character vectors are not
            allowed.
Tbl must contain all of the predictors used to train
              Mdl. Optionally, Tbl can contain a column
            for the response variable and a column for the observation weights.
- The response variable must be a numeric vector. If the response variable in - Tblhas the same name as the response variable used to train- Mdl, then you do not need to specify- ResponseVarName.
- The weight values must be a numeric vector. You must specify the observation weights in - Tblby using- 'Weights'.
If you trained Mdl using sample data contained in a table, then
            the input data for loss must also be in a table.
Data Types: table
Response variable name, specified as a character vector or string scalar containing the name
            of the response variable in Tbl. For example, if the response
            variable Y is stored in Tbl.Y, then specify it as
                'Y'.
Data Types: char | string
Predictor data, specified as a numeric matrix. Each row of X corresponds to one observation, and each column corresponds to one predictor variable.
If you trained Mdl using sample data contained in a matrix, then the input data for loss must also be in a matrix.
Data Types: single | double
Name-Value Arguments
Specify optional pairs of arguments as
      Name1=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: 'IncludeInteractions',false,'Weights',w specifies to exclude
        interaction terms from the model and to use the observation weights
        w.
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.
Example: 'IncludeInteractions',false
Data Types: logical
Loss function, specified as 'mse' or a function handle.
- 'mse'— Weighted mean squared error.
- Function handle — To specify a custom loss function, use a function handle. The function must have this form: - lossval = lossfun(Y,YFit,W) - The output argument - lossvalis a floating-point scalar.
- You specify the function name ( - lossfun
- Yis a length n numeric vector of observed responses, where n is the number of observations in- Tblor- X.
- YFitis a length n numeric vector of corresponding predicted responses.
- Wis an n-by-1 numeric vector of observation weights.
 
Example: 'LossFun',@lossfun
Data Types: char | string | function_handle
Observation weights, specified as a vector of scalar values or the name of a variable in Tbl. The software weights the observations in each row of X or Tbl with the corresponding value in Weights. The size of Weights must equal the number of rows in X or Tbl.
If you specify the input data as a table Tbl, then Weights can be the name of a variable in Tbl that contains a numeric vector. In this case, you must specify Weights as a character vector or string scalar. For example, if weights vector W is stored as Tbl.W, then specify it as 'W'.
loss normalizes the values of Weights to sum to 1.
Data Types: single | double | char | string
More About
The weighted mean squared error measures the predictive inaccuracy of regression models. When you compare the same type of loss among many models, a lower error indicates a better predictive model.
The weighted mean squared error is calculated as follows:
where:
- n is the number of rows of data. 
- xj is the jth row of data. 
- yj is the true response to xj. 
- f(xj) is the response prediction of the model - Mdlto xj.
- w is the vector of observation weights. 
Version History
Introduced in R2021a
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.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)