How to extract multiple cfit or fit values into one table

10 次查看(过去 30 天)
Dear all,
So I have a 31x 19 matrix data and I had to fit the first column data (31X1) and the rest of all the 18 columns (31x18).
I wrote a simple if function to do this but my challenge now is about how to extract each fit coefficients (f) including confidence intervals into one Table. I already used coeffvalues but only returns my last f as the ans
Mycode is a follows
r = randi([0, 100], 31,19)
b = (r(:,1));b2=b(:,2:end);
for i= 1:size(b2,2)
% [f, gof_info] = fit(b1,b(:,2),'power1');
f{i}= fit(b, b2(:,i),'power1')
%f_values= coeffvalues(f{i})
end
Your help will be deeply appreciated.
Thanks
E

采纳的回答

Isabelle Levy
Isabelle Levy 2020-12-17
Hi E,
I understand you’re having trouble condensing data that you’ve generated using the fit function. I noticed a few potential typos in your code. See my comments in green:
r = randi([0, 100], 31,19) %Power functions are unable to fit to data where X may contain nonpositive values; change the range from [0-100] to [1-100]
b = (r(:,1));
b2=b(:,2:end); %I think you meant to write b2 = r(:,2:end)
for i= 1:size(b2,2)
f{i}= fit(b, b2(:,i),'power1') %The appropriate syntax for a fit function would be f = fit(b, b2(:,i),'power1')
end
In addition to the coeffvalues function, you will also want to use the confint function to obtain the confidence levels associated with each result. With that being said, consider the revised following revised code (see comment below). The first iteration of the for loop populates the first two columns of an array with the corresponding coefficients and confidence intervals determined by fitting the data. The for loop executes a total of 18 times, so the result is a 3-by-36 array where the first row stores the coefficients and the second two rows store the corresponding lower and upper bounds. The array2table function then converts the array to a table.
  2 个评论
Isabelle Levy
Isabelle Levy 2020-12-17
r = randi([1,100],31,19);
b = (r(:,1));
b2 = r(:,2:end);
array1 = zeros(3,2*size(b2,2));
col=1;
for i= 1:size(b2,2)
f = fit(b, b2(:,i),'power1');
array1(1,col:col+1) = coeffvalues(f);
array1(2:3,col:col+1) = confint(f);
col=col+2;
end
table = array2table(output,'RowNames',{'Coefficients','Lower Bound','Upper Bound'})
I hope this helps!
-Isabelle

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by