【问题标题】:Remove outliers from plot in lattice (splom)从格子图中删除异常值(splom)
【发布时间】:2013-04-08 15:52:34
【问题描述】:

我想将一些数据绘制为带有格子的散点图矩阵。但是,数据包含一些异常值。这导致主要数据的图非常压缩。我想从图中删除异常值。一个观测值可以是一个测量变量的异常值,但不能是另一个测量变量的异常值,因此矩阵中的每个散点图都需要计算要移除的异常值。由于异常值构成了大约 10,000 个观测值中的最多 10 个,因此我想简单地删除每个变量值最低的 10 个观测值(异常值通常是那些处于更负方向的观测值)。我知道我必须修改面板功能,但我不知道该怎么做。另外,我的下面板是一个 hexbinplot,所以它也应该被修改,我希望这能以同样的方式工作。有人有想法吗?

MWE:

require(lattice)
require(hexbin)
data(iris)

iris.out <- iris
iris.out[2,1] <- 1
iris.out[3,1] <- .2
iris.out[4,2] <- .1
iris.out[5,2] <- .2

splom(~iris.out[1:4], groups = Species, data = iris,
  lower.panel = function(...,groups){
    panel.hexbinplot(xbins = 20,
                     colramp = function(n){heat.ob(n, beg=15, end=225)},...,groups=NULL)
  },
  diag.panel = function(x,...){
    yrng <- current.panel.limits()$ylim
    d <- density(x, na.rm = TRUE)
    d$y <- with(d, yrng[1] + 0.95 * diff(yrng) * y / max(y))
    panel.lines(d, col = "darkgrey")
    diag.panel.splom(x, ...)
  }

)

【问题讨论】:

    标签: r lattice outliers


    【解决方案1】:

    也许我有一个部分解决方案。对于双变量图,我删除了 x 和 y 中由最低 10 个观察值定义的异常值。此外,“组”因素仅在上面板中实现。

    splom(~iris.out[1:4],
      lower.panel=function(x,y,...){
        x.out=order(x,decreasing=FALSE)[1:10]
        y.out=order(x,decreasing=FALSE)[1:10]
        out=c(x.out,y.out)
        x=x[-out]
        y=y[-out]
        panel.hexbinplot(x,y, xbins =20,colramp = function(n){heat.ob(n, beg=15, end=225)},...)
      },
      diag.panel = function(x,...){
        x=x[-order(x,decreasing=FALSE)[1:10]]
        yrng <- current.panel.limits()$ylim
        d <- density(x, na.rm = TRUE)
        d$y <- with(d, yrng[1] + 0.95 * diff(yrng) * y / max(y))
        panel.lines(d, col = "darkgrey")
        diag.panel.splom(x, ...)
      },
      upper.panel=function(x,y,groups=iris$Species,...){
        x.out=order(x,decreasing=FALSE)[1:10]
        y.out=order(x,decreasing=FALSE)[1:10]
        out=c(x.out,y.out)
        x=x[-out]
        y=y[-out]
        panel.splom(x,y,groups=iris$Species,...)
      }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 2017-12-03
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多