Fitting a nonlinear model in Matlab

1 view (last 30 days)
I have a cell array from 100 patients and each cell array has 4 signals: X{1}=4x10000, X{2}=4x10000 and so on up to X{100}. Correspondingly I have ground truth Y{1}=1x10000 and so on up to Y{100}. Now i want to fit a nonlinear model using nlmefitsa function in Matlab but i am not able to do so. Can someone please help me here? Below is the code i tried and it gives me a memory error:
clc
clear
% Assuming X is a cell array where each cell contains the signals for a patient
% Each cell contains a matrix of signals for a patient, assuming 100 patients
% Assuming you have cell arrays X and Y with signals and ground truth for each patient
num_patients = 100;
num_samples_per_patient = 10000;
num_signals = 4;
X = cell(num_patients, 1);
Y = cell(num_patients, 1);
for i = 1:num_patients
% Generate random signals for each patient
signals = randn(num_signals, num_samples_per_patient);
% Define ground truth based on some relationship with signals
% Example: Y = sum of absolute values of signals for demonstration
ground_truth = sum(abs(signals), 1);
% Store signals and ground truth for each patient
X{i} = signals;
Y{i} = ground_truth;
end
% Combine signals and ground truth across patients
X_all = cell2mat(X);
Y_all = cell2mat(Y);
% Create a grouping variable for patients
group = repelem(1:num_patients, size(X{1}, 2)); % Grouping variable based on the number of samples in each patient
% Define the nonlinear model function
modelFun = @(b, t, X) b(1) * sin(X(:, 1)) + b(2) * exp(X(:, 2)) + ... % Define the custom nonlinear model here
b(3) * log(1 + abs(X(:, 3))) + b(4) * X(:, 4).^2;
% Initial values for the parameters (beta)
initialBeta = zeros(4, 1); % Modify the size based on the number of parameters in your model
% Create V matrix with independence within each patient's measurements
num_samples_per_patient = size(X{1}, 2); % Assuming all patients have the same number of samples
V = eye(num_samples_per_patient); % Assuming independence within each patient
V = repmat(V, num_patients, 1);
Error using repmat
Requested 1000000x10000 (74.5GB) array exceeds maximum array size preference (30.9GB). This might cause MATLAB to become unresponsive.
% Fit a nonlinear mixed-effects model
mdl = nlmefitsa(X_all, Y_all, group, modelFun, initialBeta, 'V', V, 'OptimFun', 'fminunc');
% Display model summary
disp(mdl);
% Get the coefficients of the model
coefficients = mdl.beta;
% Display the estimated coefficients
disp('Estimated Coefficients:');
disp(coefficients);

Accepted Answer

Walter Roberson
Walter Roberson on 11 Dec 2023
num_samples_per_patient = 10000;
%...
signals = randn(num_signals, num_samples_per_patient);
signals (and so X{1}) is going to have a second dimension of 10000
V = eye(num_samples_per_patient); % Assuming independence within each patient
That eye is going to be 10000 x 10000.
num_patients = 100;
%...
V = repmat(V, num_patients, 1);
You want to repeat that 10000 x 10000, 100 times vertically. That would take 1000000 x 10000 double precision array elements. That would require 75 gigabytes. You do not have that much memory on your system.
  3 Comments
Walter Roberson
Walter Roberson on 11 Dec 2023
Install more memory, or rent access to a MATLAB system with more memory.
What I would probably do though is reduce the number of samples per patient
Sam Chak
Sam Chak on 11 Dec 2023
@Sunny, please correct me if I'm mistaken. Shouldn't each patient's data conform to the proposed nonlinear function? If there are 100 patients, theoretically, there should be 100 nonlinear functions, each with its own set of parameters {, , , }.
% Define the nonlinear model function
modelFun = @(b, t, X) b(1)*sin(X(:, 1)) + b(2)*exp(X(:, 2)) + ...
b(3)*log(1 + abs(X(:, 3))) + b(4)*X(:, 4).^2;

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!