仅从均值和标准差绘制置信区间

时间:2016-11-17 14:37:58

标签: matlab statistics confidence-interval

我试图从数据的均值和标准差(std)绘制置信区间。这是我写的代码片段:

 MeanA=1.876; %mean of A
 STDA=0.018; % std of A
MeanB=1.821;
STDB=0.039;

MeanC=1.735;
 STDC=0.023;
 MeanD=1.667;
STDD=0.039;
 Y = [MeanA MeanB ;
       MeanC MeanD ];     
errY= [STDA STDB;
         STDC   STDC ];

如果我绘制正态分布,那么它们的cofidence inteval似乎重叠

 alpha = 0.05;          % significance level


for tt=1:length(Y)
figure
mu = Y(tt,1);               % mean
 sigma = errY(tt,1);  
cutoff1n = norminv(alpha, mu, sigma);
cutoff2n = norminv(1-alpha, mu, sigma);
 xn = [linspace(mu-4*sigma,cutoff1n), ...
linspace(cutoff1n,cutoff2n), ...
linspace(cutoff2n,mu+4*sigma)];
yn = normpdf(xn, mu, sigma);
plot(xn,yn)
mu = Y(tt,2);               % mean
sigma = errY(tt,2);  
cutoff1 = norminv(alpha, mu, sigma);
cutoff2 = norminv(1-alpha, mu, sigma);
x = [linspace(mu-4*sigma,cutoff1), ...
linspace(cutoff1,cutoff2), ...
linspace(cutoff2,mu+4*sigma)];
y = normpdf(x, mu, sigma);
hold on, plot(x,y)
plot(x,y,'r-',xn,yn,'g-' , 'LineWidth',3) 
set(gca,'Fontsize', 32)
if tt==1
hleg1=legend('A', 'B');
title('study1')
elseif tt==2
hleg1=legend('C', 'D');
title('Study2') 
end
 set(hleg1,'Location','NorthEastoutside')
 set(gca,'Fontsize',22)
 xlo = [x(x<=cutoff1) cutoff1];
 ylo = [y(x<=cutoff1) 0];
 patch(xlo, ylo, 'r')
 xhi = [cutoff2 x(x>=cutoff2)];
 yhi = [0 y(x>=cutoff2)];
 patch(xhi, yhi, 'r')
 xlon = [xn(xn<=cutoff1n) cutoff1n];
 ylon = [yn(xn<=cutoff1n) 0];
  patch(xlon, ylon, 'g') 
  xhin = [cutoff2n xn(xn>=cutoff2n)];
  yhin = [0 yn(xn>=cutoff2n)];
  patch(xhin, yhin, 'g')
end

我看到了置信区间(CI)重叠的情节。

enter image description here

现在我需要以;

的形式绘制CI

enter image description here

有人可以帮助绘制意味着

的CI

%%

我正按以下方式计算Ci:

    SE2=errY/sqrt(10);
    CI2n=Y-1.96*(SE2);
    CI2p=Y+1.96*(SE2);

请告诉它是否正确,如果是,我该如何绘制它们。感谢

1 个答案:

答案 0 :(得分:0)

当标准差mu已知且样本大小为sigma时,平均值n的95%置信区间由

给出
CI = mu +/- 1.96*sigma/sqrt(n)

因此,对于mu = Ysigma = errY,您对95%置信区间的计算对于n = 10的样本大小是正确的。

要使用误差线绘制这些值,请使用MATLAB的errorbar函数(documentation)尝试以下操作:

n = 10;
SE2=errY/sqrt(n);
for i = 1:size(SE2,1)
    figure
    y = Y(i,:);
    x = 1:length(y);
    e = 1.96*SE2(i,:);
    errorbar(1:length(y),y,e,e,'k.','markersize',18);
    if i == 1
        c = ['A';'B'];
    elseif i == 2
        c = ['C';'D'];
    end
    set(gca,'xtick',x,'xticklabels',c)
end
产生this image

(我想将图像内联,但缺乏足够的代表)。

您可能会注意到置信区间似乎没有重叠。但是,我们可以通过设置n = 1来获取密度图中的结果,这意味着CI = mu +/- 1.96*sigma。我会发布第二张图片,但我需要更多代表!