Non linear mixed effects model - Fitting Time Activity Curve

3 views (last 30 days)
Hello MATLAB users!
I'm facing some issues with fitting the data I have in a excel spreadsheet. The Excel table contains 16 columns and 150 rows, excluding the header. From left to right, the columns are Times, Kidneys_P1, Kidneys_P2, Kidneys_P3, Kidneys_P4, Kidneys_P5, Tumor_P1, Tumor_P2, Tumor_P3, Tumor_P4, Tumor_P5, Blood_P1, Blood_P2, Blood_P3, Blood_P4, Blood_P5. The values represent the administered activity (radioactive activity) in patients. I want to fit the curves using a non-linear mixed-effects model, using the following equation:
model = [A1*e^-(λ1+λ1_phys)*t] + [A2*e^-(λ_phys)*t] - [A3*e^-(λ_2+λ_phys)*t] - (A1+A2-A3)*e^-(λ_bc+λ_phys)*t
where A1, A2, and A3 are the coefficients of the respective exponential terms with values ≥ 0, λ1 and λ2 are biological rate constants with values ≥ 0, λ_bc is the rate of blood circulation approximated as λbc = ln(2)/1min, and λphys represents the radionuclide physical decay constant = ln(2)/6.7*24*60 = 7.18E-5 min-1.
Here is a piece of my MATLAB code:
% Import Excel Data
clc, clear all, format compact
TAC_data = readtable("CorrectedFile.xlsx");
% Extract the times and activity data for each organ and patient
times = TAC_data{:,1};
kidneys = TAC_data(:, 2:6);
tumor = TAC_data(:, 7:11);
blood = TAC_data(:, 12:16);
% Initial guess
initialGuess = [0.5, 0.5, 0.5, 0.1, 0.1, 7.18E-5, 0.30];
% Model function
model = @(params, times) (params(1) * exp(-(params(4) + params(6)) * times)) + ...
(params(2) * exp(-params(6) * times)) - ...
(params(3) * exp(-(params(5) + params(6)) * times)) - ...
(params(1) + params(2) - params(3)) * exp(-(params(7) + params(6)) * times);
% Iterate over each organ and perform the curve fitting for all patients within that organ
organs = {'Kidneys', 'Tumor', 'Blood'};
patients = {'P1', 'P2', 'P3', 'P4', 'P5'};
optimized_parameters = cell(length(organs), length(patients));
for i = 1:length(organs)
organ = organs{i};
% Select the data for the current organ
organ_data = TAC_data(:, (i-1)*5 + 2 : i*5 + 1);
% Stack the columns of organ data into a matrix
stacked_organ_data = table2array(organ_data);
% Perform curve fitting for each combination of organ and patient
for j = 1:length(patients)
% Select the activity data for the current patient
activity = stacked_organ_data(:, j);
% Create the grouping variable for this patient
patient_group = repmat(j, size(activity)); % Repeat patient index
% Perform curve fitting for the current organ and patient
[optimized_params, ~, ~] = nlmefitsa(times, activity, patient_group, [], model, initialGuess);
% Store the optimized parameters
optimized_parameters{i, j} = optimized_params;
% Print the optimized parameters
fprintf('Optimized parameters for %s - %s:\n', organ, patients{j});
disp(optimized_params);
end
end
In the line "Perform curve fitting" I am using the nlmefitsa tool, however, I ran the code and got the following error. Sorry in advance, I don`t know how to put the error text in red.
Error using nlmefitsa>modelcaller
Model function has returned Inf or NaN values.
Error in nlmefitsa>@(p,x,v,e)modelcaller(fvc,vect,modelfun,IdM,transphi(p),x,v,e) (line 602)
structural_model = @(p,x,v,e) modelcaller(fvc,vect,modelfun,IdM,transphi(p),x,v,e);
Error in nlmefitsa>saem_randstep (line 2120)
[f,g] = structural_model(phiMc,XM,VM,errmod);
Error in nlmefitsa/onefit (line 692)
[f,g,etaMc] = saem_randstep(ind_eta,eta0,vk2,myrandn,cholfact,...
Error in nlmefitsa (line 470)
[stop,betas(:,jrep),Gj,statsj,b_hat(idxRandPerm,uId,jrep)] = onefit(beta0(:,jbeta));
Error in test (line 43)
[optimized_params, ~, ~] = nlmefitsa(times, activity, patient_group, [], model, initialGuess);
I think the error can be related to the initial guess, but I don't know how to fix it.
Any help is welcome. I appreciate it if anyone can assist me!

Answers (1)

Shivansh
Shivansh on 21 Apr 2024
Hi Nicollas!
I understand that you are working on fitting a non-linear mixed effects model.
The error message you're encountering, "Model function has returned Inf or NaN values," typically occurs when the model function evaluates to Infinity (Inf) or Not-a-Number (NaN) for some input parameter values.
A few steps for debugging can be
  1. Ensuring no NaNs or inf in data.
  2. Experiment with initial guess values.
  3. Implement Constraints to prevent out-of-bound values.
You can also add a code snippet on sample data to check the problematic values. You can refer to the below example for implementation.
problematicValues = [];
for aValue = testRange
% Update A1 in the parameter list
params = initialGuess;
params(1) = aValue; % Update A1
% Evaluate the model with the updated parameters
modelOutput = model(params, testTimes);
% Check for NaN or Inf
if any(isnan(modelOutput) | isinf(modelOutput))
problematicValues = [problematicValues; aValue];
end
end
If the problem persists, provide a sample data for reproduction of the issue at my end.
You can refer to the following documentation for more information about "nlmefitsa": https://www.mathworks.com/help/stats/nlmefitsa.html.
I hope it helps!

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!