Main Content

Fit generalized additive model (GAM) for regression

returns a generalized additive model
`Mdl`

= fitrgam(`Tbl`

,`ResponseVarName`

)`Mdl`

trained using the sample data contained in the table
`Tbl`

. The input argument `ResponseVarName`

is the
name of the variable in `Tbl`

that contains the response values for
regression.

specifies options using one or more name-value arguments in addition to any of the input
argument combinations in the previous syntaxes. For example,
`Mdl`

= fitrgam(___,`Name,Value`

)`'Interactions',5`

specifies to include five interaction terms in the
model. You can also specify a list of interaction terms using the
`'Interactions'`

name-value argument.

Train a univariate GAM, which contains linear terms for predictors. Then, interpret the prediction for a specified data instance by using the `plotLocalEffects`

function.

Load the data set `NYCHousing2015`

.

`load NYCHousing2015`

The data set includes 10 variables with information on the sales of properties in New York City in 2015. This example uses these variables to analyze the sale prices (`SALEPRICE`

).

Preprocess the data set. Remove outliers, convert the `datetime`

array (`SALEDATE`

) to the month numbers, and move the response variable (`SALEPRICE`

) to the last column.

idx = isoutlier(NYCHousing2015.SALEPRICE); NYCHousing2015(idx,:) = []; NYCHousing2015.SALEDATE = month(NYCHousing2015.SALEDATE); NYCHousing2015 = movevars(NYCHousing2015,'SALEPRICE','After','SALEDATE');

Display the first three rows of the table.

head(NYCHousing2015,3)

`ans=`*3×10 table*
BOROUGH NEIGHBORHOOD BUILDINGCLASSCATEGORY RESIDENTIALUNITS COMMERCIALUNITS LANDSQUAREFEET GROSSSQUAREFEET YEARBUILT SALEDATE SALEPRICE
_______ ____________ ____________________________ ________________ _______________ ______________ _______________ _________ ________ _________
2 {'BATHGATE'} {'01 ONE FAMILY DWELLINGS'} 1 0 4750 2619 1899 8 0
2 {'BATHGATE'} {'01 ONE FAMILY DWELLINGS'} 1 0 4750 2619 1899 8 0
2 {'BATHGATE'} {'01 ONE FAMILY DWELLINGS'} 1 1 1287 2528 1899 12 0

Train a univariate GAM for the sale prices. Specify the variables for `BOROUGH`

, `NEIGHBORHOOD`

, `BUILDINGCLASSCATEGORY`

, and `SALEDATE`

as categorical predictors.

Mdl = fitrgam(NYCHousing2015,'SALEPRICE','CategoricalPredictors',[1 2 3 9])

Mdl = RegressionGAM PredictorNames: {1x9 cell} ResponseName: 'SALEPRICE' CategoricalPredictors: [1 2 3 9] ResponseTransform: 'none' Intercept: 3.7518e+05 NumObservations: 83517 Properties, Methods

`Mdl`

is a `RegressionGAM`

model object. The model display shows a partial list of the model properties. To view the full list of properties, double-click the variable name `Mdl`

in the Workspace. The Variables editor opens for `Mdl`

. Alternatively, you can display the properties in the Command Window by using dot notation. For example, display the estimated intercept (constant) term of `Mdl`

.

Mdl.Intercept

ans = 3.7518e+05

Predict the sale price for the first observation of the training data, and plot the local effects of the terms in `Mdl`

on the prediction.

yFit = predict(Mdl,NYCHousing2015(1,:))

yFit = 4.4421e+05

plotLocalEffects(Mdl,NYCHousing2015(1,:))

The `predict`

function predicts the sale price for the first observation as `4.4421e5`

. The `plotLocalEffects`

function creates a horizontal bar graph that shows the local effects of the terms in `Mdl`

on the prediction. Each local effect value shows the contribution of each term to the predicted sale price.

Train a generalized additive model that contains linear and interaction terms for predictors in three different ways:

Specify the interaction terms using the

`formula`

input argument.Specify the

`'Interactions'`

name-value argument.Build a model with linear terms first and add interaction terms to the model by using the

`addInteractions`

function.

Load the `carbig`

data set, which contains measurements of cars made in the 1970s and early 1980s.

`load carbig`

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

, `Displacement`

, `Horsepower`

, and `Weight`

) and the response variable (`MPG`

).

tbl = table(Acceleration,Displacement,Horsepower,Weight,MPG);

**Specify formula**

Train a GAM that contains the four linear terms (`Acceleration`

, `Displacement`

, `Horsepower`

, and `Weight`

) and two interaction terms (`Acceleration*Displacement`

and `Displacement*Horsepower`

). Specify the terms using a formula in the form `'Y ~ terms'`

.

`Mdl1 = fitrgam(tbl,'MPG ~ Acceleration + Displacement + Horsepower + Weight + Acceleration:Displacement + Displacement:Horsepower');`

The function adds interaction terms to the model in the order of importance. You can use the `Interactions`

property to check the interaction terms in the model and the order in which `fitrgam`

adds them to the model. Display the `Interactions`

property.

Mdl1.Interactions

`ans = `*2×2*
2 3
1 2

Each row of `Interactions`

represents one interaction term and contains the column indexes of the predictor variables for the interaction term.

**Specify 'Interactions'**

Pass the training data (`tbl`

) and the name of the response variable in `tbl`

to `fitrgam`

, so that the function includes the linear terms for all the other variables as predictors. Specify the `'Interactions'`

name-value argument using a logical matrix to include the two interaction terms, `x1*x2`

and `x2*x3`

.

Mdl2 = fitrgam(tbl,'MPG','Interactions',logical([1 1 0 0; 0 1 1 0])); Mdl2.Interactions

`ans = `*2×2*
2 3
1 2

You can also specify `'Interactions'`

as the number of interaction terms or as `'all'`

to include all available interaction terms. Among the specified interaction terms, `fitrgam`

identifies those whose *p*-values are not greater than the `'MaxPValue'`

value and adds them to the model. The default `'MaxPValue'`

is 1 so that the function adds all specified interaction terms to the model.

Specify `'Interactions','all'`

and set the `'MaxPValue'`

name-value argument to 0.05.

Mdl3 = fitrgam(tbl,'MPG','Interactions','all','MaxPValue',0.05);

Warning: Model does not include interaction terms because all interaction terms have p-values greater than the 'MaxPValue' value, or the software was unable to improve the model fit.

Mdl3.Interactions

ans = 0x2 empty double matrix

`Mdl3`

includes no interaction terms, which implies one of the following: all interaction terms have *p*-values greater than 0.05, or adding the interaction terms does not improve the model fit.

**Use addInteractions Function**

Train a univariate GAM that contains linear terms for predictors, and then add interaction terms to the trained model by using the `addInteractions`

function. Specify the second input argument of `addInteractions`

in the same way you specify the `'Interactions'`

name-value argument of `fitrgam`

. You can specify the list of interaction terms using a logical matrix, the number of interaction terms, or `'all'`

.

Specify the number of interaction terms as 3 to add the three most important interaction terms to the trained model.

```
Mdl4 = fitrgam(tbl,'MPG');
UpdatedMdl4 = addInteractions(Mdl4,3);
UpdatedMdl4.Interactions
```

`ans = `*3×2*
2 3
1 2
3 4

`Mdl4`

is a univariate GAM, and `UpdatedMdl4`

is an updated GAM that contains all the terms in `Mdl4`

and three additional interaction terms.

`fitrgam`

Train a cross-validated GAM with 10 folds, which is the default cross-validation option, by using `fitrgam`

. Then, use `kfoldPredict`

to predict responses for validation-fold observations using a model trained on training-fold observations.

Load the `carbig`

data set, which contains measurements of cars made in the 1970s and early 1980s.

`load carbig`

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

, `Displacement`

, `Horsepower`

, and `Weight`

) and the response variable (`MPG`

).

tbl = table(Acceleration,Displacement,Horsepower,Weight,MPG);

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

name-value argument as `'on'`

.

rng('default') % For reproducibility CVMdl = fitrgam(tbl,'MPG','CrossVal','on')

CVMdl = RegressionPartitionedGAM CrossValidatedModel: 'GAM' PredictorNames: {1x4 cell} ResponseName: 'MPG' NumObservations: 398 KFold: 10 Partition: [1x1 cvpartition] NumTrainedPerFold: [1x1 struct] ResponseTransform: 'none' Properties, Methods

The `fitrgam`

function creates a `RegressionPartitionedGAM`

model object `CVMdl`

with 10 folds. During cross-validation, the software completes these steps:

Randomly partition the data into 10 sets.

For each set, reserve the set as validation data, and train the model using the other 9 sets.

Store the 10 compact, trained models a in a 10-by-1 cell vector in the

`Trained`

property of the cross-validated model object`RegressionPartitionedGAM`

.

You can override the default cross-validation setting by using the `'CVPartition'`

, `'Holdout'`

, `'KFold'`

, or `'Leaveout' `

name-value argument.

Predict responses for the observations in `tbl`

by using `kfoldPredict`

. The function predicts responses for every observation using the model trained without that observation.

yHat = kfoldPredict(CVMdl);

`yHat`

is a numeric vector. Display the first five predicted responses.

yHat(1:5)

`ans = `*5×1*
19.4848
15.7203
15.5742
15.3185
17.8223

Compute the regression loss (mean squared error).

L = kfoldLoss(CVMdl)

L = 17.7248

`kfoldLoss`

returns the average mean squared error over 10 folds.

`bayesopt`

Optimize the parameters of a GAM with respect to cross-validation by using the `bayesopt`

function.

Load the `carbig`

data set, which contains measurements of cars made in the 1970s and early 1980s.

`load carbig`

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;

Prepare `optimizableVariable`

objects for the name-value arguments that you want to optimize using Bayesian optimization. This example finds optimal values for the `MaxNumSplitsPerPredictor`

and `NumTreesPerPredictor`

arguments of `fitrgam`

.

maxNumSplits = optimizableVariable('maxNumSplits',[1,10],'Type','integer'); numTrees = optimizableVariable('numTrees',[1,500],'Type','integer');

Create an objective function that takes an input `z = [maxNumSplits,numTrees]`

and returns the cross-validated loss value of `z`

.

minfun = @(z)kfoldLoss(fitrgam(X,Y,'CrossVal','on', ... 'MaxNumSplitsPerPredictor',z.maxNumSplits, ... 'NumTreesPerPredictor',z.numTrees));

If you specify the cross-validation option (`'CrossVal','on'`

), then the `fitrgam`

function returns a cross-validated model object `RegressionPartitionedGAM`

. The `kfoldLoss`

function returns the regression loss (mean squared error) obtained by the cross-validated model. Therefore, the function handle `minfun`

computes the cross-validation loss at the parameters in `z`

.

Search for the best parameters `[maxNumSplits,numTrees]`

using `bayesopt`

. For reproducibility, choose the `'expected-improvement-plus'`

acquisition function. The default acquisition function depends on run time and, therefore, can give varying results.

rng('default') results = bayesopt(minfun,[maxNumSplits,numTrees],'Verbose',0, ... 'IsObjectiveDeterministic',true, ... 'AcquisitionFunctionName','expected-improvement-plus');

Obtain the best point from `results`

.

zbest = bestPoint(results)

`zbest=`*1×2 table*
maxNumSplits numTrees
____________ ________
1 215

Train an optimized GAM using the `zbest`

values.

Mdl = fitrgam(X,Y, ... 'MaxNumSplitsPerPredictor',zbest.maxNumSplits, ... 'NumTreesPerPredictor',zbest.numTrees);

`Tbl`

— Sample datatable

Sample data used to train the model, 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.

Optionally,

`Tbl`

can contain a column for the response variable and a column for the observation weights. The response variable and the weight values must be numeric vectors.You must specify the response variable in

`Tbl`

by using`ResponseVarName`

or`formula`

and specify the observation weights in`Tbl`

by using`'Weights'`

.Specify the response variable by using

`ResponseVarName`

—`fitrgam`

uses the remaining variables as predictors. To use a subset of the remaining variables in`Tbl`

as predictors, specify predictor variables by using`'PredictorNames'`

.Define a model specification by using

`formula`

—`fitrgam`

uses a subset of the variables in`Tbl`

as predictor variables and the response variable, as specified in`formula`

.

If

`Tbl`

does not contain the response variable, then specify a response variable by using`Y`

. The length of the response variable`Y`

and the number of rows in`Tbl`

must be equal. To use a subset of the variables in`Tbl`

as predictors, specify predictor variables by using`'PredictorNames'`

.

`fitrgam`

considers `NaN`

,
`''`

(empty character vector), `""`

(empty string),
`<missing>`

, and `<undefined>`

values in
`Tbl`

to be missing values.

`fitrgam`

does not use observations with all missing values in the fit.`fitrgam`

does not use observations with missing response values in the fit.`fitrgam`

uses observations with some missing values for predictors to find splits on variables for which these observations have valid values.

**Data Types: **`table`

`ResponseVarName`

— Response variable namename of variable in

`Tbl`

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`

`formula`

— Model specificationcharacter vector | string scalar

Model specification, specified as a character vector or string scalar in the form
`'Y ~ terms'`

. The `formula`

argument specifies
a response variable and linear and interaction terms for predictor variables. Use
`formula`

to specify a subset of variables in
`Tbl`

as predictors for training the model. If you specify a
formula, then the software does not use any variables in `Tbl`

that
do not appear in `formula`

.

For example, specify `'Y~x1+x2+x3+x1:x2'`

. In this form,
`Y`

represents the response variable, and `x1`

,
`x2`

, and `x3`

represent the linear terms for the
predictor variables. `x1:x2`

represents the interaction term for
`x1`

and `x2`

.

The variable names in the formula must be both variable names in `Tbl`

(`Tbl.Properties.VariableNames`

) and valid MATLAB^{®} identifiers. You can verify the variable names in `Tbl`

by
using the `isvarname`

function. If the variable names
are not valid, then you can convert them by using the `matlab.lang.makeValidName`

function.

Alternatively, you can specify a response variable and linear terms for predictors
using `formula`

, and specify interaction terms for predictors using
`'Interactions'`

.

`fitrgam`

builds a set of interaction trees using only the
terms whose *p*-values are not greater than the
`'MaxPValue'`

value.

**Example: **`'Y~x1+x2+x3+x1:x2'`

**Data Types: **`char`

| `string`

`Y`

— Response datanumeric column vector

`X`

— Predictor datanumeric matrix

Predictor data, specified as a numeric matrix. Each row of `X`

corresponds to one observation, and each column corresponds to one predictor variable.

`fitrgam`

considers `NaN`

values in
`X`

as missing values. The function does not use observations
with all missing values in the fit. `fitrgam`

uses observations
with some missing values for `X`

to find splits on variables for
which these observations have valid values.

**Data Types: **`single`

| `double`

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`

.

`'Interactions','all','MaxPValue',0.05`

specifies to include
all available interaction terms whose `'InitialLearnRateForInteractions'`

— Initial learning rate of gradient boosting for interaction terms`1`

(default) | numeric scalar in (0,1]Initial learning rate of gradient boosting for interaction terms, specified as a numeric scalar in the interval (0,1].

For each boosting iteration for interaction trees, `fitrgam`

starts fitting with the initial learning rate. The function halves the learning rate
until it finds a rate that improves the model fit.

Training a model using a small learning rate requires more learning iterations, but often achieves better accuracy.

For more details about gradient boosting, see Gradient Boosting Algorithm.

**Example: **`'InitialLearnRateForInteractions',0.1`

**Data Types: **`single`

| `double`

`'InitialLearnRateForPredictors'`

— Initial learning rate of gradient boosting for linear terms`1`

(default) | numeric scalar in (0,1]Initial learning rate of gradient boosting for linear terms, specified as a numeric scalar in the interval (0,1].

For each boosting iteration for predictor trees, `fitrgam`

starts fitting with the initial learning rate. The function halves the learning rate
until it finds a rate that improves the model fit.

Training a model using a small learning rate requires more learning iterations, but often achieves better accuracy.

For more details about gradient boosting, see Gradient Boosting Algorithm.

**Example: **`'InitialLearnRateForPredictors',0.1`

**Data Types: **`single`

| `double`

`'Interactions'`

— Number or list of interaction terms`0`

(default) | nonnegative integer scalar | logical matrix | `'all'`

Number or list of interaction terms to include in the candidate set *S*,
specified as a nonnegative integer scalar, a logical matrix, or
`'all'`

.

Number of interaction terms, specified as a nonnegative integer —

*S*includes the specified number of important interaction terms, selected based on the*p*-values of the terms.List of interaction terms, specified as a logical matrix —

*S*includes the terms specified by a`t`

-by-`p`

logical matrix, where`t`

is the number of interaction terms, and`p`

is the number of predictors used to train the model. For example,`logical([1 1 0; 0 1 1])`

represents two pairs of interaction terms: a pair of the first and second predictors, and a pair of the second and third predictors.If

`fitrgam`

uses a subset of input variables as predictors, then the function indexes the predictors using only the subset. That is, the column indexes of the logical matrix do not count the response and observation weight variables. The indexes also do not count any variables not used by the function.`'all'`

—*S*includes all possible pairs of interaction terms, which is`p*(p – 1)/2`

number of terms in total.

Among the interaction terms in *S*, the `fitrgam`

function identifies those whose *p*-values are not greater than the
`'MaxPValue'`

value and uses them to build a set of
interaction trees. Use the default value (`'MaxPValue'`

,1) to
build interaction trees using all terms in *S*.

**Example: **`'Interactions','all'`

**Data Types: **`single`

| `double`

| `logical`

| `char`

| `string`

`'MaxNumSplitsPerInteraction'`

— Maximum number of decision splits per interaction tree4 (default) | positive integer scalar

Maximum number of decision splits (or branch nodes) for each interaction tree (boosted tree for an interaction term), specified as a positive integer scalar.

**Example: **`'MaxNumSplitsPerInteraction',5`

**Data Types: **`single`

| `double`

`'MaxNumSplitsPerPredictor'`

— Maximum number of decision splits per predictor tree1 (default) | positive integer scalar

Maximum number of decision splits (or branch nodes) for each predictor tree (boosted tree for
a linear term), specified as a positive integer
scalar. By default,
`fitrgam`

uses a tree stump
for a predictor tree.

**Example: **`'MaxNumSplitsPerPredictor',5`

**Data Types: **`single`

| `double`

`'MaxPValue'`

— Maximum 1 (default) | numeric scalar in [0,1]

Maximum *p*-value for detecting interaction terms, specified as a numeric
scalar in the interval [0,1].

`fitrgam`

first finds the candidate set *S* of
interaction terms from `formula`

or
`'Interactions'`

. Then the function identifies the interaction
terms whose *p*-values are not greater than the
`'MaxPValue'`

value and uses them to build a set of interaction
trees.

The default value (`'MaxPValue',1`

) builds interaction trees for all
interaction terms in the candidate set *S*.

For more details about detecting interaction terms, see Interaction Term Detection.

**Example: **`'MaxPValue',0.05`

**Data Types: **`single`

| `double`

`'NumBins'`

— Number of bins for numeric predictors`256`

(default) | positive integer scalar | `[]`

(empty)Number of bins for numeric predictors, specified as a positive integer scalar or
`[]`

(empty).

If you specify the

`'NumBins'`

value as a positive integer scalar (`numBins`

), then`fitrgam`

bins every numeric predictor into at most`numBins`

equiprobable bins, and then grows trees on the bin indices instead of the original data.The number of bins can be less than

`numBins`

if a predictor has fewer than`numBins`

unique values.`fitrgam`

does not bin categorical predictors.

If the

`'NumBins'`

value is empty (`[]`

), then`fitrgam`

does not bin any predictors.

When you use a large training data set, this binning option speeds up training but might cause
a decrease in accuracy. You can first use the default value of
`'NumBins'`

, and then change the value depending on the accuracy
and training speed.

The trained model `Mdl`

stores the bin edges in the
`BinEdges`

property.

**Example: **`'NumBins',50`

**Data Types: **`single`

| `double`

`'NumTreesPerInteraction'`

— Number of trees per interaction term100 (default) | positive integer scalar

Number of trees per interaction term, specified as a positive integer scalar.

The `'NumTreesPerInteraction'`

value is equivalent to the number of
gradient boosting iterations for the interaction terms for predictors. For each
iteration, `fitrgam`

adds a set of interaction trees to the
model, one tree for each interaction term. To learn about the gradient boosting
algorithm, see Gradient Boosting Algorithm.

You can determine whether the fitted model has the specified number of trees by
viewing the diagnostic message displayed when `'Verbose'`

is 1 or 2,
or by checking the `ReasonForTermination`

property value of the model
`Mdl`

.

**Example: **`'NumTreesPerInteraction',500`

**Data Types: **`single`

| `double`

`'NumTreesPerPredictor'`

— Number of trees per linear term300 (default) | positive integer scalar

Number of trees per linear term, specified as a positive integer scalar.

The `'NumTreesPerPredictor'`

value is equivalent to the number of
gradient boosting iterations for the linear terms for predictors. For each iteration,
`fitrgam`

adds a set of predictor trees to the model, one
tree for each predictor. To learn about the gradient boosting algorithm, see Gradient Boosting Algorithm.

You can determine whether the fitted model has the specified number of trees by
viewing the diagnostic message displayed when `'Verbose'`

is 1 or 2,
or by checking the `ReasonForTermination`

property value of the model
`Mdl`

.

**Example: **`'NumTreesPerPredictor',500`

**Data Types: **`single`

| `double`

`'CategoricalPredictors'`

— Categorical predictors listvector of positive integers | logical vector | character matrix | string array | cell array of character vectors |

`'all'`

Categorical predictors list, specified as one of the values in this table.

Value | Description |
---|---|

Vector of positive integers |
Each entry in the vector is an index value corresponding to the column of the predictor data that contains a categorical variable. The index values are between 1 and If |

Logical vector |
A |

Character matrix | Each row of the matrix is the name of a predictor variable. The names must match the entries in `PredictorNames` . Pad the names with extra blanks so each row of the character matrix has the same length. |

String array or cell array of character vectors | Each element in the array is the name of a predictor variable. The names must match the entries in `PredictorNames` . |

`'all'` | All predictors are categorical. |

By default, if the predictor data is in a table
(`Tbl`

), `fitrgam`

assumes that a variable is
categorical if it is a logical vector, unordered categorical vector, character array, string
array, or cell array of character vectors. If the predictor data is a matrix
(`X`

), `fitrgam`

assumes that all predictors are
continuous. To identify any other predictors as categorical predictors, specify them by using
the `'CategoricalPredictors'`

name-value argument.

**Example: **`'CategoricalPredictors','all'`

**Data Types: **`single`

| `double`

| `logical`

| `char`

| `string`

| `cell`

`'NumPrint'`

— Number of iterations between diagnostic message printouts`10`

(default) | nonnegative integer scalarNumber of iterations between diagnostic message printouts, specified as a nonnegative integer
scalar. This argument is valid only when you specify `'Verbose'`

as 1.

If you specify `'Verbose',1`

and `'NumPrint',numPrint`

, then
the software displays diagnostic messages every `numPrint`

iterations in the Command Window.

**Example: **`'NumPrint',500`

**Data Types: **`single`

| `double`

`'PredictorNames'`

— Predictor variable namesstring array of unique names | cell array of unique character vectors

Predictor variable names, specified as a string array of unique names or cell array of unique
character vectors. The functionality of `PredictorNames`

depends on the
way you supply the training data.

If you supply

`X`

and`Y`

, then you can use`PredictorNames`

to assign names to the predictor variables in`X`

.The order of the names in

`PredictorNames`

must correspond to the column order of`X`

. That is,`PredictorNames{1}`

is the name of`X(:,1)`

,`PredictorNames{2}`

is the name of`X(:,2)`

, and so on. Also,`size(X,2)`

and`numel(PredictorNames)`

must be equal.By default,

`PredictorNames`

is`{'x1','x2',...}`

.

If you supply

`Tbl`

, then you can use`PredictorNames`

to choose which predictor variables to use in training. That is,`fitrgam`

uses only the predictor variables in`PredictorNames`

and the response variable during training.`PredictorNames`

must be a subset of`Tbl.Properties.VariableNames`

and cannot include the name of the response variable.By default,

`PredictorNames`

contains the names of all predictor variables.A good practice is to specify the predictors for training using either

`'PredictorNames'`

or`formula`

, but not both.

**Example: **`'PredictorNames',{'SepalLength','SepalWidth','PetalLength','PetalWidth'}`

**Data Types: **`string`

| `cell`

`'ResponseName'`

— Response variable name`'Y'`

(default) | character vector | string scalarResponse variable name, specified as a character vector or string scalar.

If you supply

`Y`

, then you can use`'ResponseName'`

to specify a name for the response variable.If you supply

`ResponseVarName`

or`formula`

, then you cannot use`'ResponseName'`

.

**Example: **`'ResponseName','response'`

**Data Types: **`char`

| `string`

`'ResponseTransform'`

— Response transformation`'none'`

(default) | function handleResponse transformation, specified as either `'none'`

or a function
handle. The default is `'none'`

, which means `@(y)y`

,
or no transformation. For a MATLAB function or a function you define, use its function handle for the
response transformation. The function handle must accept a vector (the original response
values) and return a vector of the same size (the transformed response values).

**Example: **Suppose you create a function handle that applies an exponential
transformation to an input vector by using `myfunction = @(y)exp(y)`

.
Then, you can specify the response transformation as
`'ResponseTransform',myfunction`

.

**Data Types: **`char`

| `string`

| `function_handle`

`'Verbose'`

— Verbosity level`0`

(default) | `1`

| `2`

Verbosity level, specified as `0`

, `1`

, or
`2`

. The `Verbose`

value controls the amount of
information that the software displays in the Command Window.

This table summarizes the available verbosity level options.

Value | Description |
---|---|

`0` | The software displays no information. |

`1` | The software displays diagnostic messages every `numPrint` iterations, where
`numPrint` is the `'NumPrint'`
value. |

`2` | The software displays diagnostic messages at every iteration. |

Each line of the diagnostic messages shows the information about each boosting iteration and includes the following columns:

`Type`

— Type of trained trees,`1D`

(predictor trees, or boosted trees for linear terms for predictors) or`2D`

(interaction trees, or boosted trees for interaction terms for predictors)`NumTrees`

— Number of trees per linear term or interaction term that`fitrgam`

added to the model so far`Deviance`

— Deviance of the model`RelTol`

— Relative change of model predictions: $${\left({\widehat{y}}_{k}-{\widehat{y}}_{k-1}\right)}^{\prime}\left({\widehat{y}}_{k}-{\widehat{y}}_{k-1}\right)/{\widehat{y}}_{k}{}^{\prime}{\widehat{y}}_{k}$$, where $${\widehat{y}}_{k}$$ is a column vector of model predictions at iteration*k*`LearnRate`

— Learning rate used for the current iteration

**Example: **`'Verbose',1`

**Data Types: **`single`

| `double`

`'Weights'`

— Observation weights`ones(size(X,1),1)`

(default) | vector of scalar values | name of variable in `Tbl`

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'`

.

`fitrgam`

normalizes the values of `Weights`

to sum to 1.

**Data Types: **`single`

| `double`

| `char`

| `string`

`'CrossVal'`

— Flag to train cross-validated model`'off'`

(default) | `'on'`

Flag to train a cross-validated model, specified as `'on'`

or `'off'`

.

If you specify `'on'`

, then the software trains a
cross-validated model with 10 folds.

You can override this cross-validation setting using the
`'CVPartition'`

, `'Holdout'`

,
`'KFold'`

, or `'Leaveout'`

name-value argument. You can use only one cross-validation name-value
argument at a time to create a cross-validated model.

Alternatively, cross-validate after creating a model by passing
`Mdl`

to `crossval`

.

**Example: **`'Crossval','on'`

`'CVPartition'`

— Cross-validation partition`[]`

(default) | `cvpartition`

partition objectCross-validation partition, specified as a `cvpartition`

partition object
created by `cvpartition`

. The partition object
specifies the type of cross-validation and the indexing for the training and validation
sets.

To create a cross-validated model, you can specify only one of these four name-value
arguments: `CVPartition`

, `Holdout`

,
`KFold`

, or `Leaveout`

.

**Example: **Suppose you create a random partition for 5-fold cross-validation on 500
observations by using `cvp = cvpartition(500,'KFold',5)`

. Then, you can
specify the cross-validated model by using
`'CVPartition',cvp`

.

`'Holdout'`

— Fraction of data for holdout validationscalar value in the range (0,1)

Fraction of the data used for holdout validation, specified as a scalar value in the range
(0,1). If you specify `'Holdout',p`

, then the software completes these
steps:

Randomly select and reserve

`p*100`

% of the data as validation data, and train the model using the rest of the data.Store the compact, trained model in the

`Trained`

property of the cross-validated model.

To create a cross-validated model, you can specify only one of these four name-value
arguments: `CVPartition`

, `Holdout`

,
`KFold`

, or `Leaveout`

.

**Example: **`'Holdout',0.1`

**Data Types: **`double`

| `single`

`'KFold'`

— Number of folds`10`

(default) | positive integer value greater than 1Number of folds to use in a cross-validated model, specified as a positive integer value
greater than 1. If you specify `'KFold',k`

, then the software completes
these steps:

Randomly partition the data into

`k`

sets.For each set, reserve the set as validation data, and train the model using the other

`k`

– 1 sets.Store the

`k`

compact, trained models in a`k`

-by-1 cell vector in the`Trained`

property of the cross-validated model.

To create a cross-validated model, you can specify only one of these four name-value
arguments: `CVPartition`

, `Holdout`

,
`KFold`

, or `Leaveout`

.

**Example: **`'KFold',5`

**Data Types: **`single`

| `double`

`'Leaveout'`

— Leave-one-out cross-validation flag`'off'`

(default) | `'on'`

Leave-one-out cross-validation flag, specified as `'on'`

or
`'off'`

. If you specify `'Leaveout','on'`

, then
for each of the *n* observations (where *n* is the
number of observations, excluding missing observations, specified in the
`NumObservations`

property of the model), the software completes
these steps:

Reserve the one observation as validation data, and train the model using the other

*n*– 1 observations.Store the

*n*compact, trained models in an*n*-by-1 cell vector in the`Trained`

property of the cross-validated model.

`CVPartition`

, `Holdout`

,
`KFold`

, or `Leaveout`

.

**Example: **`'Leaveout','on'`

`Mdl`

— Trained generalized additive model`RegressionGAM`

model object | `RegressionPartitionedGAM`

cross-validated model objectTrained generalized additive model, returned as one of the model objects in this table.

Model Object | Cross-Validation Options to Train Model Object | Ways to Predict Responses Using Model Object |
---|---|---|

`RegressionGAM` | None | Use `predict` to predict responses for new observations, and use
`resubPredict` to predict responses for training
observations. |

`RegressionPartitionedGAM` | Specify the name-value argument `KFold` ,
`Holdout` , `Leaveout` ,
`CrossVal` , or `CVPartition` | Use `kfoldPredict` to predict responses
for observations that `fitrgam` holds out during training.
`kfoldPredict` predicts a response for every observation
by using the model trained without that observation. |

To reference properties of `Mdl`

, use dot notation. For example,
enter `Mdl.Interactions`

in the Command Window to display the
interaction terms in `Mdl`

.

A generalized additive model (GAM) is an interpretable model that explains a response variable using a sum of univariate and bivariate shape functions of predictors.

`fitrgam`

uses a boosted tree as a shape function for each predictor
and, optionally, each pair of predictors; therefore, the function can capture a nonlinear
relation between a predictor and the response variable. Because contributions of individual
shape functions to the prediction (response value) are well separated, the model is easy to
interpret.

The standard GAM uses a univariate shape function for each predictor.

$$\begin{array}{l}y~N\left(\mu ,{\sigma}^{2}\right)\\ g(\mu )=\mu =c+\text{}{f}_{1}({x}_{1})+\text{}{f}_{2}({x}_{2})+\cdots +{f}_{p}({x}_{p}),\end{array}$$

where *y* is a response variable that follows the normal
distribution with mean *μ* and standard deviation *σ*. *g*(*μ*) is an identity link function, and *c* is an intercept
(constant) term.
*f _{i}*(

You can include interactions between predictors in a model by adding bivariate shape functions of important interaction terms to the model.

$$\mu =c+\text{}{f}_{1}({x}_{1})+\text{}{f}_{2}({x}_{2})+\cdots +{f}_{p}({x}_{p})+{\displaystyle \sum _{i,j\in \{1,2,\cdots ,p\}}{f}_{ij}({x}_{i}{x}_{j})},$$

where
*f _{ij}*(

`fitrgam`

finds important interaction terms based on the
*p*-values of *F*-tests. For details, see Interaction Term Detection.

Deviance is a generalization of the residual sum of squares. It measures the goodness of fit compared to the saturated model.

The deviance of a fitted model is twice the difference between the loglikelihoods of the model and the saturated model:

-2(log*L* -
log*L _{s}*),

where *L* and
*L _{s}* are the likelihoods of the fitted model and
the saturated model, respectively. The saturated model is the model with the maximum number
of parameters that you can estimate.

`fitrgam`

uses the deviance to measure the goodness of model fit
and finds a learning rate that reduces the deviance at each iteration. Specify
`'Verbose'`

as 1 or 2 to display the deviance and learning rate in
the Command Window.

`fitrgam`

fits a generalized additive model using a gradient
boosting algorithm (Least-Squares Boosting).

`fitrgam`

first builds sets of predictor trees (boosted trees for
linear terms for predictors) and then builds sets of interaction trees (boosted trees for
interaction terms for predictors). The boosting algorithm iterates for at most
`'NumTreesPerPredictor'`

times for predictor trees, and then iterates
for at most `'NumTreesPerInteraction'`

times for interaction
trees.

For each boosting iteration, `fitrgam`

builds a set of predictor trees with the initial learning rate `'InitialLearnRateForPredictors'`

, or builds a set of interaction trees with the initial learning rate `'InitialLearnRateForInteractions'`

.

When building a set of trees, the function trains one tree at a time. It fits a tree to the residual that is the difference between the response and the aggregated prediction from all trees grown previously. To control the boosting learning speed, the function shrinks the tree by the learning rate and then adds the tree to the model and updates the residual.

Updated model = current model + (learning rate)·(new tree)

Updated residual = current residual – (learning rate)·(response explained by new tree)

If adding the set of trees improves the model fit (that is, reduces the deviance of the fit), then

`fitrgam`

moves to the next iteration.Otherwise,

`fitrgam`

halves the learning rate and uses it to update the model and residual. The function continues to halve the learning rate until it finds a rate that improves the model fit.If the function cannot find such a learning rate for predictor trees, then it stops boosting iterations for linear terms and starts boosting iterations for interaction terms.

If the function cannot find such a learning rate for interaction trees, then it terminates the model fitting.

You can determine why training stopped by checking the

`ReasonForTermination`

property of the trained model.

For each pairwise interaction term
*x _{i}*

`formula`

or `'Interactions'`

), the
software performs an To speed up the process, `fitrgam`

bins numeric predictors into at
most 8 equiprobable bins. The number of bins can be less than 8 if a predictor has fewer
than 8 unique values. The *F*-test examines the null hypothesis that the
bins created by *x _{i}* and

`fitrgam`

builds a set of interaction trees using the terms whose
*p*-values are not greater than the `'MaxPValue'`

value. You can use the default `'MaxPValue'`

value `1`

to build interaction trees using all terms specified by `formula`

or
`'Interactions'`

.

`fitrgam`

adds interaction terms to the model in the order of
importance based on the *p*-values. Use the
`Interactions`

property of the returned model to check the order of
the interaction terms added to the model.

[1] Lou, Yin, Rich Caruana, and Johannes Gehrke. "Intelligible Models for Classification and Regression." *Proceedings of the 18th ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD ’12).* Beijing, China: ACM Press, 2012, pp. 150–158.

[2] Lou, Yin, Rich Caruana, Johannes Gehrke, and Giles Hooker. "Accurate Intelligible Models with Pairwise Interactions." *Proceedings of the 19th ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD ’13)* Chicago, Illinois, USA: ACM Press, 2013, pp. 623–631.

`addInteractions`

| `predict`

| `RegressionGAM`

| `RegressionPartitionedGAM`

| `resume`

You have a modified version of this example. Do you want to open this example with your edits?

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.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- 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)