【问题标题】:R: Identifying outliers in 2D gaussian distributionR:识别二维高斯分布中的异常值
【发布时间】:2017-03-21 12:00:51
【问题描述】:

我有一个二维高斯分布,我正在尝试识别异常值。这不是去除异常值的意义,而是识别与主体最不相似的样本。

http://imgur.com/hlOqjig

您对如何最好地处理这些数据有什么建议吗?我试图在两个维度上拟合正态分布并计算所有数据点的 p 值,然后将异常值识别为 p 值最低的数据点。但是,我得到以下结果:

http://imgur.com/a/w6SAz

这是计算P值的代码:

library(fitdistrplus)

norm_pvalue <- function(input_dist, input_values) {
  # Fitting normal distribution
  fit <- fitdist(input_dist, "norm")

  # Calculating p-values
  p_values <- unlist(lapply(input_values, function(x) dnorm(x = x, mean=     fit$estimate[['mean']], sd= fit$estimate[['sd']])))

  return(p_values)
}

我希望解决方案具有普遍性。

【问题讨论】:

  • 求马氏距离质心?

标签: r normal-distribution outliers n-dimensional


【解决方案1】:

没有数据,很难做出任何详细的回应。但是,您可能想查看最新版本的软件包 assertr,此处注明:http://www.onthelambda.com/2017/03/20/data-validation-with-the-assertr-package/。

我真的很喜欢它的工作流程,它非常通用。

例如,如果您要检查数据框 (df) 中的列 (col) 中的数据,您可以使用类似以下内容:

library(assertr)
library(magrittr)  

df %>% insist(within_n_sds(2), col)

然后,此最终函数将通知您所有异常值(即与平均值相差两个以上标准差的点)。该软件包还包括许多用于评估异常值的不同措施。

在您的情况下,有问题的列可能基于 PC1 和 PC2 的最佳拟合线的残差:

PCA.lm = lm(PC2 ~ PC1, data=df) 
PCA.res = resid(PCA.lm)

希望对你有帮助。

【讨论】:

  • 感谢您的回答。不幸的是,由于工作原因,我无法附上数据。正如您在应用线性回归时建议的那样,我没有研究变量之间的关系。相反,我正在寻找距离绘图密集区域最远的数据点。我考虑过使用密度聚类,然后测量到质心的距离。但是,这不适用于具有这种分布类型的数据。
【解决方案2】:

我刚刚使用 ggplot2 的 stat_ellipse 来识别异常值。我使用了 0.999 的置信水平。

此函数提取椭球外的点,并获取一个 ggplot 和绘制椭球的图层。

# Function for identifying points outside ellipse
outside_ellipse <- function(ggplot, ellipsoid_layer_number) {
  # Extracting components
  build <- ggplot_build(ggplot)$data
  points <- build[[1]]
 ell <- build[[ellipsoid_layer_number]]

  # Finding points are inside the ellipse, and add this to the data
  df <- data.frame(points[1:2], 
                in.ell = as.logical(point.in.polygon(points$x, points$y, ell$x, ell$y)))

  # Plot the result
  ggplot(df, aes(x, y)) +
    geom_point(aes(col = in.ell)) +
    stat_ellipse()

  # Returning indices of outliers
  return(which(df$in.ell == FALSE))
}

在这里,我使用椭球选项绘制我的数据,并提取椭球外的点并将它们的信息添加到数据框中。

  # Saving plot with confidence ellipsoid
  plotData <- ggplot(pc_df, aes(PC1, PC2)) + geom_point() + stat_ellipse(level = 0.999)

  # Identifying points outside ellipsoid
  outside <- outside_ellipse(plotData, 2)
  pc_df$in_ellipsoid <- rep(FALSE, dim(pc_df)[1])
  pc_df$in_ellipsoid[outside] <- TRUE 

【讨论】:

    猜你喜欢
    • 2015-09-28
    • 2016-04-15
    • 2019-07-27
    • 2011-01-05
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2018-07-21
    • 2015-07-11
    相关资源
    最近更新 更多