Transferring Linear Models to excel/csv

조회 수: 4 (최근 30일)
Kanak Gupta
Kanak Gupta 2020년 8월 14일
댓글: Kanak Gupta 2020년 8월 17일
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

채택된 답변

Abdolkarim Mohammadi
Abdolkarim Mohammadi 2020년 8월 15일
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
Abdolkarim Mohammadi
Abdolkarim Mohammadi 2020년 8월 16일
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.
Kanak Gupta
Kanak Gupta 2020년 8월 17일
Oohh, I get it now. Thank you!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by