箱线图:Matlab 显示关于 R 的不同图形?
Boxplot: Matlab displays different graphic in respect of R?
我画了一个简单的数据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:如果我从两个脚本中省略参数 'whiskers' 和 'range',我将获得相同的图形;预计 1.5 似乎是默认的晶须值。
观察 2:matlab 和 R 似乎都以正确的方式读取数据,我的意思是两个工作区可视化相同的矩阵
我错过了什么?我应该相信哪个图表?
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
我使用您数据的第 3 列来测试并查看分位数是如何计算的。如您所见,25% 和 75% 差别不大,但差别足以在 matlab 代码中导致更大的晶须截止。
来自 MATLAB boxplot
documentation:
On each box, the central mark indicates the median, and the bottom and top edges of the box indicate the 25th and 75th percentiles, respectively. The whiskers extend to the most extreme data points not considered outliers, and the outliers are plotted individually using the '+' symbol.
您可能想检查异常值计算。
可选的'Whisker'
输入下(默认1.5),可以看到这样的解释:
boxplot
draws points as outliers if they are greater than q3 + w × (q3 – q1)
or less than q1 – w × (q3 – q1)
, where w
is the maximum whisker length, and q1
and q3
are the 25th and 75th percentiles of the sample data, respectively.
如果将 'Whisker'
选项设置为 0.7
,您将得到与 R 代码中相同的图:
boxplot(df(:, 1:end-1), 'Whisker', 0.7);
R 的 boxplot
的等效输入是 range
(docs):
Range: this 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.
这似乎与 MATLAB 文档中的上述定义相同 - 请参阅 了解有关 IQR 计算的更多详细信息。
我画了一个简单的数据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:如果我从两个脚本中省略参数 'whiskers' 和 'range',我将获得相同的图形;预计 1.5 似乎是默认的晶须值。
观察 2:matlab 和 R 似乎都以正确的方式读取数据,我的意思是两个工作区可视化相同的矩阵
我错过了什么?我应该相信哪个图表?
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
我使用您数据的第 3 列来测试并查看分位数是如何计算的。如您所见,25% 和 75% 差别不大,但差别足以在 matlab 代码中导致更大的晶须截止。
来自 MATLAB boxplot
documentation:
On each box, the central mark indicates the median, and the bottom and top edges of the box indicate the 25th and 75th percentiles, respectively. The whiskers extend to the most extreme data points not considered outliers, and the outliers are plotted individually using the '+' symbol.
您可能想检查异常值计算。
可选的'Whisker'
输入下(默认1.5),可以看到这样的解释:
boxplot
draws points as outliers if they are greater thanq3 + w × (q3 – q1)
or less thanq1 – w × (q3 – q1)
, wherew
is the maximum whisker length, andq1
andq3
are the 25th and 75th percentiles of the sample data, respectively.
如果将 'Whisker'
选项设置为 0.7
,您将得到与 R 代码中相同的图:
boxplot(df(:, 1:end-1), 'Whisker', 0.7);
R 的 boxplot
的等效输入是 range
(docs):
Range: this 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.
这似乎与 MATLAB 文档中的上述定义相同 - 请参阅