1

我使用 aregImpute 来估算缺失值,然后我使用 impute.transcan 函数尝试使用以下代码获取完整的数据集。

impute_arg <- aregImpute(~ age + job + marital + education + default +
balance + housing + loan + contact + day + month + duration + campaign +
pdays + previous + poutcome + y , data = mov.miss, n.impute = 10 , nk =0)


imputed <- impute.transcan(impute_arg, imputation=1, data=mov.miss, list.out=TRUE, pr=FALSE, check=FALSE)
y <- completed[names(imputed)]

当我使用 str(y) 时,它已经给了我一个数据框,但是由于之前没有估算过NA,我的问题是如何在估算后获得没有 NA 的完整数据集?

str(y)
'data.frame':   4521 obs. of  17 variables:
 $ age      : int  30 NA 35 30 NA 35 36 39 41 43 ...
 $ job      : Factor w/ 12 levels "admin.","blue-collar",..: 11 8 5 5 2 5 7 10 3 8 ...
 $ marital  : Factor w/ 3 levels "divorced","married",..: 2 2 3 2 2 3 2 2 2 2 ...
 $ education: Factor w/ 4 levels "primary","secondary",..: 1 2 3 3 2 3 NA 2 3 1 ...
 $ default  : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 NA 1 1 1 ...
 $ balance  : int  NA 4789 1350 1476 0 747 307 147 NA -88 ...
 $ housing  : Factor w/ 2 levels "no","yes": NA 2 2 2 NA 1 2 2 2 2 ...
 $ loan     : Factor w/ 2 levels "no","yes": 1 2 1 2 NA 1 1 NA 1 2 ...
 $ contact  : Factor w/ 3 levels "cellular","telephone",..: 1 1 1 3 3 1 1 1 NA 1 ...
 $ day      : int  19 NA 16 3 5 23 14 6 14 NA ...
 $ month    : Factor w/ 12 levels "apr","aug","dec",..: 11 9 1 7 9 4 NA 9 9 1 ...
 $ duration : int  79 220 185 199 226 141 341 151 57 313 ...
 $ campaign : int  1 1 1 4 1 2 1 2 2 NA ...
 $ pdays    : int  -1 339 330 NA -1 176 330 -1 -1 NA ...
 $ previous : int  0 4 NA 0 NA 3 2 0 0 2 ...
 $ poutcome : Factor w/ 4 levels "failure","other",..: 4 1 1 4 4 1 2 4 4 1 ...
 $ y        : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
4

1 回答 1

1

我自己测试了你的代码,它工作得很好,除了最后一行:

y <- completed[names(imputed)]

我相信上面一行中有一个类型。另外,您甚至不需要该completed功能。

此外,如果您想从impute.transcan函数中获取 data.frame,则将其包装为as.data.frame

imputed <- as.data.frame(impute.transcan(impute_arg, imputation=1, data=mov.miss, list.out=TRUE, pr=FALSE, check=FALSE))

此外,如果您需要测试您的缺失数据模式,您还可以使用包md.pattern提供的功能mice

于 2017-03-09T13:57:27.183 回答