MATLAB Answers

How to get regression statistics for several run with different data sets?

4 views (last 30 days)
MD Mahmudul Haque
MD Mahmudul Haque on 20 Mar 2012
I have a data set of 48 row and 5 column. Like the following Year Month Water_Demand Population rainfall, data for 4 years (48 months). Here water_demand is dependent variable and population and rainfall are independent variables. I estimate the regression coefficients four times, each time excluded 12 months from the data set.Here is the code, number_of_lines=length(data); C = zeros(3,4); for n=[1 13 25 37] under_test=data; under_test(n:n+11,:)=[]; B1=under_test(:,3); B2=under_test(:,4); B3=under_test(:,5); x1=[ones(size(B1)),B2,B3]; y1=B1; C(:,n)=regress(y1,x1); end now i want to get standard error, R2, adjusted R2 of those four run. Can any one help plz.thanks.

Answers (1)

Richard Willey
Richard Willey on 20 Mar 2012
Hey there
The following code assumes that you have the 12a release of Statistics Toolbox. (In other words, I am using the new LinearModel function which makes life much much easier)
I'm going to generate some "fake" data. It should be pretty easy to modify this for your own uses.
% Generate a data set
X1 = randn(100,1);
X2 = randn(100,1);
X3 = randn(100,1);
Y = 3 + 5*X1 + 7*X2 + 9*X3 + randn(100,1);
X = [X1 X2 X3];
% Use Linear Model to model Y as a function of X
myFit =, Y);
% Display "myFit"
% Note that R^2, Adjusted R^2, and the standard error of the regression
% coefficients are all part of the default display
% See all the methods available for myFit
% Retrieve the statistics of interest
R_Squared = myFit.Rsquared.Ordinary
Adj_R_Squared = myFit.Rsquared.Adjusted
SE = myFit.Coefficients.SE
Tom Lane
Tom Lane on 20 Mar 2012
In your example you assigned coefficients into C(:,n) each time through the loop. You could do the same with SE, R^2, etc. by assigning into arrays of those. Richard showed how to use the new LinearModel feature to get those results. You could also use regstats to get them.

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!