Search code examples
rroundingr-mice

Round function no longer works with mice output


Since updating R to 4.0.0 and re-installing mice it seems that the round() function no longer works on mice output but yields an error message.

For example (using iris dataset):

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)

#Imputation
imputed_data <- mice(data = iris.mis, m = 5, method = "pmm", 
    maxit = 50, seed = 500)
model <- with(data = imputed_data, lm(Sepal.Length ~ Sepal.Width))
round(summary(pool(model), conf.int = TRUE, exponentiate = TRUE), 2)

This yields the error message:

Error in Math.data.frame(list(term = 1:2, estimate = c(760.13466726231, : non-numeric variable(s) in data frame: term

Using summary(pool(model), conf.int = T, exponentiate = T) works just fine.

Before updating R and mice I've never had issues with the round function in R.


Solution

  • I don't know exactly why this would have changed, but the NEWS file for mice says in version 3.8

    There is now a more flexible pool() function that integrates better with the broom and broom.mixed packages.

    It makes sense that row names would have been changed to a term column instead, as this is more compatible with tidyverse machinery (like broom and broom.mixed).

    You can ask R to round all but the first column:

    ss <- summary(pool(model), conf.int = TRUE, exponentiate = TRUE)
    ss[-1] <- round(ss[-1],2)
    ss
    ##          term estimate std.error statistic     df p.value  2.5 %  97.5 %
    ## 1 (Intercept)   670.98      0.48     13.69 143.68    0.00 262.19 1717.15
    ## 2 Sepal.Width     0.80      0.15     -1.44 143.36    0.15   0.59    1.09
    

    If you like tidyverse you could

    mutate_if(ss,is.numeric, round, 2)