忽略 NA 分数函数离群值包 R

Ignore NA scores function outlier package R

我想忽略要计算 mscore 的向量中的 NA。我不想使用 na.omit,因为这会改变我以后用于其他目的的向量的长度。

我目前拥有的是:

library("outliers")

myvector <- c(0.00750,0.04750,0.06500,0.04750,0.0150,0.00750,0.210,0.02525,0.05750,NA)

mscore <- scores(myvector, "mad")

这会产生所有 NA,并且在分数函数中似乎不支持 NA。

不使用 na.omit,是否有变通办法?

编写一个内部处理 NA 条目的新函数:

scores_na <- function(x, ...) {
  not_na <- !is.na(x)
  scores <- rep(NA, length(x))
  scores[not_na] <- outliers::scores(na.omit(x), ...)
  scores
}

myvector <- c(0.00750,0.04750,0.06500,0.04750,0.0150,0.00750,0.210,0.02525,0.05750,NA)
scores_na(myvector, "mad")
 [1] -1.2125677  0.0000000  0.5304984  0.0000000 -0.9852112 -1.2125677
 [7]  4.9260561 -0.6744908  0.3031419         NA

你拥有所有 NA 的原因是:

当您选择 type = "mad" 时, "mad" 给出每个值与中位数之间的差异,除以中位数绝对偏差。

myvector中有1个NA,所以中位数为NA,中位数绝对差为NA。这就是为什么您在 mscore

中拥有所有 NA