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

3 views (last 30 days)
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 = LinearModel.fit(X, 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
disp(myFit)
% See all the methods available for myFit
methods(myFit)
% Retrieve the statistics of interest
R_Squared = myFit.Rsquared.Ordinary
Adj_R_Squared = myFit.Rsquared.Adjusted
SE = myFit.Coefficients.SE
  2 Comments
MD Mahmudul Haque
MD Mahmudul Haque on 20 Mar 2012
Hi Richard, thanks for your answer. I can get the required statistics for an individual run by your code. But if you see my question, i am running inside the loop. Loop has been defined to run required times by excluding the 12 months data each time. In my case, loop run 4 times, as the data length is only 48 row. I get the matrix of coefficient of four run at a time. My question is, is it possible to get the other statistics in that matrix or in a separate matrix for those four runs together.
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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!