Search code examples
rimputationr-mice

Unable to pool mean values in mice using R: no glance method of object class numeric


I am trying to impute the mean of a continuous variable using the mice package in R. I am able to generate m imputed mean values but when I try to aggregate these using the pool() function I get an error message: "Error: No glance method for objects of class numeric".

I have been able to use the pool function when aggregating the results of a linear regression model but not for mean/median values or counts of categorical data.

I've tried the iris dataset as well as my own data. I will use the iris data for illustration of the problem

library(missForest) # for the prodNA function
library(mice)       # for the imputations

#Creating dataset with missing values (NAs)
iris.mis <- prodNA(iris, noNA = 0.1)
head(iris.mis)

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2           NA         3.0           NA         0.2    <NA>
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5           NA         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

#Imputation
imputed_data <- mice(data = iris.mis, m = 5, method = "pmm", maxit = 50, seed = 500)
mean_sepal_width <- with(data = imputed_data, expr = mean(Sepal.Width))
print(mean_sepal_width)
summary(pool(mean_sepal_width))

The summary(pool()) command gives the error message: "Error: No glance method for objects of class numeric".

I've also tried without the summary function but that doesn't work either i.e.

pool(mean_sepal_width)

Further, I've tried converting the mean_sepal_width object (which is of class mira and matrix) to a vector and then finding the mean and median value (not quite the same as Rubin's rule for imputation but given the similarity of the imputed values in my dataset as well as the iris example shown here this would be acceptable for the purpose of this project). Unfortunately that doesn't work either. For example:

mean(as.vector(mean_sepal_width))

The above also gives an error message: "Warning message: In mean.default(as.vector(mean_sepal_width)) : argument is not numeric or logical: returning NA"

There is a similar question on stackoverflow for an imer test: mice's pool.compare gives "Error: No glance method for objects of class call" for lmerTest models but the proposed answer does not work for me either.

Perhaps it is just not possible to do this in mice?

I would be very grateful for any advice on how to tackle this issue. Many thanks in advance.


Solution

  • For your second method, taking the mean of a vector, you can try mean(unlist(mean_sepal_width$analyses)). I am not sure the first method, using pool, will work without fitting a model.

    library(pacman)
    p_load(mice)
    p_load(missForest)
    
    set.seed(12345)
    
    #Creating dataset with missing values (NAs)
    iris.mis <- prodNA(iris, noNA = 0.1)
    head(iris.mis)
    #>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    #> 1          5.1         3.5          1.4          NA  setosa
    #> 2          4.9         3.0          1.4         0.2  setosa
    #> 3          4.7         3.2          1.3         0.2  setosa
    #> 4          4.6          NA          1.5         0.2  setosa
    #> 5          5.0         3.6          1.4         0.2    <NA>
    #> 6          5.4         3.9          1.7         0.4  setosa
    
    #Imputation
    imputed_data <- mice(data = iris.mis, m = 5, method = "pmm", maxit = 50, seed = 500)
    
    mean_sepal_width <- with(data = imputed_data, expr = mean(Sepal.Width))
    
    print(mean_sepal_width)
    #> call :
    #> with.mids(data = imputed_data, expr = mean(Sepal.Width))
    #> 
    #> call1 :
    #> mice(data = iris.mis, m = 5, method = "pmm", maxit = 50, seed = 500)
    #> 
    #> nmis :
    #> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
    #>           20            8            9           21           17 
    #> 
    #> analyses :
    #> [[1]]
    #> [1] 3.059333
    #> 
    #> [[2]]
    #> [1] 3.050667
    #> 
    #> [[3]]
    #> [1] 3.06
    #> 
    #> [[4]]
    #> [1] 3.065333
    #> 
    #> [[5]]
    #> [1] 3.052
    
    # look at the structure of the mean_sepal_width object
    str(mean_sepal_width)
    #> List of 4
    #>  $ call    : language with.mids(data = imputed_data, expr = mean(Sepal.Width))
    #>  $ call1   : language mice(data = iris.mis, m = 5, method = "pmm", maxit = 50, seed = 500)
    #>  $ nmis    : Named int [1:5] 20 8 9 21 17
    #>   ..- attr(*, "names")= chr [1:5] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" ...
    #>  $ analyses:List of 5
    #>   ..$ : num 3.06
    #>   ..$ : num 3.05
    #>   ..$ : num 3.06
    #>   ..$ : num 3.07
    #>   ..$ : num 3.05
    #>  - attr(*, "class")= chr [1:2] "mira" "matrix"
    
    # the estimates are held in "analyses", in a list, so take the mean of these
    mean(unlist(mean_sepal_width$analyses))
    #> [1] 3.057467
    

    Created on 2020-02-10 by the reprex package (v0.3.0)