I am unable to reproduce the error in question and cannot attach my full data. Any help understanding the error received would be appreciated.
After imputation, I make several changes to the imputed datasets and would like to convert the transformed data to a mids object for modeling. However, I get the following error:
Error in `[<-.data.frame`(`*tmp*`, j, value = list(variable = c(0.9998, :
replacement element 1 has 78 rows, need 86
Below is a toy example of steps I take:
library(mice)
library(dplyr)
df <- data.frame(
id = rep(1:3, 3),
a = sample(c(NA, 1, 0), size=9, replace = T),
b = sample(c(NA, 1:10), size=9, replace = T)
)
# perform imputation
imp <- mice(df)
# get complete data + transform
comp <- complete(imp, action="long", include = T) %>%
filter(id %in% 1:2) %>%
rowwise() %>%
mutate(add = sum(a,b, na.rm = T)) %>%
dplyr::ungroup()
# convert to mids object - error encountered here
to_mids <- as.mids(comp)