# loss

Regression error

## Syntax

L = loss(ens,tbl,ResponseVarName)
L = loss(ens,tbl,Y)
L = loss(ens,X,Y)
L = loss(___,Name,Value)

## Description

L = loss(ens,tbl,ResponseVarName) returns the mean squared error between the predictions of ens to the data in tbl, compared to the true responses tbl.ResponseVarName.

L = loss(ens,tbl,Y) returns the mean squared error between the predictions of ens to the data in tbl, compared to the true responses Y.

L = loss(ens,X,Y) returns the mean squared error between the predictions of ens to the data in X, compared to the true responses Y.

L = loss(___,Name,Value) computes the error in prediction with additional options specified by one or more Name,Value pair arguments, using any of the previous syntaxes.

## Input Arguments

 ens A regression ensemble created with fitrensemble, or the compact method. tbl Sample data, specified as a table. Each row of tbl corresponds to one observation, and each column corresponds to one predictor variable. tbl must contain all of the predictors used to train the model. Multi-column variables and cell arrays other than cell arrays of character vectors are not allowed. If you trained ens using sample data contained in a table, then the input data for this method must also be in a table. ResponseVarName Response variable name, specified as the name of a variable in tbl. The response variable must be a numeric vector. You must specify ResponseVarName as a character vector or string scalar. For example, if the response variable Y is stored as tbl.Y, then specify it as 'Y'. Otherwise, the software treats all columns of tbl, including Y, as predictors when training the model. X A matrix of predictor values. Each column of X represents one variable, and each row represents one observation. NaN values in X are taken to be missing values. Observations with all missing values for X are not used in the calculation of loss. If you trained ens using sample data contained in a matrix, then the input data for this method must also be in a matrix. Y A numeric column vector with the same number of rows as tbl or X. Each entry in Y is the response to the data in the corresponding row of tbl or X. NaN values in Y are taken to be missing values. Observations with missing values for Y are not used in the calculation of loss.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

 'learners' Indices of weak learners in the ensemble ranging from 1 to ens.NumTrained. oobEdge uses only these learners for calculating loss. Default: 1:NumTrained 'lossfun' Function handle for loss function, or 'mse', meaning mean squared error. If you pass a function handle fun, loss calls it as fun(Y,Yfit,W) where Y, Yfit, and W are numeric vectors of the same length. Y is the observed response.Yfit is the predicted response.W is the observation weights. The returned value fun(Y,Yfit,W) should be a scalar. Default: 'mse' 'mode' Meaning of the output L: 'ensemble' — L is a scalar value, the loss for the entire ensemble.'individual' — L is a vector with one element per trained learner.'cumulative' — L is a vector in which element J is obtained by using learners 1:J from the input list of learners. Default: 'ensemble' 'UseObsForLearner' A logical matrix of size N-by-NumTrained, where N is the number of observations in ens.X, and NumTrained is the number of weak learners. When UseObsForLearner(I,J) is true, predict uses learner J in predicting observation I. Default: true(N,NumTrained) 'weights' Numeric vector of observation weights with the same number of elements as Y. The formula for loss with weights is in Weighted Mean Squared Error. Default: ones(size(Y))

## Output Arguments

 L Weighted mean squared error of predictions. The formula for loss is in Weighted Mean Squared Error.

## Examples

expand all

Find the loss of an ensemble predictor using the carsmall data set.

Load the carsmall data set and select engine displacement, horsepower, and vehicle weight as predictors.

X = [Displacement Horsepower Weight];

Train an ensemble of regression trees and find the regression error for predicting MPG.

ens = fitrensemble(X,MPG);
L = loss(ens,X,MPG)
L = 0.3463