从R中的Hmisc包中提取完整的数据帧

时间:2017-03-07 11:46:48

标签: r hmisc

我使用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)时它已经给了我一个数据帧,但是 NAs ,因为之前没有估算,我的问题是如何在插补后获得没有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 ...

1 个答案:

答案 0 :(得分:0)

我自己测试了你的代码,除了最后一行外,它的工作正常:

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函数。