PRCC sensitive analysis graphs

53 次查看(过去 30 天)
mallela ankamma rao
mallela ankamma rao 2024-9-11,15:44
评论: Shivam Gothi 2024-9-12,16:34
I have tried to get prcc sensitve analysis graph, but unfortunately i got an error.I request you please help to overcome the error.
CODE:
% Define parameter ranges
paramRanges = struct('beta', [0.1, 0.5], 'sigma', [0.1, 0.3], 'gamma', [0.1, 0.4]);
% Number of samples
nSamples = 100;
% Number of parameters
paramNames = fieldnames(paramRanges);
nParams = numel(paramNames);
% Generate LHS samples
lhsSamples = lhsdesign(nSamples, nParams);
% Scale samples to parameter ranges
scaledSamples = zeros(nSamples, nParams);
for i = 1:nParams
low = paramRanges.(paramNames{i})(1);
high = paramRanges.(paramNames{i})(2);
scaledSamples(:, i) = low + lhsSamples(:, i) * (high - low);
end
% Define SEIR model parameters and calculate R0
% Run simulations to compute R0 for each parameter set
R0Values = zeros(nSamples, 1);
for i = 1:nSamples
R0Values(i) = calculateR0(scaledSamples(i, :));
end
% Calculate PRCC
function prcc = calculatePRCC(outputs, samples)
nParams = size(samples, 2);
prcc = zeros(nParams, 1);
for i = 1:nParams
% Rank data
ranksSamples = tiedrank(samples(:, i));
ranksOutputs = tiedrank(outputs);
% Compute correlation
prcc(i) = corr(ranksSamples, ranksOutputs);
end
end
% Perform PRCC analysis for R0 values
prccValues = calculatePRCC(R0Values, scaledSamples);
function R0 = calculateR0(params)
beta = params(1);
sigma = params(2);
gamma = params(3);
% Basic reproduction number for SEIR model
R0 = beta / gamma;
end
% Display results
for i = 1:nParams
fprintf('PRCC for %s: %.4f\n', paramNames{i}, prccValues(i));
end
ERROR:
Function definitions in a script must appear at the end of the file.
Move all statements after the "calculateR0" function definition to before the first local function definition.

回答(2 个)

Shivam Gothi
Shivam Gothi 2024-9-12,14:19
I executed the code shared by you in MATLAB R2021a. I was able to reproduce the error as highlighted by you.
But, when I run the same code in MATLAB R2024a, it was executed without throwing error.
In older versions, all the function definations should go at the end of the file. This is not the case with R2024a version.
Therefore, there are two possible work arounds for this problem:
1) modify your code.
Upon inspecting the code provided by you in the comment section, I found that you have writen MATLAB commands after/in-between function definations, which is violating the defination:
"all the function definations should go at the end of the file"
I have re-organised your code and attached the corresponding (.m) file with this answer. I have compiled it in R2021a version and it is found to be executed without any error.
2) Update your MATLAB version
You can install the updated MATLAB version (R2024b) by refering the link:
R2024b allows to compile your code (shared in the question) without any error.
I hope this helps !
  2 个评论
mallela ankamma rao
mallela ankamma rao 2024-9-12,15:11
Thank you very much Shivam gothi sir
I request you please guide me to write prcc graph like below image by taking three parameters values
beta: 0.7196
sigma: 0.0342
gamma: -0.6795
Shivam Gothi
Shivam Gothi 2024-9-12,16:34
I appreciate your efforts in seeking solutions. It might be beneficial to ask this as a new question in the MATLAB Answers community. There are many experts who might be able to assist you more effectively.
Additionally, if you find any of the responses you've received helpful, it would be great to provide the feedback or marking an answer as accepted.

请先登录,再进行评论。


Shivam Gothi
Shivam Gothi 2024-9-11,16:08
编辑:Shivam Gothi 2024-9-11,16:12
I copied the same code provided by you in a new (.m) file and saved it with some name. Then I executed the file and it did not throw any error. Also, running the above code in the below given "code section" does not produce any error. But, I can suggest the possible cause behind it.
% Define parameter ranges
paramRanges = struct('beta', [0.1, 0.5], 'sigma', [0.1, 0.3], 'gamma', [0.1, 0.4]);
% Number of samples
nSamples = 100;
% Number of parameters
paramNames = fieldnames(paramRanges);
nParams = numel(paramNames);
% Generate LHS samples
lhsSamples = lhsdesign(nSamples, nParams);
% Scale samples to parameter ranges
scaledSamples = zeros(nSamples, nParams);
for i = 1:nParams
low = paramRanges.(paramNames{i})(1);
high = paramRanges.(paramNames{i})(2);
scaledSamples(:, i) = low + lhsSamples(:, i) * (high - low);
end
% Define SEIR model parameters and calculate R0
% Run simulations to compute R0 for each parameter set
R0Values = zeros(nSamples, 1);
for i = 1:nSamples
R0Values(i) = calculateR0(scaledSamples(i, :));
end
% Calculate PRCC
function prcc = calculatePRCC(outputs, samples)
nParams = size(samples, 2);
prcc = zeros(nParams, 1);
for i = 1:nParams
% Rank data
ranksSamples = tiedrank(samples(:, i));
ranksOutputs = tiedrank(outputs);
% Compute correlation
prcc(i) = corr(ranksSamples, ranksOutputs);
end
end
% Perform PRCC analysis for R0 values
prccValues = calculatePRCC(R0Values, scaledSamples);
function R0 = calculateR0(params)
beta = params(1);
sigma = params(2);
gamma = params(3);
% Basic reproduction number for SEIR model
R0 = beta / gamma;
end
% Display results
for i = 1:nParams
fprintf('PRCC for %s: %.4f\n', paramNames{i}, prccValues(i));
end
PRCC for beta: 0.7334 PRCC for sigma: -0.1026 PRCC for gamma: -0.6995
In MATLAB, all the function definations should go at the end of the file. Refer to the below MATLAB answer:
Therefore, I think that the compilation error in your case can be resolved by putting the "for loop" before the function definations as shown in below code:
I hope this helps !
  5 个评论
Shivam Gothi
Shivam Gothi 2024-9-11,16:42
Which version of MATLAB are you using?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Physical Channels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by