如何在r中找到平均绝对偏差(MAD)

时间:2019-10-06 18:11:23

标签: r robustness

我发现r中的平均绝对偏差。当我运行它时,r提示我应该有数字数据。此外,当我将数据转换为数字时,它给我一个错误,即您的x必须是原子的。

md <- mad(x, center = median(x), constant = 1.4826, na.rm = FALSE,
      low = FALSE, high = FALSE)
Error in median.default(x) : need numeric data

当我将数据转换为数值时,它再次给我一个错误,即您的x必须是原子的。这是错误。

md <- mad(x.num, center = median(x.num), constant = 1.4826, na.rm = FALSE,
      low = FALSE, high = FALSE)
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
  'x' must be atomic

1 个答案:

答案 0 :(得分:0)

如果您可以在此处提供x向量,它将很有帮助。在以下原子向量和非数字向量上都可以正常工作。

# numeric vector
> x <- 1:10
> mad(x, center = median(x), constant = 1.4826, na.rm = FALSE, low = FALSE, high = FALSE)
> 3.7065

# non-numeric vector
> x <- c(TRUE, FALSE, T, F)
> mad(x)
> 0.7413

# atomic vector
> x <- c(1L, 6L, 10L)
> mad(x)
> 10

# works even with NA's in the vector
> x <- c(1L, 6L, 10L, NA)
> mad(x, na.rm = TRUE)
> 5.9304