MICE填补受限条件

huangapple go评论74阅读模式
英文:

Conditioned MICE imputation / with restrictions

问题

Here's the translation of the relevant content:

我有一个这样的数据集:

d <- data.frame(X1 = c(1, 1, NA, NA, 0, NA, NA, 1, 0),
                X2 = c(NA, 0, NA, NA, 0, NA, NA, 1, 0))

我想要根据X1的未来估计值执行mice插补,并根据以下条件进行插补:

  • 如果X1 = 0 --> X2 = 0
  • 如果X1 = 1 --> X2 = 0或1

如何编写这个条件?我的插补基本代码是:

library(mice)
imp <- mice(d, seed=123, m=5, maxit=10)

我在理解上遇到了困难。谢谢。

英文:

I have a dataset like this:

d &lt;- data.frame(X1 = c(1, 1, NA, NA, 0, NA, NA, 1, 0),
                X2 = c(NA, 0, NA, NA, 0, NA, NA, 1, 0))

  X1 X2
1  1 NA
2  1  0
3 NA NA
4 NA NA
5  0  0
6 NA NA
7 NA NA
8  1  1
9  0  0

I want to perform mice imputation with the following condition based on the future imputed values of X1:

  • If X1 = 0 --> X2 = 0
  • If X1 = 1 --> X2 = 0 or 1

How can I write this condition? The base code for my imputation is:

library(mice)
imp &lt;- mice(d, seed=123, m=5, maxit=10)

I am having troubles in understand. Thank you

答案1

得分: 1

我们可以使用Heymans和Eekhout(2019年)所描述的被动填补方法,通过指定填补方法来实现。如果 X1 为 0,则 X2 将始终为 0,但如果 X11,则将应用通常的 pmm 方法(或您指定的任何方法)。

library(mice)
set.seed(123)

d <- data.frame(X1 = c(1, 1, NA, NA, 0, NA, NA, 1, 0),
                X2 = c(NA, 0, NA, NA, 0, NA, NA, 1, 0))

fakeimp <- mice(d, seed = 123, maxit = 0, printFlag = FALSE)
meth <- fakeimp$method
meth["X2"] <- "~I((ifelse(X1 == 0, 0, X2)))"

imp <- mice(d, seed = 123, m = 5, maxit = 10, method = meth, printFlag = FALSE)

full <- complete(imp, action = "long", include = FALSE)
head(full)
#>   .imp .id X1 X2
#> 1    1   1  1  1
#> 2    1   2  1  0
#> 3    1   3  1  0
#> 4    1   4  0  0
#> 5    1   5  0  0
#> 6    1   6  0  0
英文:

We can use a passive imputation approach as described by Heymans and Eekhout (2019) by specifying the imputation method. If X1 is 0, X2 will always be 0, but if X1 is 1, then the usual pmm method (or whatever method you specify) applies.

library(mice)
set.seed(123)

d &lt;- data.frame(X1 = c(1, 1, NA, NA, 0, NA, NA, 1, 0),
                X2 = c(NA, 0, NA, NA, 0, NA, NA, 1, 0))

fakeimp &lt;- mice(d, seed = 123, maxit = 0, printFlag = FALSE)
meth &lt;- fakeimp$method
meth[&quot;X2&quot;] &lt;- &quot;~I((ifelse(X1 == 0, 0, X2)))&quot;

imp &lt;- mice(d, seed = 123, m = 5, maxit = 10, method = meth, printFlag = FALSE)

full &lt;- complete(imp, action = &quot;long&quot;, include = FALSE)
head(full)
#&gt;   .imp .id X1 X2
#&gt; 1    1   1  1  1
#&gt; 2    1   2  1  0
#&gt; 3    1   3  1  0
#&gt; 4    1   4  0  0
#&gt; 5    1   5  0  0
#&gt; 6    1   6  0  0

huangapple
  • 本文由 发表于 2023年6月15日 03:55:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定