英文:
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 <- 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 <- 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,但如果 X1
为 1
,则将应用通常的 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 <- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论