如何在 matlab 中为 f 分布遮蔽拒绝区域?

问题描述

我需要为从 4 个样本确定的给定自由度 v1 和 v2 绘制 f 分布,并为给定的 alpha 遮蔽拒绝区域(如下图所示) rejection region shading 但是我不知道怎么做,我试过的任何东西似乎都不起作用。 附件是我目前的代码

clear
clc
format short g

%% Samples

s1=[407 411 409];
s2=[404 406 408 405 402];
s3=[410 408 406 408];
s4=[400 413 407 405 403 410 409];

observations=[s1 s2 s3 s4];

%% degrees of freedom
n=4; %no of samples
m=length(observations);

v1=n-1
v2=m-n

%% Level of sig
alpha=0.05
level_of_sig=1-alpha
critical_value=finv(level_of_sig,v1,v2)

%% Plotting F-dist

x=0:0.01:max(observations); 
fdist=fpdf(x,v2);

fig1 = figure(1);
hold on
plot(x,fdist,'linewidth',1.5)
xline(0,'Color',[0.5 0.5 0.5])
yline(0,[0.5 0.5 0.5])
grid on

% shading rejection region <<<<<<<<<<<<<<<< not working
grey  = [127 127 127]./255;
area(x(critical_value:max(observations)),fdist(critical_value:max(observations)),'basevalue','FaceColor',grey);
hold off

解决方法

使用逻辑数组为绘图的特定区域着色

不确定哪个变量决定了阴影的开始,但这里的东西可能与您想要的类似。这里基于指定为范围的条件创建了一个名为 Region_Indices 的逻辑数组。这个逻辑数组 Region_Indices 然后用于矩阵索引 xfdist 数组对应于绘图。然后使用 area() 函数对与图的这些索引坐标对应的区域进行着色。在这种情况下,我将 Shaded_Region_Start(阴影区域的开始)设置为 critical_value,但我不确定您希望从脚本中使用的正确值。

Shaded Region of a Plot

代码片段:

grey  = [127 127 127]./255;

Shaded_Region_Start = critical_value;
Region_Indices = x>Shaded_Region_Start & x<=max(x);
area(x(Region_Indices),fdist(Region_Indices),'FaceColor',grey);
xlim([0 6]);
hold off

完整脚本:

clear
clc
format short g

%% Samples

s1=[407 411 409];
s2=[404 406 408 405 402];
s3=[410 408 406 408];
s4=[400 413 407 405 403 410 409];

observations=[s1 s2 s3 s4];

%% degrees of freedom
n=4; %no of samples
m=length(observations);

v1=n-1;
v2=m-n;

%% Level of sig
alpha=0.05;
level_of_sig=1-alpha;
critical_value=finv(level_of_sig,v1,v2);

%% Plotting F-Dist

x=0:0.01:max(observations); 
fdist=fpdf(x,v2);

fig1 = figure(1);
hold on
plot(x,fdist,'LineWidth',1.5);
xline(0,'Color',[0.5 0.5 0.5]);
yline(0,[0.5 0.5 0.5]);
grid on

% shading rejection region <<<<<<<<<<<<<<<< not working
grey  = [127 127 127]./255;

Shaded_Region_Start = critical_value;
Region_Indices = x>Shaded_Region_Start & x<=max(x);
area(x(Region_Indices),grey);
xlim([0 6]);
hold off