Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何计算置信区间并将其绘制在条形图上_Matlab_Plot_Bar Chart_Confidence Interval - Fatal编程技术网

Matlab 如何计算置信区间并将其绘制在条形图上

Matlab 如何计算置信区间并将其绘制在条形图上,matlab,plot,bar-chart,confidence-interval,Matlab,Plot,Bar Chart,Confidence Interval,我如何从一个平面中绘制一个条形图 data=1x10单元 ,其中单元格中的每个值具有不同的维度,如3x100、3x40、66x2等 我的目标是得到一个条形图,其中我有10组条形图,每组中每个值有三个条形图。在条形图上,我想让它显示值的中间值,我想计算置信区间并另外显示它 在本例中,没有一组条形图,但我的目的是向您展示我希望如何显示置信区间。在上,我发现了这个例子,他们提供了一个解决方案,其中有这个命令行 e1 = errorbar(mean(data), ci95); 但我的问题是,它找不到

我如何从一个平面中绘制一个条形图

data=1x10单元
,其中单元格中的每个值具有不同的维度,如3x100、3x40、66x2等

我的目标是得到一个条形图,其中我有10组条形图,每组中每个值有三个条形图。在条形图上,我想让它显示值的中间值,我想计算置信区间并另外显示它

在本例中,没有一组条形图,但我的目的是向您展示我希望如何显示置信区间。在上,我发现了这个例子,他们提供了一个解决方案,其中有这个命令行

e1 = errorbar(mean(data), ci95);
但我的问题是,它找不到任何ci95


那么,在不安装或下载其他服务的情况下,还有其他有效的方法吗?

因为我不确定您的数据是什么样子的,因为在您的问题中,您指出单元格的元素包含不同维度的数据,如

3x100、3x40、66x2

我假设您的数据可以按列或行排列,并且并非所有数据都需要三个栏

由于您没有提供一小段数据供我们测试,我生成了一些人工数据:

data = cell(1,10);

% Random length of the data
l = randi(500, 10, 1) + 50;  

% Random "width" of the data, with 3 more likely
w = randi(4, 10, 1);
w(w==4) = 3;
% random "direction" of the data
d = randi(2, 10, 1);

% sigma of the data (in fraction of mean)
sigma = rand(10,1) / 3;

% means of the data
dmean = randi(150,10,1);
dsigma = dmean.*sigma;

for c = 1 : 10
    if d(c) == 1
        data{c} = randn(l(c), w(c)) .* dsigma(c) + dmean(c);
    else
        data{c} = randn(w(c), l(c)) .* dsigma(c) + dmean(c);
    end
end
下一件事是

在条形图上,我想让它显示值的中间值,我想计算置信区间并另外显示它

是否确实要绘制中间值?某些数据的中值与数据的方差无关,不需要任何类型的误差条。我想你是想表现出你的意思。如果你真的想显示中位数,a可能是更好的选择

以下代码计算并绘制条形图中的平均值:

means = zeros(numel(data),3);
stds = zeros(numel(data),3);
n = zeros(numel(data),3);
for c = 1:numel(data)
    d = data{c};
    if size(d,1) < size(d,2)
        d = d';
    end
    cols = size(d,2);
    means(c, 1:cols) = nanmean(d);
    stds(c, 1:cols) = nanstd(d);
    n(c, 1:cols) = sum(~isnan((d)));
end

b = bar(means);
最后一件事是绘制错误条。在这里,我选择了您在问题中提出的ci95,如果您想更改它,只需在调用
errorbar
时更改变量即可:

for c = 1:3
    size(means(:, c))
    size(b(c).XData)
    e = errorbar(b(c).XData + b(c).XOffset, means(:,c), ci95(:, c));
    e.LineStyle = 'none';
end

我发现Patrick Happel的答案不起作用,因为图形窗口(以及变量
b
)被后续调用
errorbar
清除。只需添加一个
hold on
命令即可解决此问题。为了避免混淆,这里有一个新的答案,它复制了Patrick的所有原始代码,加上我的小改动:

%% Old answer
%Just to be safe, let's clear everything
clear all

data = cell(1,10);

% Random length of the data
l = randi(500, 10, 1) + 50;  

% Random "width" of the data, with 3 more likely
w = randi(4, 10, 1);
w(w==4) = 3;
% random "direction" of the data
d = randi(2, 10, 1);

% sigma of the data (in fraction of mean)
sigma = rand(10,1) / 3;

% means of the data
dmean = randi(150,10,1);
dsigma = dmean.*sigma;

for c = 1 : 10
    if d(c) == 1
        data{c} = randn(l(c), w(c)) .* dsigma(c) + dmean(c);
    else
        data{c} = randn(w(c), l(c)) .* dsigma(c) + dmean(c);
    end
end
%============================================
%Next thing is 
%    On the bar, I want it to be shown the median of the values, and I
%    want to calculate the confidence interval and show it additionally.
%
%Are you really sure you want to plot the median? The median of some data
%is not connected to the variance of the data, and hus no type of error
%bars are required. I guess you want to show the mean. If you really want
%to show the median, a box plot might be a better alternative.
%
%The following code computes and plots the mean in a bar plot:
%============================================
means = zeros(numel(data),3);
stds = zeros(numel(data),3);
n = zeros(numel(data),3);
for c = 1:numel(data)
    d = data{c};
    if size(d,1) < size(d,2)
        d = d';
    end
    cols = size(d,2);
    means(c, 1:cols) = nanmean(d);
    stds(c, 1:cols) = nanstd(d);
    n(c, 1:cols) = sum(~isnan((d)));
end

b = bar(means);

%% New code
%This ensures that b continues to reference existing data in the next for
%loop, as the graphics objects can otherwise be deleted.  
hold on
%% Continuing Patrick Happel's answer
%============================================
%Now, we need to compute the length of the error bars. Typical choices are
%the standard deviation of the data (already computed by the code above,
%stored in stds), the standard error or the 95% confidence interval (which
%is the 1.96fold of the standard error, assuming the underlying data
%follows a normal distribution).
%============================================
% for standard deviation use stds

% for standard error
ste = stds./sqrt(n);

% for 95% confidence interval
ci95 = 1.96 * ste;
%============================================
%Last thing is to plot the error bars. Here I chose the ci95 as you asked
%in your question, if you want to change that, simply change the variable
%in the call to errorbar:
%============================================
for c = 1:3
    size(means(:, c))
    size(b(c).XData)
    e = errorbar(b(c).XData + b(c).XOffset, means(:,c), ci95(:, c));
    e.LineStyle = 'none';
end
%%旧答案
%为了安全起见,让我们把一切都清理干净
清除所有
数据=单元(1,10);
%数据的随机长度
l=randi(500,10,1)+50;
%数据的随机“宽度”,更有可能为3
w=randi(4,10,1);
w(w==4)=3;
%数据的随机“方向”
d=兰迪(2,10,1);
%数据的西格玛(平均分数)
西格玛=兰德(10,1)/3;
%数据的方法
dmean=randi(150,10,1);
dsigma=dmean.*西格玛;
对于c=1:10
如果d(c)==1
数据{c}=randn(l(c),w(c)).*dsigma(c)+dmean(c);
其他的
数据{c}=randn(w(c),l(c)).*dsigma(c)+dmean(c);
结束
结束
%============================================
%下一件事是
%在条形图上,我希望它显示值的中间值,然后
%希望计算置信区间并另外显示它。
%
%是否确实要绘制中间值?某些数据的中值
%未连接到数据的方差,并且没有任何类型的错误
%需要钢筋。我想你是想表现出你的意思。如果你真的想
%为了显示中间值,方框图可能是更好的选择。
%
%以下代码计算并绘制条形图中的平均值:
%============================================
平均值=零(numel(数据),3);
STD=零(numel(数据),3);
n=零(numel(数据),3);
对于c=1:numel(数据)
d=数据{c};
如果尺寸(d,1)<尺寸(d,2)
d=d';
结束
cols=尺寸(d,2);
平均数(c,1:cols)=平均数(d);
性病(c,1:感冒)=nanstd(d);
n(c,1:cols)=和(~isnan((d));
结束
b=巴(平均值);
%%新代码
%这确保了b在下一个数据库中继续引用现有数据
%循环,否则可以删除图形对象。
等等
%%继续Patrick Happel的回答
%============================================
%现在,我们需要计算误差条的长度。典型的选择是
%数据的标准偏差(已由上述代码计算,
%标准误差或95%置信区间(其中
%是标准误差的1.96倍,假设基础数据
%遵循正态分布)。
%============================================
%对于标准偏差,请使用STD
%标准误差
ste=性病/sqrt(n);
%95%置信区间
ci95=1.96*ste;
%============================================
%最后一件事是绘制错误条。在这里,我按照你的要求选择了ci95
%在你的问题中,如果你想改变它,只需改变变量
%在对errorbar的调用中:
%============================================
对于c=1:3
尺寸(指(:,c))
大小(b(c).扩展数据)
e=errorbar(b(c).XData+b(c).XOffset,表示(:,c),ci95(:,c));
e、 LineStyle='无';
结束

您的问题是如何在matlab中获得95%的置信区间(给定一些上下文)还是如何绘制条形图?如果您希望单元格中的每个元素有三个条形图,您希望如何从66×2大小的元素中生成三个条形图?“一根杆子的高度应该是零吗?”阿盖尔说,问题是如何绘制信心曲线intervals@PatrickHappel这张图片与条数无关。这只是为了显示我希望在整个过程中有一个置信区间bar@CroatiaHR:置信区间始终与平均值、标准偏差等的估计值相关联。置信区间假设一个基本分布,其准确性需要在别处确认。你知道它是如何工作的吗?1.96因子假设基础中的正态分布。很高兴你能解释一切。但我认为跳到1.96并不一定对OP有利,因为OP目前还不了解o
%% Old answer
%Just to be safe, let's clear everything
clear all

data = cell(1,10);

% Random length of the data
l = randi(500, 10, 1) + 50;  

% Random "width" of the data, with 3 more likely
w = randi(4, 10, 1);
w(w==4) = 3;
% random "direction" of the data
d = randi(2, 10, 1);

% sigma of the data (in fraction of mean)
sigma = rand(10,1) / 3;

% means of the data
dmean = randi(150,10,1);
dsigma = dmean.*sigma;

for c = 1 : 10
    if d(c) == 1
        data{c} = randn(l(c), w(c)) .* dsigma(c) + dmean(c);
    else
        data{c} = randn(w(c), l(c)) .* dsigma(c) + dmean(c);
    end
end
%============================================
%Next thing is 
%    On the bar, I want it to be shown the median of the values, and I
%    want to calculate the confidence interval and show it additionally.
%
%Are you really sure you want to plot the median? The median of some data
%is not connected to the variance of the data, and hus no type of error
%bars are required. I guess you want to show the mean. If you really want
%to show the median, a box plot might be a better alternative.
%
%The following code computes and plots the mean in a bar plot:
%============================================
means = zeros(numel(data),3);
stds = zeros(numel(data),3);
n = zeros(numel(data),3);
for c = 1:numel(data)
    d = data{c};
    if size(d,1) < size(d,2)
        d = d';
    end
    cols = size(d,2);
    means(c, 1:cols) = nanmean(d);
    stds(c, 1:cols) = nanstd(d);
    n(c, 1:cols) = sum(~isnan((d)));
end

b = bar(means);

%% New code
%This ensures that b continues to reference existing data in the next for
%loop, as the graphics objects can otherwise be deleted.  
hold on
%% Continuing Patrick Happel's answer
%============================================
%Now, we need to compute the length of the error bars. Typical choices are
%the standard deviation of the data (already computed by the code above,
%stored in stds), the standard error or the 95% confidence interval (which
%is the 1.96fold of the standard error, assuming the underlying data
%follows a normal distribution).
%============================================
% for standard deviation use stds

% for standard error
ste = stds./sqrt(n);

% for 95% confidence interval
ci95 = 1.96 * ste;
%============================================
%Last thing is to plot the error bars. Here I chose the ci95 as you asked
%in your question, if you want to change that, simply change the variable
%in the call to errorbar:
%============================================
for c = 1:3
    size(means(:, c))
    size(b(c).XData)
    e = errorbar(b(c).XData + b(c).XOffset, means(:,c), ci95(:, c));
    e.LineStyle = 'none';
end