我绘制了一个简单的数据matrix
39 135 249 1 91 8 28 0 0 74 17 65 560
69 0 290 26 254 88 31 0 18 53 4 63 625
66 186 344 0 9 0 0 0 18 54 0 74 554
80 41 393 0 0 0 2 0 6 51 0 65 660
271 112 511 1 0 274 0 0 0 0 16 48 601
88 194 312 0 110 0 0 0 44 13 2 76 624
198 147 367 0 15 0 0 3 9 44 3 39 590
使用标准箱线图(即晶须从Q1和Q3延伸1.5 x IRQ的位置)。 每列都是变量,每行都是观察值。
尽管如此,我还是使用R(RStudio 1.0.44)和Matlab2018获得了两个不同的图形。 特别是,晶须以不同的方式延伸。
在Matlab中,我使用以下代码:
% clearing workspace
clear all;
close all;
clc;
%entering in current directory where I find the txt data file
tmp = matlab.desktop.editor.getActive;
cd(fileparts(tmp.Filename));
clear tmp;
%reading data
df = readtable('pippo.txt', 'Delimiter', '\t', 'ReadVariableNames',false);
df = table2array(df)
figure(1);
boxplot(df(:, 1:end-1), 'Whisker', 1.5);
ylim([0 600]);
在R中,我使用以下代码:
rm(list = ls())
# getting the current directory
working_dir <-dirname(rstudioapi::getActiveDocumentContext()$path)
# setting the working directory where I finf the txt file with data
setwd(working_dir)
df <- read.table("pippo.txt")
jpeg('r_boxplot.jpg')
boxplot(df[,1:12], las=2, ylim=c(0,600), range=1.5)
dev.off()
将产生以下图形:
观察1:如果我在两个脚本中都省略了“晶须”和“范围”参数,则会获得相同的图形;预期值为1.5似乎是默认的晶须值。
观察2:matlab和R似乎都以正确的方式读取数据,我的意思是两个工作区都可视化了同一矩阵
我想念什么?我应该信任哪个图?
答案 0 :(得分:2)
explanation for R boxplot code
因此,通过这两个函数,我发现它们似乎都在计算完全相同的东西,甚至在定义IQR的方式上也是如此
R声称要对箱形图进行以下操作
upper whisker = min(max(x), Q_3 + 1.5 * IQR)
lower whisker = max(min(x), Q_1 – 1.5 * IQR)
where IQR = Q_3 – Q_1, the box length.
MATLAB声称是针对他们的箱线图
p75 + w(p75 – p25)
p25 – w(p75 – p25)
where p25 and p75 are the 25th and 75th percentiles, respectively.
即使它们定义晶须扩展的方式也与Matlab说明相同
% The plotted whisker extends to the adjacent value, which is the most
% extreme data value that is not an outlier. Set whisker to 0 to give
% no whiskers and to make every point outside of p25 and p75 an outlier.
R状态
Range determines how far the plot whiskers extend out from the box. If range is
positive, the whiskers extend to the most extreme data point which is no more than
range times the interquartile range from the box. A value of zero causes the whiskers
to extend to the data extremes.
我个人认为,这与执行计算的某些基本方式有关。 编辑将代码弄乱后,我可以确认它与基础计算有关。
R代码
quantile(a,c(.25, .75))
25% 75%
301 380
> 380+1.5*(380-301)
[1] 498.5
> 301-1.5*(380-301)
[1] 182.5
Matlab代码
prctile(te,[25,75])
ans =
295.5000 386.5000
W75 = p75 + 1.5*(p75-p25)
W25 = p25 - 1.5*(p75-p25)
W75 =
523
W25 =
159
我使用了数据的第三列来测试并查看分位数的计算方式。如您所见,25%和75%差别不大,只是差别足以导致matlab代码中的晶须截断较大。
答案 1 :(得分:1)
从MATLAB boxplot
documentation:
在每个框上,中心标记表示中位数,而框的底部和顶部边缘分别表示第25个百分点和第75个百分点。 晶须延伸到不视为异常值的最极端数据点,并且使用“ +”符号分别绘制异常值。
您可能想检查异常值计算。
在可选的'Whisker'
输入(默认为1.5)下,您可以看到以下说明:
boxplot
将点画成离群值,如果它们大于q3 + w × (q3 – q1)
或小于q1 – w × (q3 – q1)
,其中w
是最大晶须长度,而q1
和q3
分别是样本数据的第25个百分点和第75个百分点。
如果将'Whisker'
选项设置为0.7
,则会得到与R代码相同的图:
boxplot(df(:, 1:end-1), 'Whisker', 0.7);
R的boxplot
的等效输入为range
(docs):
范围:确定晶须从盒中伸出的距离。如果范围为正,则晶须延伸到最极端的数据点,该范围不超过框内四分位间距的范围乘以。值为零会导致晶须扩展到数据极限。
这似乎与MATLAB文档中的定义相同-请参阅Hojo's answer,以获取有关IQR计算的更多详细信息。
答案 2 :(得分:-3)
我认为差异取决于盒图的晶须的绘制方式。晶须定义了几种方法。
在您的情况下,我猜想matlab(作为非统计性语言)默认情况下会将晶须从最小值吸引到最大值,而R则更注重统计性,并且默认情况下使用四分位间距(IQR)。因此,它将外部的所有内容标记为数据中的异常值。 R中的范围参数是四分位间距的因子,用于确定离群值。我不使用matlab,但我猜想添加晶须会改变晶须生成方式的行为(可能从IQR到min-max)。
值得信赖的人取决于您要分析的内容以及异常值是否对您起作用。