Stepwise fit wont run through loop, can't find a way around the problem.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
You will see from my code below That I load some data, then in a for loop a adjust it according to a pre-determined lag and run a stepwise regression.
The loop runs through 1505 columns of 96 rows. however, the first 47 columns are all NaN (though I need to keep them) and there are other columns full of NaN in there too.
The problem is that I get the following error
"Attempted to access R(1); index out of bounds because numel®=0."
This makes sense, because the first column is all NaN. I have tried getting around the problem using the following
if isnan(Nc)
continue
else
%stepwise regression performed
end
But that results in the following error
"Subscripted assignment dimension mismatch.
Error in Shift_decomposed_data2_hdg (line 101) xxn(:,xip)=xx(:,k);"
So I am stumped and would like a solution if anyone can think of one. Thank you in advance.
Here is the code, Including loading the data
clear all;
clc
lag = load ('lagtime.txt'); %load the lag for each pixel
A1 = load ('Lag_Results\data\Anomalies\TWSA1anomalies.mat');
Approx1x = A1.TWSA1anomalies;
Approx1y = reshape(Approx1x,[1505,96]);
Approx1 = Approx1y';
A2 = load ('Lag_Results\data\Anomalies\TWSA2anomalies.mat');
Approx2x = A2.TWSA2anomalies;
Approx2y = reshape(Approx2x,[1505,96]);
Approx2 = Approx2y';
A3 = load ('Lag_Results\data\Anomalies\TWSA3anomalies.mat');
Approx3x = A3.TWSA3anomalies;
Approx3y = reshape(Approx3x,[1505,96]);
Approx3 = Approx3y';
A4 = load ('Lag_Results\data\Anomalies\TWSA4anomalies.mat');
Approx4x = A4.TWSA4anomalies;
Approx4y = reshape(Approx4x,[1505,96]);
Approx4 = Approx4y';
D1 = load ('Lag_Results\data\Anomalies\TWSD1anomalies.mat');
Det1x = D1.TWSD1anomalies;
Det1y = reshape(Det1x,[1505,96]);
Det1 = Det1y';
D2 = load ('Lag_Results\data\Anomalies\TWSD2anomalies.mat');
Det2x = D2.TWSD2anomalies;
Det2y = reshape(Det2x,[1505,96]);
Det2 = Det2y';
D3 = load ('Lag_Results\data\Anomalies\TWSD3anomalies.mat');
Det3x = D3.TWSD3anomalies;
Det3y = reshape(Det3x,[1505,96]);
Det3 = Det3y';
D4 = load ('Lag_Results\data\Anomalies\TWSD4anomalies.mat');
Det4x = D4.TWSD4anomalies;
Det4y = reshape(Det4x,[1505,96]);
Det4 = Det4y';
NDVIdata = load ('Lag_Results\data\Anomalies\NDVIanomalies.mat');
NDVIx = NDVIdata.NDVIanomalies;
NDVIy = reshape(NDVIx,[1505,96]);
NDVI = NDVIy';
for j = 1:1505
m = lag (1,j)
Nc=NDVI(m+1:end,j);
A1c=Approx1 (1:end-m,j);
A2c=Approx2 (1:end-m,j);
A3c=Approx3 (1:end-m,j);
A4c=Approx4 (1:end-m,j);
D1c=Det1 (1:end-m,j);
D2c=Det2 (1:end-m,j);
D3c=Det3 (1:end-m,j);
D4c=Det4 (1:end-m,j);
xx=[A1c, A2c, A3c, A4c, D1c, D2c, D3c, D4c];
yy = Nc;
%Begin Stepwise Regression
if isnan(Nc)
continue
else
[B,SE,PVAL,INMODEL,STATS,NEXTSTEP,HISTORY]= ...
stepwisefit(xx,yy,'penter',.05);
inApprox1(j)=INMODEL(1);
inApprox2(j)=INMODEL(2);
inApprox3(j)=INMODEL(3);
inApprox4(j)=INMODEL(4);
inDpprox7(j)=INMODEL(5);
inDpprox8(j)=INMODEL(6);
inDpprox9(j)=INMODEL(7);
inDpprox10(j)=INMODEL(8);
sstotApprox1(j)=STATS.SStotal; %calculate R^2
ssresidApprox1(j)=STATS.SSresid;
rsq = 1- ssresidApprox1./sstotApprox1
rsq(rsq==Inf) = NaN %Set Inf to NaN
rmse(j)=STATS.rmse; %Extract rmse
rmse(rmse==Inf) = NaN; %Set Inf to NaN
% repeat regresson only on the sigificant variables
if sum(INMODEL,2)>0
xip=0;
for k=1:8 %9 refers to previous 9 variables including intecept
if INMODEL(1,k)==1
xip=xip+1;
xxn(:,xip)=xx(:,k);
end
end
[Bn,SEn,PVALn,INMODELn,STATSn,NEXTSTEPn,HISTORYn]= ...
stepwisefit(xxn,yy,'penter',.05);
rmsen(j)=STATSn.rmse; %Extract rmse
rmsen(rmse==Inf) = NaN; %Set Inf to NaN
end
end
end
5 commentaires
dpb
le 22 Déc 2015
If'en
length(xip)~=length(k)
then, no, you can't store the k values on the RHS of the assignment into the the xip target locations on the LHS. There are either too many to fit or to few to assign one for each depending upon which way the inequality works.
One way to hold disparate-sized objects/arrays is in a cell array...
Réponses (0)
Voir également
Catégories
En savoir plus sur Descriptive Statistics dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!