如何从使用 ggplot() 制作的 QQ 绘图中删除异常值?
How to delete outliers from a QQ-plot graph made with ggplot()?
我有一个二维数据集(比如 x 和 y 列)。我使用以下函数绘制此数据的 QQ 图。
# Creating a toy data for presentation
df = cbind(x = c(1,5,8,2,9,6,1,7,12), y = c(1,4,10,1,6,5,2,1,32))
# Plotting the QQ-plot
df_qq = as.data.frame(qqplot(df[,1], df[,2], plot.it=FALSE))
ggplot(df_qq) +
geom_point(aes(x=x, y=y), size = 2) +
geom_abline(intercept = c(0,0), slope = 1)
这是结果图:
我的问题是,如何避免绘制最后一点(即 (12,32))?我宁愿不手动删除它,因为我有几个这样的数据对,并且每个数据对中都有类似的异常值。我想做的是编写一个代码,以某种方式识别离 45 度线太远的点,并将它们从 df_qq 中消除(例如,如果它比到 45 度线的平均距离远 5 倍)行它可以被消除)。我的主要 objective 是让图表更易于阅读。当异常值没有被消除时,QQ-plot 中更规则的部分占据了图形的太小部分,它阻止了我从视觉上评估两个向量除了异常值之外的相似性。
如有任何帮助,我将不胜感激。
有一个 CRAN 包,referenceIntervals 使用 Cook 的距离来检测异常值。通过将它应用于 df_qq$y
的值,它可以给出要删除的 df_qq
的索引。
library(referenceIntervals)
out <- cook.outliers(df_qq$y)$outliers
i <- which(df_qq$y %in% out)
ggplot(df_qq[-i, ]) +
geom_point(aes(x=x, y=y), size = 2) +
geom_abline(intercept = c(0,0), slope = 1)
编辑。
根据 OP 的评论,
But as far as I understand this function does not look at
the relation between x & y,
也许只有当异常值在其中一个向量中是异常值而不是在两个向量中都是异常值时,才需要使用以下函数来删除异常值。
cookOut <- function(X){
out1 <- cook.outliers(X[[1]])$outliers
out2 <- cook.outliers(X[[2]])$outliers
i <- X[[1]] %in% out1
j <- X[[2]] %in% out2
w <- which((!i & j) | (i & !j))
if(length(w)) X[-w, ] else X
}
使用评论中的第二个数据集进行测试。
额外的向量 id
只是为了让分面更容易。
df1 <- data.frame(x = c(1,5,8,2,9,6,1,7,12), y = c(1,4,10,1,6,5,2,1,32))
df2 <- data.frame(x = c(1,5,8,2,9,6,1,7,32), y = c(1,4,10,1,6,5,2,1,32))
df_qq1 = as.data.frame(qqplot(df1[,1], df1[,2], plot.it=FALSE))
df_qq2 = as.data.frame(qqplot(df2[,1], df2[,2], plot.it=FALSE))
df_qq_out1 <- cookOut(df_qq1)
df_qq_out2 <- cookOut(df_qq2)
df_qq_out1$id <- "A"
df_qq_out2$id <- "B"
df_qq_out <- rbind(df_qq_out1, df_qq_out2)
ggplot(df_qq_out) +
geom_point(aes(x=x, y=y), size = 2) +
geom_abline(intercept = c(0,0), slope = 1) +
facet_wrap(~ id)
我有一个二维数据集(比如 x 和 y 列)。我使用以下函数绘制此数据的 QQ 图。
# Creating a toy data for presentation
df = cbind(x = c(1,5,8,2,9,6,1,7,12), y = c(1,4,10,1,6,5,2,1,32))
# Plotting the QQ-plot
df_qq = as.data.frame(qqplot(df[,1], df[,2], plot.it=FALSE))
ggplot(df_qq) +
geom_point(aes(x=x, y=y), size = 2) +
geom_abline(intercept = c(0,0), slope = 1)
这是结果图:
我的问题是,如何避免绘制最后一点(即 (12,32))?我宁愿不手动删除它,因为我有几个这样的数据对,并且每个数据对中都有类似的异常值。我想做的是编写一个代码,以某种方式识别离 45 度线太远的点,并将它们从 df_qq 中消除(例如,如果它比到 45 度线的平均距离远 5 倍)行它可以被消除)。我的主要 objective 是让图表更易于阅读。当异常值没有被消除时,QQ-plot 中更规则的部分占据了图形的太小部分,它阻止了我从视觉上评估两个向量除了异常值之外的相似性。
如有任何帮助,我将不胜感激。
有一个 CRAN 包,referenceIntervals 使用 Cook 的距离来检测异常值。通过将它应用于 df_qq$y
的值,它可以给出要删除的 df_qq
的索引。
library(referenceIntervals)
out <- cook.outliers(df_qq$y)$outliers
i <- which(df_qq$y %in% out)
ggplot(df_qq[-i, ]) +
geom_point(aes(x=x, y=y), size = 2) +
geom_abline(intercept = c(0,0), slope = 1)
编辑。
根据 OP 的评论,
But as far as I understand this function does not look at the relation between x & y,
也许只有当异常值在其中一个向量中是异常值而不是在两个向量中都是异常值时,才需要使用以下函数来删除异常值。
cookOut <- function(X){
out1 <- cook.outliers(X[[1]])$outliers
out2 <- cook.outliers(X[[2]])$outliers
i <- X[[1]] %in% out1
j <- X[[2]] %in% out2
w <- which((!i & j) | (i & !j))
if(length(w)) X[-w, ] else X
}
使用评论中的第二个数据集进行测试。
额外的向量 id
只是为了让分面更容易。
df1 <- data.frame(x = c(1,5,8,2,9,6,1,7,12), y = c(1,4,10,1,6,5,2,1,32))
df2 <- data.frame(x = c(1,5,8,2,9,6,1,7,32), y = c(1,4,10,1,6,5,2,1,32))
df_qq1 = as.data.frame(qqplot(df1[,1], df1[,2], plot.it=FALSE))
df_qq2 = as.data.frame(qqplot(df2[,1], df2[,2], plot.it=FALSE))
df_qq_out1 <- cookOut(df_qq1)
df_qq_out2 <- cookOut(df_qq2)
df_qq_out1$id <- "A"
df_qq_out2$id <- "B"
df_qq_out <- rbind(df_qq_out1, df_qq_out2)
ggplot(df_qq_out) +
geom_point(aes(x=x, y=y), size = 2) +
geom_abline(intercept = c(0,0), slope = 1) +
facet_wrap(~ id)