Repository navigation
MICE on train/test split for machine learning prediction #755
Replies: 2 comments 4 replies
|
Your approach is correct — the ignore = TRUE flag is exactly right. The issue is with this line: This tells MICE to not impute Target at all, so test rows with NA Target stay NA. That's actually fine and expected — you don't want to impute the target. |
|
Hi @AlirezaShiraniBidabadi Thanks for your interest in using the mice package. We are currently finalizing a preprint about this problem. I'm assuming the target is completely observed in your training data, and here only the predictors need to be imputed. In the test data, predictors can be missing and the target is obviously not available. The solution is as follows. You can marginalize over the distribution of the outcome variable. In other words, you treat the outcome in the test set as a missing value that you impute. These imputations serve solely as a vehicle to impute the missing test predictors, and can be discarded afterwards. In this way, you can use the relation between the target variable and the missing data to create more accurate imputations, resulting in a better prediction model and ultimately in better predictions. Hope this helps! |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
hello everyone i am using mice to generate five imputed datasets (five train and five test) then i would use an AI model for each data and finally using avg( based on rubin's rule) i will combine the predictions. now my question is how should i treat the target column. if i keep it for train data and make it null for test data then there is an inconsistency since when the imputed model look at null in test data it would probably return null and therefore the test data remains not imputed(this is my case) but in the document i have seen that it is recommended to keep the target col in train data. now i am also thinking of filling the target col for test with noise(random selection of train labels) i could not find any specific doc on this i found that you should use ignore to make sure test data is not present during imputation but how about the target col? here is my code ''
library(mice)
library(caret)
library(haven)
continuous_vars <- c("features")
binary_vars <- c("features"")
categorical_vars <- c("features")
Fix types
df[continuous_vars] <- lapply(df[continuous_vars], as.numeric)
df[binary_vars] <- lapply(df[binary_vars], as.factor)
df[categorical_vars] <- lapply(df[categorical_vars], as.factor)
df$Target<- as.factor(df$Target)
set.seed(123)
....
....
....
...code for splitting....
train_data <- df[ train_idx, ]
test_data <- df[-train_idx, ]
train_target <- train_data$Target
test_target <- test_data$Target
train_data$Target <- train_target
test_data$Target <- NA
── 3. Stack train + test; mark test rows with ignore = TRUE ───────────────
n_train <- nrow(train_data)
n_test <- nrow(test_data)
combined <- rbind(train_data, test_data)
ignore_flags <- c(rep(FALSE, n_train),
rep(TRUE, n_test))
── 4. Configure imputation methods ───────────────────────────────────────
ini <- mice(combined, maxit = 0, printFlag = FALSE)
meth <- ini$method
meth[continuous_vars] <- "pmm"
meth[binary_vars] <- "logreg"
meth[categorical_vars] <- "polyreg"
meth["Target"] <- ""
── 5. Run MICE ────────────────────────────────────────────────────────────
cat("-> Running MICE (Target as predictor in train, absent in test)...\n")
mice_fit <- mice(
combined,
m = 5,
maxit = 10,
method = meth,
ignore = ignore_flags,
printFlag = FALSE,
seed = 123
)
── 6. Extract train and test using mice filter() ─────────────────────────
mice_train <- filter(mice_fit, !ignore_flags)
mice_test <- filter(mice_fit, ignore_flags)
for (m_idx in 1:5) {
final_train <- complete(mice_train, m_idx)
final_test <- complete(mice_test, m_idx)
rownames(final_train) <- NULL
rownames(final_test) <- NULL
final_train$Target<- train_target
final_test$Target<- test_target
final_train$Set_Type <- "Train"
final_test$Set_Type <- "Test"
...saving files ''''' but in my case test data stays unimputed.
All reactions