www.gusucode.com > stats 源码程序 matlab案例代码 > stats/TrainLinearRegressionModelExample.m

    %% Train Linear Regression Model
% Train a linear regression model using SVM, dual SGD, and ridge
% regularization.
%%
% Simulate 10000 observations from this model
%
% $$y = x_{100} + 2x_{200} + e.$$
%
%
% * $X = {x_1,...,x_{1000}}$ 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| 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.