Closed isaactpetersen closed 1 year ago
The pred
error you receive is not related to mice
, but relates to pan::pan()
. Its third argument (pred
) expects a fully observed predictor space. With the specified where
matrix you have deliberately chosen to leave this predictor space incomplete. See addition to the example below:
library("mice")
temp <- tempfile()
download.file("https://github.com/amices/mice/files/9982439/df.zip", temp)
load(unz(temp, "df.rdata"))
unlink(temp)
# Variables to Impute
Y <- c("v1","v2")
# Multilevel Imputation Methods
meth <- make.method(df)
meth[1:length(meth)] <- ""
meth[Y] <- "2l.pan"
# Predictor Matrix
banana <- make.predictorMatrix(df)
banana[1:nrow(banana), 1:ncol(banana)] <- 0
banana[Y, "id"] <- (-2) #cluster variable
banana[Y, "age"] <- 2 #random effect predictor
banana[Y, Y] <- 1 #fixed effect predictor
diag(banana) <- 0 #don't let variable predict itself
table(is.na(banana))
#>
#> FALSE
#> 25
# Specify cells to impute
whereMatrix <- is.na(df)
whereMatrix[which(df$wave >= 6), ] <- FALSE
# Perform Multiple Imputation
midata <- mice(
data.frame(df),
method = meth,
predictorMatrix = banana,
m = 1,
maxit = 1,
seed = 52242,
where = whereMatrix)
#>
#> iter imp variable
#> 1 1 v1
#> Error in pan::pan(y1, subj, pred, xcol, zcol, prior, seed = s1, iter = paniter): missing values in pred not allowed
# Perform Multiple Imputation without where
midata <- mice(
data.frame(df),
method = meth,
predictorMatrix = banana,
m = 1,
maxit = 1,
seed = 52242)
#>
#> iter imp variable
#> 1 1 v1 v2
Created on 2022-11-10 with reprex v2.0.2
FWIW: mice
uses mitml::panImpute()
internally. Perhaps @simongrund1 can shine some light if using a where
strategy to omit some cases to be imputed would be desired behaviour with mitml::panImpute()
.
Thanks for the prompt response. Does that mean it is not possible to use the where
argument with 2l.pan
? I would like to use the where
argument to specify a subset of rows to impute. I do not want to filter out rows before fitting the model because I want all data to inform the imputation for the subset of rows. Does that make sense?
Yes, but not if the method expects the column to be completely observed.
If you'd like the incomplete data to inform the imputations, then why would you not draw values for those informers? They're coming from the same posterior predictive distribution, after all. Information that is not there can also not inform if left unobserved.
Internally in 'mice' there is the possibility for a difference between the set used for parameter estimation and the set to be imputed. The set used for parameter estimation is then, naturally, made complete behind the scenes. The caveat is now that the method for generating the imputations gets parsed the incomplete predictor space. And it does not accept that. So, unfortunately you cannot use the 'where' argument in this case.
That said, if your goal is to exclude the flow of information in your analysis, you may choose to still impute all and analyze some. The models will still be congenial, then.
I don't think it matters whether "2l.pan"
uses pan::pan
oder mitml::panImpute
internally, because panImpute
just arranges the outcome and predictor matrices and then calls pan::pan
. PAN requires complete predictors. Any workaround would require splitting the estimation vs. imputation part of the method like it is for "norm"
an other methods that support where
. This would need to be done in mice
though.
In the case described above, where v1
and v2
are missing simultaneously, and all other variables are complete, I believe the where
strategy is equivalent with just running mice
on the wave <= 5
subset. More generally, if the aim is to leave values in a variable unimputed while still using that variable to inform imputations for the same units on other variables, then one could impute the variable anyway and delete the values afterwards. But then again, I'm no expert on the where
feature, so maybe there's a better way.
Thanks, @simongrund1.
@isaactpetersen thanks for raising this issue. I have proposed a note to the where
argument documentation. See #521. Closing now as it is not related to mice
.
Agreed that a note to the documentation would be helpful. Thanks for clarifying!
I'm trying to perform multilevel imputation using
2l.pan
and to use thewhere
argument to specify which cells to impute. I receive the following error:However, the error is odd because there are no missing values in the
pred
object. I did some troubleshooting, and I do not receive the error when I remove thewave
variable (which is not used in the prediction model) from the dataframe. In addition, the error does not occur when I don't use thewhere
argument.Below is a minimal reproducible example. Attached is an example datafile.
Created on 2022-11-10 with reprex v2.0.2