-1

I have use mice to impute data, save the data as csv, and then run a Factor Analysis in SPSS and generated some factors. I now want to load the csv in R and run an imputed linear regression on the data. However, when I try to convert the dataframe to mids I get and error message saying:

library(mice)

# assign mtcars to a new dataframe
df <- mtcars

# loop 10 times
for (x in 1:10){
  
  # create a fake imp number
  a <- rep(x, 1, nrow(df))
  
  # bind the fake imp number to the df
  df2 <- cbind(df, a)
  
  # crate a 10 folded version of mtcars with also the fake imp number
  if (x ==1){
    new_df <- df2
  } else{
    new_df <- rbind(new_df, df2)
  }
}

# change the column name of the fake imp to ".imp"
names(new_df)[names(new_df) == 'a'] <- '.imp'

# convert df to mids
df_imp <- as.mids(new_df, .imp = .imp)

> Error in as.mids(df) : Original data not found. Use `complete(...,
> action = 'long', include = TRUE)` to save original data.

Can you please help me with this error?

Nico Biagi
  • 21
  • 6

1 Answers1

1

From the as.mids() documentation.

This function converts imputed data stored in long format into an object of class mids. The original incomplete dataset needs to be available so that we know where the missing data are. The function is useful to convert back operations applied to the imputed data back in a mids object. It may also be used to store multiply imputed data sets from other software into the format used by mice.

The incomplete data is stored as imputation 0 in the long format. Therefore starting your procedure at 0 instead of 1 resolves the issue. (Also, you need quotes around .imp = '.imp' in the as.mids() call. Or, remove it and rely on the default. Or, just supply "a" as the imputation variable.)

library(mice)
df <- mtcars
for (x in 0:10){
  a <- rep(x, 1, nrow(df))
  df2 <- cbind(df, a)
  if (x == 0){
    new_df <- df2
  } else{
    new_df <- rbind(new_df, df2)
  }
}
names(new_df)[names(new_df) == 'a'] <- '.imp'
df_imp <- as.mids(new_df)