Train a cross-validated generalized additive model (GAM) with 10 folds. Then, use `kfoldLoss`

to compute the cumulative cross-validation regression loss (mean squared errors). Use the errors to determine the optimal number of trees per predictor (linear term for predictor) and the optimal number of trees per interaction term.

Alternatively, you can find optimal values of `fitrgam`

name-value arguments by using the `bayesopt`

function. For an example, see Optimize Cross-Validated GAM Using bayesopt.

Load the `patients`

data set.

Create a table that contains the predictor variables (`Age`

, `Diastolic`

, `Smoker`

, `Weight`

, `Gender`

, and `SelfAssessedHealthStatus`

) and the response variable (`Systolic`

).

Create a cross-validated GAM by using the default cross-validation option. Specify the `'CrossVal'`

name-value argument as `'on'`

. Also, specify to include 5 interaction terms.

If you specify `'Mode'`

as `'cumulative'`

for `kfoldLoss`

, then the function returns cumulative errors, which are the average errors across all folds obtained using the same number of trees for each fold. Display the number of trees for each fold.

ans = *struct with fields:*
PredictorTrees: [300 300 300 300 300 300 300 300 300 300]
InteractionTrees: [76 100 100 100 100 42 100 100 59 100]

`kfoldLoss`

can compute cumulative errors using up to 300 predictor trees and 42 interaction trees.

Plot the cumulative, 10-fold cross-validated, mean squared errors. Specify `'IncludeInteractions'`

as `false`

to exclude interaction terms from the computation.

The first element of `L_noInteractions`

is the average error over all folds obtained using only the intercept (constant) term. The (`J+1`

)th element of `L_noInteractions`

is the average error obtained using the intercept term and the first `J`

predictor trees per linear term. Plotting the cumulative loss allows you to monitor how the error changes as the number of predictor trees in the GAM increases.

Find the minimum error and the number of predictor trees used to achieve the minimum error.

The GAM achieves the minimum error when it includes 5 predictor trees.

Compute the cumulative mean squared error using both linear terms and interaction terms.

The first element of `L`

is the average error over all folds obtained using the intercept (constant) term and all predictor trees per linear term. The (`J+1`

)th element of `L`

is the average error obtained using the intercept term, all predictor trees per linear term, and the first `J`

interaction trees per interaction term. The plot shows that the error increases when interaction terms are added.

If you are satisfied with the error when the number of predictor trees is 5, you can create a predictive model by training the univariate GAM again and specifying `'NumTreesPerPredictor',5`

without cross-validation.