www.gusucode.com > stats 源码程序 matlab案例代码 > stats/FitLinearRegressionUsingDatainTableExample.m
%% Fit Linear Regression Using Data in Table %% % Load the sample data. load carsmall %% % Store the variables in a table. tbl = table(Weight,Acceleration,MPG,'VariableNames',{'Weight','Acceleration','MPG'}); %% % Display the first five rows of the table. tbl(1:5,:) %% % Fit a linear regression model for miles per gallon (MPG). lm = fitlm(tbl,'MPG~Weight+Acceleration') %% % This syntax uses Wilkinson notation to specify the |modelspec|. %% % The model |'MPG~Weight+Acceleration'| in this example is equivalent to % fitting the model using |'linear'| as |modelspec|. For example, lm2 = fitlm(tbl,'linear'); %% % When you use a character vector as |modelspec| and do not specify the response variable, % |fitlm| by default accepts the last variable in |tbl| as the response % variable and the other variables as the predictor variables. If there % are any categorical variables and you use |'linear'| as the |modelspec|, % then you must explicitly specify those variables as categorical variables % using the |CategoricalVars| name-value pair argument.