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 函数值替换。
编辑
以下是有关如何根据您的评论要求访问结果的说明。
# 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