【问题标题】:Impute missing values估算缺失值
【发布时间】:2014-04-12 04:10:48
【问题描述】:

我想估算一些数据。我使用 mvoutlier 包中的数据 moss。目标是从 Bi 列中估算 moss 日期是组成数据,所以我使用包 robCompositions 中的方法。当我尝试估算这些值时,出现错误。

代码:

    library(mvoutlier)
    library(robCompositions)
    data(moss)
    attach(moss)

    x <- moss[-c(1,2,3)] # copying the data from moss, withoud the first 3 variables into x
    x$Bi[Bi < 0.004] <- 0 # the values that are under 0.004 are replaced with 0
    res <- impRZilr(x,dl=c(0,0,0,0,0,0.004,rep(0,25)))
    |=======                                                               |  10%Error in !all.equal(x[!w], xOrig[!w]) : invalid argument type

不知道如何处理这个错误

【问题讨论】:

  • 我已经缩小了问题的范围,但我没有解决方案。这有效:res &lt;- impRZilr(x[,c(1:5,7:31)],dl=rep(0,30)),所以问题与 Bi 列有关。不过我不知道是什么。
  • 尝试用x$Bi[x$Bi &lt; 0.004] &lt;- 0替换x$Bi[Bi &lt; 0.004] &lt;- 0
  • @crmhaske 我尝试了你的建议,但是这样做我不会在结果变量中包含 Bi 列,所以它很无奈
  • @jlhoward 您的建议对结果没有影响
  • 是的,我并没有试图提供解决方案,只是为了演示,因为它在没有该列的情况下也可以工作,所以错误在 Bi 中......并且 jlhoward 为您找到了解决方案。

标签: r missing-data imputation


【解决方案1】:

OP 在编辑中写道:

我设法解决了这个问题,这就是我所做的:

   x <-moss[-c(1,2,3)]
   x$Bi[Bi <- 0.004] <- NA
   res <- impAll(x)

并且对象 res 包含估算矩阵。

【解决方案2】:
library(mvoutlier)
library(robCompositions)
data(moss)

x <- moss[-c(1,2,3)] #copying the data from moss, withoud the first 3 variables into x
### Before
head(x$Bi)
## [1] 0.002 0.039 0.012 0.033 0.002 0.052

# Impute below 0.004
x$Bi[x$Bi < 0.004] <- 0

## head(x$Bi)
## [1] 0.000 0.039 0.012 0.033 0.000 0.052

# Imputation
result <- impRZilr(x, dl = rep(0.004, nrow(x)))
res <- data.frame(result$x)

head(res$Bi)
## [1] 0.002515667 0.039000000 0.012000000 0.033000000 0.002836172 0.052000000

如您所见,0 的值被 impRZilr 函数值替换。

编辑

这里描述了如何根据您的 cmets 的要求访问结果。

# Imputation
# Use the verbose = TRUE option to see how the algorithm is iterating
result <- impRZilr(x, dl = rep(0.004, nrow(x)), verbose = TRUE)

### Results description
str(result)
# List of 7
# $ x       : num [1:598, 1:31] 0.016 0.073 0.032 0.118 0.038 ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : NULL
# .. ..$ : chr [1:31] "Ag" "Al" "As" "B" ...
# $ criteria: num 0.0203
# $ iter    : num 4
# $ maxit   : num 10
# $ wind    : logi [1:598, 1:31] FALSE FALSE FALSE FALSE FALSE FALSE ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : chr [1:598] "1" "2" "3" "4" ...
# .. ..$ : chr [1:31] "U" "Bi" "Th" "Tl" ...
# $ nComp   : int [1:4] 4 6 3 5
# $ method  : chr "pls"
# - attr(*, "class")= chr "replaced"

# Results data.frame with imputed ceros
res <- data.frame(result$x)

# Index of missing values
index_missing_wind <- data.frame(result$wind)

# Number of iterations
result$iter
# [1] 4

# Method used (you can change this)
result$method

【讨论】:

  • 我试过了,还是不行,停在30%。 impRZilr 方法的描述说结果应该具有这种形式 Value: xOrig : 原始数据框或矩阵 xImp : 估算数据 wind : 数据中缺失值的索引 iter : 迭代次数 eps : eps 但最后我没有得到这个结构
  • 我使用了 str(),但它没有那个结构
猜你喜欢
  • 2020-06-19
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
相关资源
最近更新 更多