Mennatallah Ibrahim
Mennatallah Ibrahim

Reputation: 43

Extracting complete dataframe from Hmisc package in R

I've used aregImpute to impute the missing values then i used impute.transcan function trying to get complete dataset using the following code.

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)]

and when i used str(y) it already gives me a dataframe but with NAs as it is not imputed before, My question is how to get complete dataset without NAs after imputation?

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 ...

Upvotes: 1

Views: 1198

Answers (1)

Ahmadov
Ahmadov

Reputation: 1607

I have tested your code myself, and it works just fine, except for the last line:

y <- completed[names(imputed)]

I believe there's a type in the above line. Plus, you do not even need the completed function.

Besides, if you want to get a data.frame from the impute.transcan function, then wrap it with as.data.frame:

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

Moreover, if you need to test your missing data pattern, you can also use the md.pattern function provided by the mice package.

Upvotes: 1

Related Questions