MATLAB Answers

Calculate mean and confidence interval

5 ビュー (過去 30 日間)
Rajvi Amle
Rajvi Amle 2021 年 6 月 21 日
編集済み: Rajvi Amle 2021 年 6 月 27 日
I am trying to calculate mean and confidence interval of x=1x10 cells containing 146x18 simulations calculated in a loop for 10 iterations. Although when code is run, it shows me the error that
'matrix dimensions must agree'.
Error in toy_example (line 262)
CI95 = mean_x + ts.*(std_x/sqrt(length(x)));
Could someone please help me with this suggesting what mistake I did. This would be really very helpful and appreciated.
My code for the loop for 10 iterations and them mean and CI for those 10 simulations/iterations is mentioned below and also attached the values of 'x':
%Enteries in a loop
n=10;
t=[];
x=[];
for i=1:n
tspan=[0 max(EXP.t)];
MAT=[SYSTEM.m_BW(i) SYSTEM.w_L(i) SYSTEM.w_K(i) SYSTEM.w_Lu(i) SYSTEM.w_BR(i) SYSTEM.w_Blood(i) SYSTEM.w_Plasma(i) SYSTEM.m_L(i) SYSTEM.m_BR(i) SYSTEM.m_K(i) SYSTEM.m_S(i) SYSTEM.m_Lu(i) SYSTEM.m_Blood(i) SYSTEM.m_Plasma(i)...
SYSTEM.V_L(i) SYSTEM.V_BR(i) SYSTEM.V_K(i) SYSTEM.V_S(i) SYSTEM.V_Lu(i) SYSTEM.V_Blood(i) SYSTEM.V_Plasma(i) SYSTEM.F_C(i) SYSTEM.F_L(i) SYSTEM.F_BR(i) SYSTEM.F_K(i) SYSTEM.F_S(i) SYSTEM.F_Res(i) SYSTEM.F_bal(i) SYSTEM.F_Bile(i) SYSTEM.F_Urine(i)...
SYSTEM.V_Res(i) SYSTEM.V_bal(i) SYSTEM.V_L_b(i) SYSTEM.V_L_t(i) SYSTEM.V_BR_b(i) SYSTEM.V_BR_t(i) SYSTEM.V_K_b(i) SYSTEM.V_K_t(i) SYSTEM.V_S_b(i) SYSTEM.V_S_t(i) SYSTEM.V_Lu_b(i) SYSTEM.V_Lu_t(i) SYSTEM.V_Res_b(i) SYSTEM.V_Res_t(i)...
DRUG.m_Au_iv(i) DRUG.M_Au_iv(i)]';
options=odeset('AbsTol',10e-2,'RelTol',10e-2,'Stats','on');
col=zeros(18,1);
tic;
[t0,x0]=ode15s(@ode_toy, tspan, col,[],ov,DRUG,MAT);
t=[t, {t0}];
x=[x, {x0}];
toc;
end
% Confidence interval of 95% for simulated data
mean_x=mean([x{:,2}]); % convert cell array to matrix, mean by row
% N=size(y,1);
std_x=mean_x/size(x,2); % std dev of mean(y) is mean(y)/nObs
SEM=(std_x/sqrt(length(x)))
ts = tinv([0.025 0.975],length(x)-1); % T-Score
CI95 = mean_x + ts.*(std_x/sqrt(length(x)));
Please do help me.

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 6 月 21 日
mean_x=mean([x{:,2}]); % convert cell array to matrix, mean by row
Your x is a cell array, with entries that might be different number of rows because the ode*() routines can output a variable number of rows when you provide a time span that is exactly two elements.
The syntax you are using [x{:,2}] is going to access the second element of the cell array only, and extra the one matrix there. Your initial conditions are length 18, so that is going to be a something-by-18 matrix, Then the [] around the single something-by-18 matrix is going to leave it as a something-by-18 matrix. Your x{:,2} looks on the surface like it might be selecting multiple entries in x, but x is a row vector of cell arrays because of the way you build it with [x,{x0}] so there is only one row in x and so x{:,2} is going to be x{1,2}
You then take the mean of that something-by-18 matrix along the first non-scalar dimension, so you will get a 1 x 18 vector.
std_x=mean_x/size(x,2); % std dev of mean(y) is mean(y)/nObs
size(x,2) is going to be the number of cells in the cell array, which will be n. But why would you divide the mean created from a single cell entry by the number of iterations you did?
SEM=(std_x/sqrt(length(x)))
why are you mixxing using length(x) and size(x,2) to get the number of entries in x ? Be consistent or people will get stuck trying to figure out if there are any circumstances under which the two could differ.
ts = tinv([0.025 0.975],length(x)-1); % T-Score
That is going to be a vector of length 2.
CI95 = mean_x + ts.*(std_x/sqrt(length(x)));
mean_x is 1 x 18, std_x is 1 x 18. sqrt(length(x)) is scalar, so you have 1 x 18 + 1 x 2 .* 1 x 18 . But 1 x 2 .* 1 x 18 is going to fail.
If you made
ts = tinv([0.025; 0.975],length(x)-1); % T-Score
then it would be 2 x 1 and that can be .* with 1 x 18 giving 2 x 18, which can be added to 1 x 18, giving 2 x 18.
  6 件のコメント
Rajvi Amle
Rajvi Amle 2021 年 6 月 27 日
@Walter Roberson I have attached 3 files. 1st file toy_example have all defined variables and 2 biological variables calculated with random function, a loop for 10 iterations, post processing sections that are stored as results which can be used to plot as simulation results and in the end are given all the plottings. 2nd file ode_toy has all 18 ode equations and 3rd file is ae_toy which has algebraic equations and connected with the ODEs. All these three files are interconnected to each other. My model is quite complex but please do help me if possible. I only have problem with the calculation of mean as I am facing problem with the matrix size, dimensions, cell arrays etc.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by