Boxplot:Matlab显示关于R的不同图形?

问题描述 投票:3回答:3

我绘制了一个简单的数据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]);

产生如下图:enter image description here

在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()

产生以下图表:

enter image description here

观察1:如果我从两个脚本中省略参数'whiskers'和'range',我会获得相同的图形;预计1.5似乎是默认的胡须价值。

观察2:matlab和R似乎都以正确的方式读取数据,我的意思是两个工作空间都可视化相同的矩阵

我错过了什么?我应该信任哪个图表?

r matlab boxplot
3个回答
2
投票

explanation for R boxplot code

MATLAB code for boxplots

因此,通过这两个函数,我发现他们似乎都在计算完全相同的东西,甚至到他们如何定义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

我使用数据的第3列来测试并查看分位数的计算方式。正如你所看到的那样,25%和75%的差异并不是很大,只是差别足以在matlab代码中产生更大的晶须截止。


1
投票

来自MATLAB boxplot documentation

在每个方框上,中心标记表示中位数,方框的底部和顶部边缘分别表示第25和第75百分位数。晶须延伸到不被视为异常值的最极端数据点,并且使用“+”符号单独绘制异常值。

您可能想要查看异常值计算。

在可选的'Whisker'输入(默认1.5)下,您可以看到以下解释:

如果boxplot大于q3 + w × (q3 – q1)或小于q1 – w × (q3 – q1)w将点作为异常值,其中q1是最大的晶须长度,q3'Whisker'分别是样本数据的第25和第75百分位数。

如果将0.7选项设置为boxplot(df(:, 1:end-1), 'Whisker', 0.7); ,则会得到与R代码中相同的图:

boxplot

boxplot

R的range的等效输入是docsHojo's answer):

范围:这决定了情节胡须从盒子中伸出的距离。如果范围是正的,则晶须延伸到最极端的数据点,该数据点不超过从框中的四分位数范围的范围。值为零会导致晶须延伸到极端数据。

这似乎与上面MATLAB文档中显示的定义相同 - 请参阅qazxswpoi以获取有关IQR计算的更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.