I've been struggling with a problem in mice
trying to impute multilevel data with missing data on both levels. It took me some time but I finally managed to recreate the error, which seems to occur when mice
tries to create logged events in the sampler
function. I have one variable which indicates the test version, but is only relevant for the first measurement (after that it's a constant). This variable seems to cause the error.
When using "2lonly.pmm"
as imputation method for a variable mice
returns the error: Error in get("state", parent.frame(frame)) : object 'state' not found
The use of any other method does not result in an error. Normally mice
would create a logged event stating that the variable (or some level in the case of a factor) is deleted from the imputation process. But somehow it does not create a logged event when method is "2lonly.pmm"
. Any help in resolving this issue is greatly appreciated
This is the dataset from the mice.impute.2lonly.pmm
help page:
G <- 250 # number of groups
n <- 20 # number of persons
beta <- .3 # regression coefficient
rho <- .30 # residual intraclass correlation
rho.miss <- .10 # correlation with missing response
missrate <- .50 # missing proportion
y1 <- rep( rnorm( G , sd = sqrt( rho ) ) , each=n ) + rnorm(G*n , sd = sqrt( 1 - rho ))
w <- rep( round( rnorm(G ) , 2 ) , each=n )
v <- rep( round( runif( G , 0 , 3 ) ) , each=n )
x <- rnorm( G*n )
y <- y1 + beta * x + .2 * w + .1 * v
dfr0 <- dfr <- data.frame( "group" = rep(1:G , each=n ) , "x" = x , "y" = y , "w" = w , "v" = v )
dfr[ rho.miss * x + rnorm( G*n , sd = sqrt( 1 - rho.miss ) ) < qnorm( missrate ) , "y" ] <- NA
dfr[ rep( rnorm(G) , each=n ) < qnorm( missrate ) , "w" ] <- NA
dfr[ rep( rnorm(G) , each=n ) < qnorm( missrate ) , "v" ] <- NA
This is a recreation of the type of variables that create the error
dfr$test <- rep(1:20,length(unique(dfr$group)))
dfr$version[dfr$test == 1]<- sample(0:2,length(unique(dfr$group)),replace = T)
dfr$version[dfr$test > 1]<- 3 # test
And the imputation process
# empty mice imputation
imp0 <- mice(dfr , maxit=0 )
predM <- imp0$predictorMatrix # Predictor matrix
impM <- imp0$method # Method
#...
# multilevel imputation
predM[c("y","v"),"group"] <- -2 # indicate grouping variable
impM[c("y","w","v")] <- c("2l.pan" , "pmm" , "2lonly.pmm" )
# y ... imputation using 2l.pan
# w ... imputation at level 2 using pmm
# v ... imputation at level 2 using 2lonly.pmm
imp <- mice(dfr, m = 1, pred = predM ,
method= impM, maxit = 1)
I'm using mice
version 3.0.0 and R 3.5.0