I figured out the first two. For the last, I believe I will use an excel merge type of function
I added Mdl.PredictorNames. And for my error issues, I used try catch.
outtable = array2table(outdata,'VariableNames',Mdl.PredictorNames)
for K = 1 : nfiles
try
thisfile = filenames{K};
infile = fullfile(indir, thisfile);
outfile = fullfile(outdir, thisfile);
outfile2 = fullfile(outdir2, thisfile);
p = readtable(infile,'PreserveVariableNames',true)
dsa = p;
X=dsa(:,ismember(dsa.Properties.VariableNames, {'Manhole','Catch','BackUp','PRCP'}))
Y=dsa(:,ismember(dsa.Properties.VariableNames, {'Street'}))
t = templateTree('NumVariablesToSample','all',...
'PredictorSelection','interaction-curvature','Surrogate','on');
rng(1); % For reproducibility
Mdl = fitrensemble(X,Y,'Method','Bag','NumLearningCycles',200, ...
'Learners',t);
yHat = oobPredict(Mdl);
R2 = corr(Mdl.Y,yHat)^2
impOOB = oobPermutedPredictorImportance(Mdl);
impOOB(impOOB<0) = 0;
impOOB = impOOB./sum(impOOB)
outdata = impOOB;
outdata2 = R2;
outtable = array2table(outdata,'VariableNames',Mdl.PredictorNames)
outtable2 = array2table(outdata2)
writetable(outtable, outfile); %needs R2019b or later %the sub-table!
writetable(outtable2, outfile2);
catch
fprintf('Inconsistent data in iteration %s, skipped.\n', K);
end
end
Unrecognized function or variable 'nfiles'.