2
votes

I want to impute some data. I use the data moss from the package mvoutlier. The goal is to impute the values < 0.004 from the column Bi. Because the moss date are compositional data, I use methods from the package robCompositions. When I try to impute the values, I get an error.

Code:

    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

Don't know how to handle this error

2
I've narrowed down the problem, but I don't have a solution. This works: res <- impRZilr(x[,c(1:5,7:31)],dl=rep(0,30)), so the problem has to do with the Bi column. I don't have a clue what though. - Christie Haskell Marsh
Try replacing x$Bi[Bi < 0.004] <- 0 with x$Bi[x$Bi < 0.004] <- 0 - jlhoward
@crmhaske I tried what you suggested, but doing that I won't have the column Bi in the result variable, so it's helpless - Paul
@jlhoward your suggestion has no effect on the result - Paul
Ya, I wasn't attempting to offer a solution, merely to demonstrate since it works without that column the error is in Bi... and jlhoward found the solution for you. - Christie Haskell Marsh

2 Answers

0
votes
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

As you can see, the values that were 0 are replaced by the impRZilr function values.

EDIT

Here is a description of how to access the results as required in your comments.

# 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
0
votes

The OP wrote in an edit:

I managed to solve the problem, this is what I did:

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

and the object res contains the imputed matrix.