MATLAB Answers

Nonlinear regression fits on data sets in a cell array?

1 view (last 30 days)
mel1708 on 6 Jan 2020
Answered: Guillaume on 6 Jan 2020
I have a cell array with multiple data sets like the following..
Dose (Gy) SF
0.48300 0.73753
0.99124 0.50232
2.01313 0.33210
2.99717 0.19733
4.01905 0.10447
6.03759 0.01870
So I have imported several of these data sets and I've them under the names dose{k} and sf{k} and I am performing operations on them using a for loop over k. What I am having trouble with is fitting a Rayleigh distribution (i.e. an equation) to each data set using nonlinear regression (fitnlm or nlinfit commands) by looping over k. This is my code..
% ==============================
for k = 1:length(theFiles)'
tbl = table(dose{k}, sf{k});
modelfun = @(sigma2,x) exp(-x.^2 / (2 * sigma2(1)));
beta0 = 1;
mdlRayl{k} = nlinfit(dose{k},sf{k}, modelfun, beta0);
%mdlRayl{k} = fitnlm(tbl, modelfun, beta0);
sigma2Estimate{k} = mdlRayl{k}.Coefficients{:, 'Estimate'}
sigma{k} = sqrt(sigma2Estimate{k});
yfitRayl{k} = (dose{k} / sigma{k}.^2) * exp(-dose{k} / (2 * sigma{k}^2));
hold on
h = errorbar(dose{k},sf{k},errs{k},'Marker','none','LineStyle','none','Color',[col(k,1), col(k,2), col(k,3)],'HandleVisibility','off');
xlabel('Dose (Gy)'); ylabel('Surviving Fraction'); grid on; legend show;
hold off
It has many problems.. it won't let me index tbl so I can have a whole new table everytime I perform a fit for each k, and therefore the fit is not being performed on each data set. Is anyone able to see where I am going wrong?


KSSV on 6 Jan 2020
Try tbl{k} instead of tbl(k). By the way tbl is not being used anywhere.
mel1708 on 6 Jan 2020
I actually copied the version of the code I was playing around with earlier today, oops! I've edited the question now so it's got the right version of the code I'm asking about.
I have tried brace indexing tbl but I end up getting the error message "Subscripting a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts) is not supported. Use a row subscript and a variable subscript."

Sign in to comment.

Answers (1)

Guillaume on 6 Jan 2020
For the problem with the tables:
Before the loop:
fittables = cell(1, numel(theFiles)); %preallocate cell array to store all the tables. Use a more descriptive name than just tbl
for k = 1:numel(theFiles) %Prefer numel to length. You had a transpose here which was completely pointless
fittables{k} = table(dose{k}, sf{k}, 'VariableNames', {'Dose', 'SF'}); %I'd recommend overriding the default variable names
I don't have the required toolbox so can't really help with the rest. These lines look suspicious:
mdlRayl{k} = nlinfit(dose{k},sf{k}, modelfun, beta0);
sigma2Estimate{k} = mdlRayl{k}.Coefficients{:, 'Estimate'};
According to the doc, nlinfit returns a vector, so mdlRayl{k} would be a vector, which can't be indexed with .Coefficients. .Coefficients{:, 'Estimate'} imply that mdlRayl{k} should be a table.
Note: instead of having one table per file, all stored in a cell array you may find it easier to have just one table with an additional variable such as FileNo. There are many aggregration functions in matlab that allow you to perform the same operation to grouped data, eg. grouptransform.


Sign in to comment.

Sign in to answer this question.