How do I perform re-regression in Matlab?
I have an excel file that contains 5 columns and 48 rows (data on water consumption, population and rainfall for four years (1997-2000) of each month)
Year Month Water_Demand Population Rainfall
1997 1 355 4500 25
1997 2 375 5000 20
1997 3 320 5200 21
.............% rest of the month data of year 1997.
1997 12 380 6000 24
1998 1 390 6500 23
1998 2 370 6700 20
............. % rest of the month data of year 1998
1998 12 400 6900 19
1999 1
1999 2
.............% rest of the month data of year 1997 and 2000
2000 12 390 7000 20
I want to do multiple linear regression in MATLAB. Here the dependent variable is water demand and the independent variable is population and rainfall. I wrote the code for this for all 48 lines
A1=data(:,3);
A2=data(:,4);
A3=data(:,5);
x=[ones(size(A1)),A2,A3];
y=A1;
b=regress(y,x);
yfit=b(1)+b(2).*A2+b(3).*A3;
Now I want to do a repetition. First, I want to exclude row number 1 (i.e. exclude data for 1997, month 1) and perform regression on the rest of the 47 rows. Then I want to exclude row number 2 and do a regression with the data of row number 1 and row 3-48. Then I want to exclude row number 3 and do a regression with the data of row number 1-2 and row 4-48. There are always 47 rows of data as I exclude one row in each run. Finally, I want to get a table of the regression coefficient and yfit of each run.
source to share
The simplest way I can think of is to create a for loop and a temporary "under test" matrix which is exactly the one you have, without the row you want to exclude, like
C = zeros(3,number_of_lines);
for n = 1:number_of_lines
under_test = data;
% this excludes the nth line of the matrix
under_test(n,:) = [];
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
I'm sure you can optimize this using some of the matlab functions that work with vectors, without using a for loop. But I think in just 48 lines it should be fast enough.
source to share