簡體   English   中英

從R中的Hmisc包中提取完整的數據幀

[英]Extracting complete dataframe from Hmisc package in R

我使用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 ...

我自己測試了你的代碼,它的工作正常,除了最后一行:

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包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM