# Multiple linear regression in MATLAB

Which is the easier way to perform multiple linear regression in MATLAB given that my dataset consists of 384 explanatory variables and 1 dependent variable? In fact, I need to compute coefficients, corresponding residuals, as well as the generalization error when testing this linear model using, 30 say, examples not in the training set.

I think that this function works fine for me, but I am not sure how to make it work with a big number of explanatory variables.

I know that there should be a more efficient way to construct such a model, but this is just a preliminary task of my research.

All ideas are welcome.

EDIT: I think I found it. Could you please confirm that the following is correct?

X = DATA(1:101,1:99);
[M,N] = size(X);
y = DATA(1:101,100);
X = [ones(M,1) X];
b = regress(y,X);


BUT, what about the residuals and the generalization error?

-
A good way to check things like this is to simulate the data using known coefficients and very small random error terms, and then check that the output is consistent with the simulation. –  whuber Jul 21 '12 at 19:16

Why don't you simulate your model on the validation set? As I understand that is exactly generalization error.

X = DATA(1:101,1:99);
[M,N] = size(X);
y = DATA(1:101,100);
%X = [ones(M,1) X]; - this thing is done automatically
r = randperm(size(X, 1));
trainX = X(r(1:70,:), :);
trainY = y(r(1:70,;), :);
testX = X(r(71:end,:),:);
testY = y(r(71:end,:),:);
model = LinearModel.fit(trainX, trainY);

generalizationError = mse(model.predict(testX) - testY)


Also model variable provides lots of info about the regression.

-
this procedure should be repeated a sufficient number of times to estimate generalization performance (the value will likely differ for each split of the data set). –  jank Oct 15 at 15:26

Did you check mvregress command?

load flu

% response: regional queries
y = double(flu(:,2:end-1));

% predictor: national CDC estimates
x = flu.WtdILI;
[nobs,nregions] = size(y);

% Create and fit model with separate intercepts but
% common slope
X = cell(nobs,1);
for j=1:nobs
X{j} = [eye(nregions), repmat(x(j),nregions,1)];
end
[beta,sig,resid,vars,loglik] = mvregress(X,y);

-
Both regress and mvregress work for copefficients and residuals, but what about the generalization error? how to compute that? –  user2295350 Jul 21 '12 at 23:40
how about sum(resid) or sum(resid.*resid) en.wikipedia.org/wiki/Residual_sum_of_squares –  s.s.o Jul 21 '12 at 23:45
I am a bit confused. As generalization error, the paper gives a series data with length the same as the number of testing points. Any ideas?? –  user2295350 Jul 21 '12 at 23:58
lets say you have 100 data points you use 70 of them for the modeling and 30 for validation. you use those 70 for calculation of model generalization error via model residuals. –  s.s.o Jul 22 '12 at 0:20
the difference between 70 observations and 70 responses is the residuals. the squared sum of the residuals are the general model error. the matlab code is just sum(resid.*resid). but note that sum squared error is just one of the ways to calculate general model error. –  s.s.o Jul 22 '12 at 1:36