Documentación

Esta página aún no se ha traducido para esta versión. Puede ver la versión más reciente de esta página en inglés.

# stepwiselm

Create linear regression model using stepwise regression

## Sintaxis

``mdl = stepwiselm(tbl,modelspec)``
``mdl = stepwiselm(X,y,modelspec)``
``mdl = stepwiselm(___,Name,Value)``

## Descripción

ejemplo

````mdl = stepwiselm(tbl,modelspec)` returns a linear model for the variables in the table or dataset array `tbl` using stepwise regression to add or remove predictors. `stepwiselm` uses forward and backward stepwise regression to determine a final model. At each step, the function searches for terms to add to or remove from the model based on the value of the `'Criterion'` argument. `modelspec` is the starting model for the stepwise procedure.```

ejemplo

````mdl = stepwiselm(X,y,modelspec)` creates a linear model of the responses `y` to the predictor variables in the data matrix `X`, using stepwise regression to add or remove predictors. `modelspec` is the starting model for the stepwise procedure.```

ejemplo

````mdl = stepwiselm(___,Name,Value)` creates a linear model for any of the inputs in the previous syntaxes, with additional options specified by one or more `Name,Value` pair arguments.For example, you can specify the categorical variables, the smallest or largest set of terms to use in the model, the maximum number of steps to take, or the criterion `stepwiselm` uses to add or remove terms.```

## Ejemplos

contraer todo

`load hald`

`hald` contains hardening data for 13 different concrete compositions. `heat` is the heat of hardening after 180 days. `ingredients` is the percentage of each different ingredient in the cement sample.

Fit a linear model to the data. Set the criterion value to enter the model as 0.06.

` mdl = stepwiselm(ingredients,heat,'PEnter',0.06)`
```1. Adding x4, FStat = 22.7985, pValue = 0.000576232 2. Adding x1, FStat = 108.2239, pValue = 1.105281e-06 3. Adding x2, FStat = 5.0259, pValue = 0.051687 4. Removing x4, FStat = 1.8633, pValue = 0.2054 ```
```mdl = Linear regression model: y ~ 1 + x1 + x2 Estimated Coefficients: Estimate SE tStat pValue ________ ________ ______ __________ (Intercept) 52.577 2.2862 22.998 5.4566e-10 x1 1.4683 0.1213 12.105 2.6922e-07 x2 0.66225 0.045855 14.442 5.029e-08 Number of observations: 13, Error degrees of freedom: 10 Root Mean Squared Error: 2.41 R-squared: 0.979, Adjusted R-Squared 0.974 F-statistic vs. constant model: 230, p-value = 4.41e-09 ```

By default, the starting model is the constant model. `stepwiselm` performs forward selection and `x4`, `x1`, and `x2`, respectively, as the corresponding -values are less than the `PEnter` value of 0.06. `stepwiselm` later uses backward elimination and eliminates `x4` from the model because, once `x2` is in the model, the -value of `x4` is higher than the default value of `PRemove`, 0.1.

Perform stepwise regression using variables stored in a dataset array. Specify the starting model using Wilkinson notation, and identify the response and predictor variables using optional arguments.

`load hospital`

The hospital dataset array includes the gender, age, weight, and smoking status of patients.

Fit a linear model with a starting model of a constant term and `Smoker` as the predictor variable. Specify the response variable, `Weight`, and categorical predictor variables, `Sex`, `Age`, and `Smoker`.

```mdl = stepwiselm(hospital,'Weight~1+Smoker',... 'ResponseVar','Weight','PredictorVars',{'Sex','Age','Smoker'},... 'CategoricalVar',{'Sex','Smoker'})```
```1. Adding Sex, FStat = 770.0158, pValue = 6.262758e-48 2. Removing Smoker, FStat = 0.21224, pValue = 0.64605 ```
```mdl = Linear regression model: Weight ~ 1 + Sex Estimated Coefficients: Estimate SE tStat pValue ________ ______ ______ ___________ (Intercept) 130.47 1.1995 108.77 5.2762e-104 Sex_Male 50.06 1.7496 28.612 2.2464e-49 Number of observations: 100, Error degrees of freedom: 98 Root Mean Squared Error: 8.73 R-squared: 0.893, Adjusted R-Squared 0.892 F-statistic vs. constant model: 819, p-value = 2.25e-49 ```

At each step, `stepwiselm` searches for terms to add and remove. At first step, stepwise algorithm adds `Sex` to the model with a -value of 6.26e-48. Then, removes Smoker from the model, since given `Sex` in the model, the variable `Smoker` becomes redundant. `stepwiselm` only includes `Sex` in the final linear model. The weight of the patients do not seem to differ significantly according to age or the status of smoking.

contraer todo

Input data, specified as a table or dataset array. When `modelspec` is a `formula`, it specifies the variables to be used as the predictors and response. Otherwise, if you do not specify the predictor and response variables, the last variable is the response variable and the others are the predictor variables by default.

Predictor variables can be numeric, or any grouping variable type, such as logical or categorical (see Grouping Variables). The response must be numeric or logical.

To set a different column as the response variable, use the `ResponseVar` name-value pair argument. To use a subset of the columns as predictors, use the `PredictorVars` name-value pair argument.

Predictor variables, specified as an n-by-p matrix, where n is the number of observations and p is the number of predictor variables. Each column of `X` represents one variable, and each row represents one observation.

By default, there is a constant term in the model, unless you explicitly remove it, so do not include a column of 1s in `X`.

Tipos de datos: `single` | `double` | `logical`

Response variable, specified as an n-by-1 vector, where n is the number of observations. Each entry in `y` is the response for the corresponding row of `X`.

Tipos de datos: `single` | `double` | `logical`

Starting model for the stepwise regression, specified as one of the following:

• Character vector or string scalar specifying the type of starting model.

ValueStarting Model Type
`'constant'`Model contains only a constant (intercept) term.
`'linear'`Model contains an intercept and linear terms for each predictor.
`'interactions'`Model contains an intercept, linear terms for each predictor, and all products of pairs of distinct predictors (no squared terms).
`'purequadratic'`Model contains an intercept, linear terms, and squared terms for each predictor.
`'quadratic'`Model contains an intercept, linear terms, interactions, and squared terms for each predictor.
`'polyijk'`Model is a polynomial with all terms up to degree `i` in the first predictor, degree `j` in the second predictor, etc. Use numerals `0` through `9`. For example, `'poly2111'` has a constant plus all linear* and product terms, and also contains terms with predictor 1 squared.

If you want to specify the smallest or largest set of terms in the model that `stepwiselm` fits, use the `Lower` and `Upper` name-value pair arguments.

• t-by-(p+1) matrix, namely a terms matrix, specifying terms to include in model, where t is the number of terms and p is the number of predictor variables, and plus one is for the response variable.

• Character vector or string scalar representing a formula in the form

```'Y ~ terms'```,

where the `terms` are in Wilkinson Notation.

### Nota

All the terms `stepwiselm` returns are linear in the coefficients.

Tipos de datos: `char` | `string` | `single` | `double`

### Argumentos de par nombre-valor

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

Ejemplo: `'Criterion','aic','Upper',interactions,'Verbose',1` instructs `stepwiselm` to use the Akaike information criterion, display the action it takes at each step, and include at most the interaction terms in the model.

Categorical variable list, specified as the comma-separated pair consisting of `'CategoricalVars'` and either a string array or cell array of character vectors containing categorical variable names in the table or dataset array `tbl`, or a logical or numeric index vector indicating which columns are categorical.

• If data is in a table or dataset array `tbl`, then, by default, `stepwiselm` treats all categorical values, logical values, character arrays, string arrays, and cell arrays of character vectors as categorical variables.

• If data is in matrix `X`, then the default value of `'CategoricalVars'` is an empty matrix `[]`. That is, no variable is categorical unless you specify it as categorical.

For example, you can specify the observations 2 and 3 out of 6 as categorical using either of the following examples.

Ejemplo: `'CategoricalVars',[2,3]`

Ejemplo: `'CategoricalVars',logical([0 1 1 0 0 0])`

Tipos de datos: `single` | `double` | `logical` | `string` | `cell`

Criterion to add or remove terms, specified as the comma-separated pair consisting of `'Criterion'` and one of the following:

• `'sse'`p-value for an F-test of the change in the sum of squared error by adding or removing the term.

• `'aic'` — Change in the value of Akaike information criterion (AIC).

• `'bic'` — Change in the value of Bayesian information criterion (BIC).

• `'rsquared'` — Increase in the value of R2.

• `'adjrsquared'` — Increase in the value of adjusted R2.

Ejemplo: `'Criterion','bic'`

Observations to exclude from the fit, specified as the comma-separated pair consisting of `'Exclude'` and a logical or numeric index vector indicating which observations to exclude from the fit.

For example, you can exclude observations 2 and 3 out of 6 using either of the following examples.

Ejemplo: `'Exclude',[2,3]`

Ejemplo: `'Exclude',logical([0 1 1 0 0 0])`

Tipos de datos: `single` | `double` | `logical`

Indicator for the constant term (intercept) in the fit, specified as the comma-separated pair consisting of `'Intercept'` and either `true` to include or `false` to remove the constant term from the model.

Use `'Intercept'` only when specifying the model using a character vector or string scalar, not a formula or matrix.

Ejemplo: `'Intercept',false`

Model specification describing terms that cannot be removed from the model, specified as the comma-separated pair consisting of `'Lower'` and one of the options for `modelspec` naming the model.

Ejemplo: `'Lower','linear'`

Number of steps to take, specified as the comma-separated pair consisting of `'NSteps'` and a positive integer.

Tipos de datos: `single` | `double`

Improvement measure for adding a term, specified as the comma-separated pair consisting of `'PEnter'` and a scalar value. The default values are below.

CriterionDefault valueDecision
`'SSE'`0.05If the SSE of the model is smaller than `PEnter`, add the term to the model.
`'AIC'`0If the change in the AIC of the model is smaller than `PEnter`, add the term to the model.
`'BIC'`0If the change in the BIC of the model is smaller than `PEnter`, add the term to the model.
`'Rsquared'`0.1If the increase in the R-squared of the model is larger than `PEnter`, add the term to the model.
`'AdjRsquared'`0If the increase in the adjusted R-squared of the model is larger than `PEnter`, add the term to the model.

For more information on the criteria, see `Criterion` name-value pair argument.

Ejemplo: `'PEnter',0.075`

Predictor variables to use in the fit, specified as the comma-separated pair consisting of `'PredictorVars'` and either a string array or cell array of character vectors of the variable names in the table or dataset array `tbl`, or a logical or numeric index vector indicating which columns are predictor variables.

The string values or character vectors should be among the names in `tbl`, or the names you specify using the `'VarNames'` name-value pair argument.

The default is all variables in `X`, or all variables in `tbl` except for `ResponseVar`.

For example, you can specify the second and third variables as the predictor variables using either of the following examples.

Ejemplo: `'PredictorVars',[2,3]`

Ejemplo: `'PredictorVars',logical([0 1 1 0 0 0])`

Tipos de datos: `single` | `double` | `logical` | `string` | `cell`

Improvement measure for removing a term, specified as the comma-separated pair consisting of `'PRemove'` and a scalar value.

CriterionDefault valueDecision
`'SSE'`0.10If the p-value of the F statistic is larger than `PRemove`, remove the term from the model.
`'AIC'`0.01If the change in the AIC of the model is larger than `PRemove`, remove the term from the model.
`'BIC'`0.01If the change in the BIC of the model is larger than `PRemove`, remove the term from the model.
`'Rsquared'`0.05If the increase in the R-squared value of the model is smaller than `PRemove`, remove the term from the model.
`'AdjRsquared'`-0.05If the increase in the adjusted R-squared value of the model is smaller than `PRemove`, remove the term from the model.

At each step, stepwise algorithm also checks whether any term is redundant (linearly dependent) with other terms in the current model. When any term is linearly dependent with other terms in the current model, it is removed, regardless of the criterion value.

For more information on the criteria, see `Criterion` name-value pair argument.

Ejemplo: `'PRemove',0.05`

Response variable to use in the fit, specified as the comma-separated pair consisting of `'ResponseVar'` and either a character vector or string scalar containing the variable name in the table or dataset array `tbl`, or a logical or numeric index vector indicating which column is the response variable. You typically need to use `'ResponseVar'` when fitting a table or dataset array `tbl`.

For example, you can specify the fourth variable, say `yield`, as the response out of six variables, in one of the following ways.

Ejemplo: `'ResponseVar','yield'`

Ejemplo: `'ResponseVar',`

Ejemplo: `'ResponseVar',logical([0 0 0 1 0 0])`

Tipos de datos: `single` | `double` | `logical` | `char` | `string`

Model specification describing the largest set of terms in the fit, specified as the comma-separated pair consisting of `'Upper'` and one of the options for `modelspec` naming the model.

Ejemplo: `'Upper','quadratic'`

Names of variables, specified as the comma-separated pair consisting of `'VarNames'` and a string array or cell array of character vectors including the names for the columns of `X` first, and the name for the response variable `y` last.

`'VarNames'` is not applicable to variables in a table or dataset array, because those variables already have names.

For example, if in your data, horsepower, acceleration, and model year of the cars are the predictor variables, and miles per gallon (MPG) is the response variable, then you can name the variables as follows.

Ejemplo: `'VarNames',{'Horsepower','Acceleration','Model_Year','MPG'}`

Tipos de datos: `string` | `cell`

Control for display of information, specified as the comma-separated pair consisting of `'Verbose'` and one of the following:

• `0` — Suppress all display.

• `1` — Display the action taken at each step.

• `2` — Also display the actions evaluated at each step.

Ejemplo: `'Verbose',2`

Observation weights, specified as the comma-separated pair consisting of `'Weights'` and an n-by-1 vector of nonnegative scalar values, where n is the number of observations.

Tipos de datos: `single` | `double`

## Output Arguments

contraer todo

Linear model representing a least-squares fit of the response to the data, returned as a `LinearModel` object.

For the properties and methods of the linear model object, `mdl`, see the `LinearModel` class page.

## Más acerca de

contraer todo

### Terms Matrix

A terms matrix is a t-by-(p + 1) matrix specifying terms in a model, where t is the number of terms, p is the number of predictor variables, and plus one is for the response variable.

The value of `T(i,j)` is the exponent of variable `j` in term `i`. Suppose there are three predictor variables `A`, `B`, and `C`:

```[0 0 0 0] % Constant term or intercept [0 1 0 0] % B; equivalently, A^0 * B^1 * C^0 [1 0 1 0] % A*C [2 0 0 0] % A^2 [0 1 2 0] % B*(C^2)```
The `0` at the end of each term represents the response variable. In general,

• If you have the variables in a table or dataset array, then `0` must represent the response variable depending on the position of the response variable. The following example illustrates this using a table.

Load the sample data and define a table.

```load hospital dsa = table(hospital.Sex,hospital.BloodPressure(:,1), ... hospital.Age,hospital.Smoker,'VariableNames', ... {'Sex','BloodPressure','Age','Smoker'});```

Represent the linear model `'BloodPressure ~ 1 + Sex + Age + Smoker'` in a terms matrix. The response variable is in the second column of the table, so the second column of the terms matrix must be a column of 0s for the response variable.

```T = [0 0 0 0;1 0 0 0;0 0 1 0;0 0 0 1] ```
```T = 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1```

Redefine the table.

```dsa = table(hospital.BloodPressure(:,1),hospital.Sex, ... hospital.Age,hospital.Smoker,'VariableNames', ... {'BloodPressure','Sex','Age','Smoker'}); ```

Now, the response variable is the first term in the table. Specify the same linear model, `'BloodPressure ~ 1 + Sex + Age + Smoker'`, using a terms matrix.

`T = [0 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1]`
```T = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1```
• If you have the predictor and response variables in a matrix and column vector, then you must include `0` for the response variable at the end of each term. The following example illustrates this.

Load the sample data and define the matrix of predictors.

```load carsmall X = [Acceleration,Weight]; ```

Specify the model ```'MPG ~ Acceleration + Weight + Acceleration:Weight + Weight^2'``` using a term matrix and fit the model to the data. This model includes the main effect and two-way interaction terms for the variables, `Acceleration` and `Weight`, and a second-order term for the variable, `Weight`.

```T = [0 0 0;1 0 0;0 1 0;1 1 0;0 2 0] ```
```T = 0 0 0 1 0 0 0 1 0 1 1 0 0 2 0 ```

Fit a linear model.

`mdl = fitlm(X,MPG,T)`
```mdl = Linear regression model: y ~ 1 + x1*x2 + x2^2 Estimated Coefficients: Estimate SE tStat pValue (Intercept) 48.906 12.589 3.8847 0.00019665 x1 0.54418 0.57125 0.95261 0.34337 x2 -0.012781 0.0060312 -2.1192 0.036857 x1:x2 -0.00010892 0.00017925 -0.6076 0.545 x2^2 9.7518e-07 7.5389e-07 1.2935 0.19917 Number of observations: 94, Error degrees of freedom: 89 Root Mean Squared Error: 4.1 R-squared: 0.751, Adjusted R-Squared 0.739 F-statistic vs. constant model: 67, p-value = 4.99e-26```

Only the intercept and `x2` term, which correspond to the `Weight` variable, are significant at the 5% significance level.

Now, perform a stepwise regression with a constant model as the starting model and a linear model with interactions as the upper model.

```T = [0 0 0;1 0 0;0 1 0;1 1 0]; mdl = stepwiselm(X,MPG,[0 0 0],'upper',T)```
```1. Adding x2, FStat = 259.3087, pValue = 1.643351e-28 mdl = Linear regression model: y ~ 1 + x2 Estimated Coefficients: Estimate SE tStat pValue (Intercept) 49.238 1.6411 30.002 2.7015e-49 x2 -0.0086119 0.0005348 -16.103 1.6434e-28 Number of observations: 94, Error degrees of freedom: 92 Root Mean Squared Error: 4.13 R-squared: 0.738, Adjusted R-Squared 0.735 F-statistic vs. constant model: 259, p-value = 1.64e-28```

The results of the stepwise regression are consistent with the results of `fitlm` in the previous step.

### Formula

A formula for model specification is a character vector or string scalar of the form ```'Y ~ terms'```

where

• `Y` is the response name.

• `terms` contains

• Variable names

• `+` means include the next variable

• `-` means do not include the next variable

• `:` defines an interaction, a product of terms

• `*` defines an interaction and all lower-order terms

• `^` raises the predictor to a power, exactly as in `*` repeated, so `^` includes lower order terms as well

• `()` groups terms

### Nota

Formulas include a constant (intercept) term by default. To exclude a constant term from the model, include `-1` in the formula.

For example,

`'Y ~ A + B + C'` means a three-variable linear model with intercept.
```'Y ~ A + B + C - 1'``` is a three-variable linear model without intercept.
`'Y ~ A + B + C + B^2'` is a three-variable model with intercept and a `B^2` term.
```'Y ~ A + B^2 + C'``` is the same as the previous example because `B^2` includes a `B` term.
```'Y ~ A + B + C + A:B'``` includes an `A*B` term.
```'Y ~ A*B + C'``` is the same as the previous example because ```A*B = A + B + A:B```.
`'Y ~ A*B*C - A:B:C'` has all interactions among `A`, `B`, and `C`, except the three-way interaction.
```'Y ~ A*(B + C + D)'``` has all linear terms, plus products of `A` with each of the other variables.

### Wilkinson Notation

Wilkinson notation describes the factors present in models. The notation relates to factors present in models, not to the multipliers (coefficients) of those factors.

Wilkinson NotationFactors in Standard Notation
`1`Constant (intercept) term
`A^k`, where `k` is a positive integer`A`, `A2`, ..., `Ak`
`A + B``A`, `B`
`A*B``A`, `B`, `A*B`
`A:B``A*B` only
`-B`Do not include `B`
`A*B + C``A`, `B`, `C`, `A*B`
`A + B + C + A:B``A`, `B`, `C`, `A*B`
`A*B*C - A:B:C``A`, `B`, `C`, `A*B`, `A*C`, `B*C`
`A*(B + C)``A`, `B`, `C`, `A*B`, `A*C`

Statistics and Machine Learning Toolbox™ notation always includes a constant term unless you explicitly remove the term using `-1`.

## Sugerencias

• You cannot use robust regression with stepwise regression. Check your data for outliers before using `stepwiselm`.

• For other methods such as `anova`, or properties of the `LinearModel` object, see `LinearModel`.

• After training a model, you can generate C/C++ code that predicts responses for new data. Generating C/C++ code requires MATLAB® Coder™ . For details, see Introduction to Code Generation.

## Algoritmos

Stepwise regression is a systematic method for adding and removing terms from a linear or generalized linear model based on their statistical significance in explaining the response variable. The method begins with an initial model, specified using `modelspec`, and then compares the explanatory power of incrementally larger and smaller models.

MATLAB uses forward and backward stepwise regression to determine a final model. At each step, the method searches for terms to add to or remove from the model based on the value of the `'Criterion'` argument. The default value of `'Criterion'` is `'sse'`, and in this case, `stepwiselm` uses the p-value of an F-statistic to test models with and without a potential term at each step. If a term is not currently in the model, the null hypothesis is that the term would have a zero coefficient if added to the model. If there is sufficient evidence to reject the null hypothesis, the term is added to the model. Conversely, if a term is currently in the model, the null hypothesis is that the term has a zero coefficient. If there is insufficient evidence to reject the null hypothesis, the term is removed from the model.

Here is how stepwise proceeds when `'Criterion'` is `'sse'`:

1. Fit the initial model.

2. Examine a set of available terms not in the model. If any of these terms have p-values less than an entrance tolerance (that is, if it is unlikely that they would have zero coefficient if added to the model), add the one with the smallest p-value and repeat this step; otherwise, go to step 3.

3. If any of the available terms in the model have p-values greater than an exit tolerance (that is, the hypothesis of a zero coefficient cannot be rejected), remove the one with the largest p-value and go to step 2; otherwise, end.

At any stage, the function will not add a higher-order term if the model does not also include all lower-order terms that are subsets of it. For example, it will not try to add the term `X1:X2^2` unless both `X1` and `X2^2` are already in the model. Similarly, the function will not remove lower-order terms that are subsets of higher-order terms that remain in the model. For example, it will not examine to remove `X1` or `X2^2` if `X1:X2^2` stays in the model.

The default for `stepwiseglm` is `'Deviance'` and it follows a similar procedure for adding or removing terms.

There are several other criteria available, which you can specify using the `'Criterion'` argument. You can use the change in the value of the Akaike information criterion, Bayesian information criterion, R-squared, adjusted R-squared as a criterion to add or remove terms.

Depending on the terms included in the initial model and the order in which terms are moved in and out, the method might build different models from the same set of potential terms. The method terminates when no single step improves the model. There is no guarantee, however, that a different initial model or a different sequence of steps will not lead to a better fit. In this sense, stepwise models are locally optimal, but might not be globally optimal.

## Alternative Functionality

You can construct a model using `fitlm`, and then manually adjust the model using `step`, `addTerms`, or `removeTerms`.