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

First predictor is dropped when using "formulas" #305

Closed
jawitte opened this issue Jan 20, 2021 · 3 comments
Closed

First predictor is dropped when using "formulas" #305

jawitte opened this issue Jan 20, 2021 · 3 comments

Comments

@jawitte
Copy link

jawitte commented Jan 20, 2021

Hello,

I noticed that when I use the "formulas" argument of "mice", then the first predictor gets lost along the way. I think it happens within mice:::sampler.univ. My formula is "X ~ 0 + Y", which is updated to "X ~ Y - 1" in mice:::sampler.univ. Then the design matrix x is obtained, which has one column containing the values of Y. So far, so good; but then the first and only column is removed:
x <- x[, -1L, drop = FALSE]
Thus Y is not used to predict X.

The issue does not occur when I use the "predictorMatrix" argument to specify the same imputation model.

Here is an example illustrating the problem:
Example_for_GitHub.txt

I would very much appreciate if you could have a look at the issue!

@prockenschaub
Copy link
Contributor

This does indeed seem to be a bug. It looks like the intercept (or what mice thinks to be the intercept) is removed twice:

  1. in make.formulas by adding 0 +
  2. in sampler.univ by removing the first column of the design matrix

My best guess is that this was changed at some point in sampler.univ and the corresponding code in make.formulas somehow slipped through the cracks.

Suggestion:
Change formulas[[h]] <- paste(paste(y, collapse = "+"), "~", paste(c("0", x), collapse = "+")) to formulas[[h]] <- paste(paste(y, collapse = "+"), "~", paste(x, collapse = "+")) in sampler.univ

Happy to make a PR if that seems like a reasonable solution.

@jawitte
Copy link
Author

jawitte commented Mar 8, 2021

Yes, I think that would fix the issue! If you don't mind, would you make a pull request? Thanks :)

@stefvanbuuren
Copy link
Member

Now merged. Thanks!

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

3 participants