RegressionLinear
R2026bLinear regression model for high-dimensional data
Description
RegressionLinear is a trained linear model object for
regression; the linear model is a support vector machine regression (SVM) or linear
regression model. fitrlinear fits a
RegressionLinear model by minimizing the objective function using
techniques that reduce computation time for high-dimensional data sets (for example,
stochastic gradient descent). The regression loss plus the regularization term compose
the objective function.
Unlike other regression models, and for economical memory usage,
RegressionLinear model objects do not store the training data.
However, they do store, for example, the estimated linear model coefficients, estimated
coefficients, and the regularization strength.
You can use trained RegressionLinear models to predict responses for
new data. For details, see predict.
Creation
Create a RegressionLinear object by using fitrlinear.
Properties
Linear Regression Properties
This property is read-only.
Half of the width of the epsilon insensitive band, returned as a nonnegative scalar.
If Learner is not 'svm', then
Epsilon is an empty array
([]).
Data Types: single | double
This property is read-only.
Regularization term strength, returned as a nonnegative scalar or vector of nonnegative values.
Data Types: double | single
This property is read-only.
Linear regression model type, returned as
'leastsquares' or
'svm'.
In this table,
β is a vector of p coefficients.
x is an observation from p predictor variables.
b is the scalar bias.
| Value | Algorithm | Loss Function | FittedLoss Value |
|---|---|---|---|
'svm' | Support vector machine regression | Epsilon insensitive: | 'epsiloninsensitive' |
'leastsquares' | Linear regression through ordinary least squares | Mean squared error (MSE): | 'mse' |
This property is read-only.
Linear coefficient estimates, returned as a numeric vector with length
equal to the number of expanded predictors (see
ExpandedPredictorNames).
Data Types: double
This property is read-only.
Estimated bias term or model intercept, returned as a numeric scalar.
Data Types: double
This property is read-only.
Loss function used to fit the model, returned as
'epsiloninsensitive' or
'mse'.
| Value | Algorithm | Loss Function | Learner Value |
|---|---|---|---|
'epsiloninsensitive' | Support vector machine regression | Epsilon insensitive: | 'svm' |
'mse' | Linear regression through ordinary least squares | Mean squared error (MSE): | 'leastsquares' |
This property is read-only.
Parameters used for training the RegressionLinear model, returned as a
structure.
Access fields of ModelParameters using dot notation. For example,
access the relative tolerance on the linear coefficients and the bias term by using
Mdl.ModelParameters.BetaTolerance.
Data Types: struct
This property is read-only.
Complexity penalty type, returned as 'lasso (L1)' or
'ridge (L2)'.
The software composes the objective function for minimization from the sum of the
average loss function (see FittedLoss) and a regularization value
from this table.
| Value | Description |
|---|---|
'lasso (L1)' | Lasso (L1) penalty: |
'ridge (L2)' | Ridge (L2) penalty: |
λ specifies the regularization term strength (see
Lambda).
The software excludes the bias term (β0) from the regularization penalty.
Data Types: char
Data Properties
This property is read-only.
Categorical predictor
indices, returned as a vector of positive integers. CategoricalPredictors
contains index values indicating that the corresponding predictors are categorical. The index
values are between 1 and p, where p is the number of
predictors used to train the model. If none of the predictors are categorical, then this
property is empty ([]).
Data Types: single | double
This property is read-only.
Predictor names in order of their appearance in the predictor data, returned as a cell
array of character vectors. The length of PredictorNames is equal
to the number of variables in the training data X or
Tbl used as predictor variables.
Data Types: cell
This property is read-only.
Expanded predictor names, returned as a cell array of character vectors.
If the model uses encoding for categorical variables, then
ExpandedPredictorNames includes the names that describe the
expanded variables. Otherwise, ExpandedPredictorNames is the same as
PredictorNames.
Data Types: cell
This property is read-only.
Response variable name, returned as a character vector.
Data Types: char
Response transformation function, specified as "none" or a function handle.
ResponseTransform describes how the software transforms raw
response values.
For a MATLAB® function or a function that you define, enter its function handle. For
example, you can enter Mdl.ResponseTransform =
@function, where
function accepts a numeric vector of the
original responses and returns a numeric vector of the same size containing the
transformed responses.
Data Types: char | string | function_handle
Object Functions
incrementalLearner | Convert linear regression model to incremental learner |
lime | Local interpretable model-agnostic explanations (LIME) |
loss | Regression loss for linear regression models |
partialDependence | Compute partial dependence |
plotPartialDependence | Create partial dependence plot (PDP) and individual conditional expectation (ICE) plots |
predict | Predict response of linear regression model |
selectModels | Select fitted regularized linear regression models |
shapley | Shapley values |
update | Update model parameters for code generation |
Examples
Train a linear regression model using SVM, dual SGD, and ridge regularization.
Simulate 10000 observations from this model
is a 10000-by-1000 sparse matrix with 10% nonzero standard normal elements.
e is random normal error with mean 0 and standard deviation 0.3.
rng(1) % For reproducibility
n = 1e4;
d = 1e3;
nz = 0.1;
X = sprandn(n,d,nz);
Y = X(:,100) + 2*X(:,200) + 0.3*randn(n,1);Train a linear regression model. By default, fitrlinear uses support vector machines with a ridge penalty, and optimizes using dual SGD for SVM. Determine how well the optimization algorithm fit the model to the data by extracting a fit summary.
[Mdl,FitInfo] = fitrlinear(X,Y)
Mdl =
RegressionLinear
ResponseName: 'Y'
ResponseTransform: 'none'
Beta: [1000×1 double]
Bias: -0.0056
Lambda: 1.0000e-04
Learner: 'svm'
Properties, Methods
FitInfo = struct with fields:
Lambda: 1.0000e-04
Objective: 0.2725
PassLimit: 10
NumPasses: 10
BatchLimit: []
NumIterations: 100000
GradientNorm: NaN
GradientTolerance: 0
RelativeChangeInBeta: 0.4907
BetaTolerance: 1.0000e-04
DeltaGradient: 1.5816
DeltaGradientTolerance: 0.1000
TerminationCode: 0
TerminationStatus: {'Iteration limit exceeded.'}
Alpha: [10000×1 double]
History: []
FitTime: 0.0626
Solver: {'dual'}
Mdl is a RegressionLinear model. You can pass Mdl and the training or new data to loss to inspect the in-sample mean-squared error. Or, you can pass Mdl and new predictor data to predict to predict responses for new observations.
FitInfo is a structure array containing, among other things, the termination status (TerminationStatus) and how long the solver took to fit the model to the data (FitTime). It is good practice to use FitInfo to determine whether optimization-termination measurements are satisfactory. In this case, fitrlinear reached the maximum number of iterations. Because training time is fast, you can retrain the model, but increase the number of passes through the data. Or, try another solver, such as LBFGS.
Create a linear regression model. Use the model to predict responses for test set observations.
Simulate 10000 observations from this model
is a 10000-by-1000 sparse matrix with 10% nonzero standard normal elements.
e is random normal error with mean 0 and standard deviation 0.3.
rng(1) % For reproducibility
n = 1e4;
d = 1e3;
nz = 0.1;
X = sprandn(n,d,nz);
Y = X(:,100) + 2*X(:,200) + 0.3*randn(n,1);Reserve 5% of the data for testing
rng(1) % For reproducibility
cvp = cvpartition(n,Holdout=0.05)cvp =
Hold-out cross validation partition
NumObservations: 10000
NumTestSets: 1
TrainSize: 9500
TestSize: 500
IsCustom: 0
IsGrouped: 0
IsStratified: 0
Properties, Methods
cvp is a CVPartition object that defines the random partition of n observations into training and test sets.
Train a linear regression model using the training set. For faster training time, orient the predictor data matrix so that the observations are in columns.
idxTrain = training(cvp); X = X'; Mdl = fitrlinear(X(:,idxTrain),Y(idxTrain), ... ObservationsIn="columns");
Predict responses for the test set observations, and compute the mean squared error (MSE).
idxTest = test(cvp); yHat = predict(Mdl,X(:,idxTest),ObservationsIn="columns"); L = loss(Mdl,X(:,idxTest),Y(idxTest),ObservationsIn="columns")
L = 0.1851
The test set MSE is approximately 0.19.
Extended Capabilities
Usage notes and limitations:
When you train a linear regression model by using
fitrlinear, the following restrictions apply.If the predictor data input argument value is a matrix, it must be a full, numeric matrix. Code generation does not support sparse data.
You can specify only one regularization strength, either
"auto"or a nonnegative scalar for theLambdaname-value argument.The value of the
ResponseTransformname-value argument cannot be an anonymous function.Code generation with a coder configurer does not support categorical predictors (
logical,categorical,char,string, orcell). You cannot use theCategoricalPredictorsname-value argument. To include categorical predictors in a model, preprocess them by usingdummyvarbefore fitting the model.
For more information, see Introduction to Code Generation for Statistics and Machine Learning Functions.
Refer to the usage notes and limitations in the C/C++ Code Generation section. The same usage notes and limitations apply to GPU code generation.
The following object functions fully support GPU arrays:
The object functions execute on a GPU if at least one of the following applies:
The model was fitted with GPU arrays.
The predictor data that you pass to the object function is a GPU array.
The response data that you pass to the object function is a GPU array.
The
shapleyobject function partially supports GPU arrays. For more information, see GPU Arrays.
Version History
Introduced in R2016aThe shapley
object function accepts GPU array input arguments when the machine learning model is
a RegressionLinear object, and the shapley function
uses an interventional algorithm
(Method="interventional").
You can fit a RegressionLinear object with GPU arrays by using
fitrlinear. Most
RegressionLinear object functions now support GPU array input
arguments so that they can execute on a GPU. The object functions that do not
support GPU array inputs are incrementalLearner, lime,
shapley,
and update.
See Also
MATLAB Command
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.
Select a Web Site
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: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)