Transferring Linear Models to excel/csv
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Kanak Gupta
il 14 Ago 2020
Commentato: Kanak Gupta
il 17 Ago 2020
I am running multiple linear regressions with one X variable and 49 Y's. So I now have 49 different linear models (which I put into an array thoguh that isn't of much help in visually consolidating them). I want to put all the linear models into an xls/csv sheet, organized by variable, but the overly complicated structure of the Linear Model object has made it impossible for me to figure out a way to do so. My final resort may be manually building a table with each variable vs lm parameter but that method comes with its own snags, and there has got to be a more efficient way to do this. I also used diary but it's a very poor substitute (as the file in txt and the formatting is illegible). Help!
diary on
for i=2:50
XY = DataFinal{:,[1,i]};
%removing rows with NaN values in Y variable all columns have some missing data
XY(any(isnan(XY), 2), :) = [];
%creating linear model
Fit = fitlm(XY(:,1),XY(:,2))
disp(DataFinal.Properties.VariableNames{i})
disp(Fit)
%Adding significant pValue Y-variables to array pSig
if Fit.Coefficients.pValue(2) <= 0.05
VN=DataFinal.Properties.VariableNames{i};
pVal=Fit.Coefficients.pValue(2);
pSig=[pSig;VN];
pValSig=[pValSig;pVal];
end
%Adding fit model to array
Fitc{1,i-1} = Fit;
end
Sigs = [pSig,pValSig];
TSigs = array2table([pSig,pValSig], 'VariableNames',{'Significant Variables', 'p Values'})
diary off
0 Commenti
Risposta accettata
Abdolkarim Mohammadi
il 15 Ago 2020
It's better to first design a 49x1 cell array and write the outputs of each experiment to the corresponding element of the array, and then iterate over the entire 49x1 cell array to write the contents of each cell to Excel using writecell. You should increment the Range in writecell by a constant number which is essentially larger than the height of each table.
3 Commenti
Abdolkarim Mohammadi
il 16 Ago 2020
Tables to be copid directly to Excel are stored as cell arrays. For example, MATLAB returns the ANOVA table as cell. You should write the first table (first cell) in A1, then, increment the row number to 11 and write the next table in A11 and so on. You should iterate over the entire cell array and your code should look like this:
RowNum = 1;
RowIncrement = 10;
for i1 = 1:49
writecell (Results{i1},'Sheet','Sheet1', 'Range',['A',RowNum]);
RowNum = RowNum + RowIncrement;
end
You can do the same thing for the scalar output, say rmse, in column B.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!