simongrund1/mitml

Obtain original row order in the imputed data

Closed this issue · 2 comments

The panImpute function resorts columns and rows. I am trying to figure out how to get the original order, but thus far no luck. What would be the advised way to do this?

library(mitml)
library(mice)

head(nhanes)
type <- c(1, 1, -2, 1)

obj <- panImpute(data = nhanes, type = type, m = 1)

# the object returned by panImpute resorts rows and columns
cmp1 <- mitmlComplete(obj, print = 1)
head(cmp1)

# try 1: get original row and column order
cmp2 <- cmp1[order(attr(obj$data, "sort")), names(nhanes)]
head(cmp2)

# try 2: get original row and column order
cmp3 <- cmp1[attr(obj$data, "sort"), names(nhanes)]
head(cmp3)

Thanks for reporting this, Stef. In general, mitmlComplete is supposed to return the data set with rows in their original order, so this is a bug. I uploaded a small change that addresses this issue, and now I obtain the original row order in the completed data sets as intended:

library(mitml)
library(mice)

type <- c(-2, 1, 1, 1)
obj <- panImpute(data = nhanes, type = type, m = 1)

# identical row order in original and completed data
mitmlComplete(obj, print = 1)
nhanes

Regarding your other question: panImpute reorders the columns according to the role of the variables in the imputation model. This is intended, mostly due to applications where new variables are created by panImpute. However, if you want to restore the original column order, you can use one of the following to solutions:

type <- c(1, 1, -2, 1)
obj <- panImpute(data = nhanes, type = type, m = 1)

# columns reordered by panImpute
cmp <- mitmlComplete(obj, print = 1)

# Option 1: direct matching
cmp[ , colnames(nhanes) ]

# Option 2: intersect()
cmp[ , intersect(colnames(nhanes),colnames(cmp)) ]

It would be possible to automatize this with an additional argument, but I prefer the current behavior.
Does this work for you?

Thanks for the quick fix on the rows. Works perfectly.

As a user, I'd generally prefer to govern the column order myself. I would be a little annoyed if the software changes the order. But of course, it's no big deal since it's easy to revert it.