Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for Conditional imputation #152

Closed
simon-r-white opened this issue Nov 16, 2018 · 5 comments
Closed

Request for Conditional imputation #152

simon-r-white opened this issue Nov 16, 2018 · 5 comments

Comments

@simon-r-white
Copy link

Proposal to consider adding a method to implement conditional imputation.

When comparing mice() capabilities to the mi chained: within Stata, Stata includes the ability to set conditional's such that only certain elements are imputed based on another column (which itself may be imputed).

Within mice() we can specify a global where to skip imputing certain entries. However, this cannot handle the situation where the desire to skip imputing is conditional itself on another imputed entry.

I imagine one way to address this would be to add a pre type argument akin to the current post type argument, which the ability to adjust the missingness indicator matrix; but there might be a better (or more mice() way of achieveing this).

Another option would be to write custom mice.impute.xxx functions, but that seems extreme (and not very user friendly).

Thoughts?

@stefvanbuuren
Copy link
Member

stefvanbuuren commented Nov 18, 2018

There are several ways to do conditional imputation in mice. The simplest method is to impute all cells, and then overwrite imputed values that meet the condition with NA after imputation.

Example: Suppose in nhanes we want to impute chl only if hyp == 2.

# this will overwrite imputed chl in records with hyp == 2 by NA
library(mice)
imp <- mice(nhanes, print = FALSE, seed = 1)
cmp <- complete(imp, action = "long", include = TRUE)
cmp[cmp$.imp >= 1 & cmp$hyp == 2, "chl"] <- NA
imp2 <- as.mids(cmp)
imp2$imp

...

$hyp
   1 2 3 4 5
1  1 1 1 1 1
4  2 1 1 2 1
6  2 1 1 1 1
10 1 1 2 1 1
11 1 1 1 1 1
12 1 2 2 1 2
16 1 1 1 1 1
21 1 1 2 1 1

$chl
     1   2   3   4   5
1  187 187 187 238 131
4   NA 218 186  NA 187
10 218 187  NA 187 206
11 187 187 187 204 187
12 186  NA  NA 187  NA
15 187 187 187 131 187
16 187 187 238 238 187
20  NA  NA  NA  NA  NA
21 187 187  NA 206 187
24 218 284 206 186 218

It is straightforward to adapt this script to other conditions. Would this address your problem?

@stefvanbuuren
Copy link
Member

stefvanbuuren commented Nov 18, 2018

Related discussions using more complex approaches are in #43 and #125.

@stefvanbuuren stefvanbuuren changed the title Request for Conditional imputation (perhaps a "pre" argument to mirror "post"?) Request for Conditional imputation Nov 18, 2018
@simon-r-white
Copy link
Author

@stefvanbuuren, fully agree that the post argument works to solve part of this type of imputation (Stata's conditional option).

However, I believe that Stata's by option goes a bit further and restricts the rows used to perform the imputation see Stata manual.

For that type of conditional imputation the suggestion above (altering the values post imputation using the post argument) would include all rows of the data frame. Whereas a pre argument, where we could restrict the to a subset, might be one way to code this.

Really, I'm thinking about Stata's conceptual framework with the separation of the imputation and estimation samples, and how to best consider/implement this within mice()'s framework.

@stefvanbuuren
Copy link
Member

It's not entirely clear to me what problem you are trying to solve.

If you develop a small example, I would be happy to think about how mice could address the problem.

@stefvanbuuren
Copy link
Member

Closing in the anticipation of a reproducible example. Feel free to re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants