Search code examples
rimputationr-mice

When I run complete(), I am getting the an error- Error in (function (classes, fdef, mtable)


I have a dataset which looks like this(A-J are column names)

A   B   C   D   E   F   G   H   I   J
1   2   2   3   2   1   1   1   1
2   1   1   1   1   1   1   1   1   1
2   1   2   2   2   2   2   2   1   1
2   1   2   1   1   1   1   1   1   1
2   1       3   3   3   2   2   2   2
2   1   3   2   2   3   1   1   1   1
    1   3   2   1   2   2   2   1   2
2   1   2   2   2   2   2   2   1   1
1   2   2   2   2   1   1   1   1
2   1   2   1   1   1   2   1   1   1
2   1   1   1   1   1   2   2   1   1
2   1   2   1   1   1   1   1   1   2
2   1   1   1           1   1   1   1
2   1   3   3   3   3   1   1   1   2
    1   2   2   1   2   1   1   1   1
    1   2   2   2   2   2   2   1   1
2   2   4   1   1   1   2   2   1   1
1   1   3   3   3   3                
2   1   3   3   1   2   2   2   2   3

I am getting the below error-

Error in (function (classes, fdef, mtable)  : unable to find an inherited method for function ‘complete’ for signature ‘"mids", "numeric"’

My data has lot of NULL values and I am trying to impute the data using below code-

imp_data<-mice(data = data_NA, m = 5, method = "rf", maxit = 5, seed = 500)

I get the error when I run the code-

complete(imp_data,1)

Please suggest where I am doing wrong


Solution

  • It seems that NA values are not properly assigned in the data_NA data.frame which is causing the problem.

    The modified data (with NA) and transforming it using mice as it worked for me:

    library(mice)
    
    imp_data <- mice(data = data_NA, m = 5, method = "rf", maxit = 5, seed = 500)
    complete(imp_data, 1)
    

    EDITED: The error seen by OP was resolved by changing the call as:

    mice::complete(imp_data, 1) 
    

    May be the mice::complete was masked by some function other package.

    #Result
    # A B C D E F G H I J
    # 1  1 2 2 3 2 1 1 1 1 2
    # 2  2 1 1 1 1 1 1 1 1 1
    # 3  2 1 2 2 2 2 2 2 1 1
    # 4  2 1 2 1 1 1 1 1 1 1
    # 5  2 1 1 3 3 3 2 2 2 2
    # 6  2 1 3 2 2 3 1 1 1 1
    # 7  1 3 2 1 2 2 2 1 2 1
    # 8  2 1 2 2 2 2 2 2 1 1
    # 9  1 2 2 2 2 1 1 1 1 1
    # 10 2 1 2 1 1 1 2 1 1 1
    # 11 2 1 1 1 1 1 2 2 1 1
    # 12 2 1 2 1 1 1 1 1 1 2
    # 13 2 1 1 1 1 1 1 1 1 1
    # 14 2 1 3 3 3 3 1 1 1 2
    # 15 1 2 2 1 2 1 1 1 1 1
    # 16 1 2 2 2 2 2 2 1 1 1
    # 17 2 2 4 1 1 1 2 2 1 1
    # 18 1 1 3 3 3 3 2 1 2 1
    # 19 2 1 3 3 1 2 2 2 2 3
    # 
    

    Data

    data_NA<- read.table(text = 
    "A   B   C   D   E   F   G   H   I   J
    1   2   2   3   2   1   1   1   1    NA
    2   1   1   1   1   1   1   1   1   1
    2   1   2   2   2   2   2   2   1   1
    2   1   2   1   1   1   1   1   1   1
    2   1   NA    3   3   3   2   2   2   2
    2   1   3   2   2   3   1   1   1   1
    1   3   2   1   2   2   2   1   2   NA
    2   1   2   2   2   2   2   2   1   1
    1   2   2   2   2   1   1   1   1   NA
    2   1   2   1   1   1   2   1   1   1
    2   1   1   1   1   1   2   2   1   1
    2   1   2   1   1   1   1   1   1   2
    2   1   1   1   NA  NA  1   1   1   1
    2   1   3   3   3   3   1   1   1   2
    1   2   2   1   2   1   1   1   1   NA
    1   2   2   2   2   2   2   1   1   NA
    2   2   4   1   1   1   2   2   1   1
    1   1   3   3   3   3   NA  NA  NA  NA             
    2   1   3   3   1   2   2   2   2   3",header = TRUE)