Here is just example data:
# generation of correlated data
matrixCR <- matrix(NA, nrow = 100, ncol = 100)
diag(matrixCR) <- 1
matrixCR[upper.tri (matrixCR, diag = FALSE)] <- 0.5
matrixCR[lower.tri (matrixCR, diag = FALSE)] <- 0.5
matrixCR[1:10,1:10]
L = chol(matrixCR)# Cholesky decomposition
nvars = dim(L)[1]
nobs = 200
set.seed(123)
rM = t(L) %*% matrix(rnorm(nvars*nobs), nrow=nvars, ncol=nobs)
rM1 <- t(rM)
rownames(rM1) <- paste("S", 1:200, sep = "")
colnames(rM1) <- paste("M", 1:100, sep = "")
# introducing missing value to the dataset
N <- 2000*0.05 # 5% random missing values
inds <- round ( runif(N, 1, length(rM1)) )
rM1[inds] <- NA
# using random forest implemented in mice package
require(mice)
out.imp <- mice(rM1, m = 5, method ="rf")
imp.data <- complete(out.imp)
I am getting following error:
iter imp variable
1 1 M1 M2Error in apply(forest, MARGIN = 1, FUN = function(s) sample(unlist(s), :
dim(X) must have a positive length
I am not sure what is causing this problem ?
mice
function seems to be throwing an error whenever it gets to a column with only a single missing value. I figured this out by running your code and successively excluding the columns where it failed. Then I compared the failing and succeeding columns and the only difference was that the failing columns had only oneNA
value. When I recreated your matrix, but with a 50% chance of a missing value (so no column would have only oneNA
) the code ran fine. I don't know if this is a bug or if there's some reason why having only oneNA
in a column should cause an error.