# resubLoss

Regression error by resubstitution

## Syntax

```L = resubLoss(ens) L = resubLoss(ens,Name,Value) ```

## Description

`L = resubLoss(ens)` returns the resubstitution loss, meaning the mean squared error computed for the data that `fitrensemble` used to create `ens`.

`L = resubLoss(ens,Name,Value)` calculates loss with additional options specified by one or more `Name,Value` pair arguments. You can specify several name-value pair arguments in any order as `Name1,Value1,…,NameN,ValueN`.

## Input Arguments

 `ens` A regression ensemble created with `fitrensemble`.

### 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.

 `learners` Indices of weak learners in the ensemble ranging from `1` to `ens``.NumTrained`. `resubLoss` 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`, `resubLoss` 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, and `W` is the observation weights. Default: `'mse'` `mode` Character vector or string scalar representing the 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'` `UseParallel` Indication to perform inference in parallel, specified as `false` (compute serially) or `true` (compute in parallel). Parallel computation requires Parallel Computing Toolbox™. Parallel inference can be faster than serial inference, especially for large datasets. Parallel computation is supported only for tree learners.Default: `false`

## Output Arguments

 `L` Loss, by default the mean squared error. `L` can be a vector, and can mean different things, depending on the name-value pair settings.

## Examples

expand all

Find the mean-squared difference between resubstitution predictions and training data.

Load the `carsmall` data set and select horsepower and vehicle weight as predictors.

```load carsmall X = [Horsepower Weight];```

Train an ensemble of regression trees, and find the mean-squared difference of predictions from the training data.

```ens = fitrensemble(X,MPG); MSE = resubLoss(ens) ```
```MSE = 0.5836 ```