Error in UseMethod("select") : no applicable method for 'select' applied to an object of class "logical"

Hi everybody,

I would like to create a list containing each "X4" colum from each of the 5 imputed datasets (imputation made with the amelia() package) contained in the my_data_im$imputations list. I'm getting the following output:

> list_im_prep_A <- lapply(my_data_im$imputations, function(x) x %>% select("X4")) Error in UseMethod("select") : no applicable method for 'select' applied to an object of class "logical"

Sometimes i get the error and sometimes i don't... Any idea for solving this issue?

Thanks in advance!

### my code
library("tcltk")
library("Rcpp")
library("readxl")
library("carData")
library("car") 
library("dplyr")
library("tidyr")
library("broom")
library("ggplot2")
library("Amelia")
library("tidyverse")
library("ggpubr")
library("rstatix")
library(wrapr)
theme_local <- theme_minimal

### setwd("C:\\Users\\Desktop\\Article ")

### my_data <- read_excel("DATA.xlsx")

my_data <- wrapr::build_frame(
   "ID"   , "1", "2", "3", "4"     , "5" |
     "P1" , 100, 100, 100, 40      , 60  |
     "P2" , 100, 60 , 80 , 80      , 40  |
     "P3" , 100, 100, 100, 100     , 100 |
     "P4" , 40 , 90 , 60 , 40      , 0   |
     "P5" , 60 , 80 , 80 , 80      , 0   |
     "P6" , 100, 100, 100, 100     , 25  |
     "P7" , 90 , 100, 80 , NA_real_, 50  |
     "P8" , 80 , 100, 100, 80      , 75  |
     "P9" , 25 , 90 , 98 , 95      , 90  |
     "P10", 26 , 35 , 40 , 50      , 35  |
     "P11", 100, 100, 100, 80      , 30  )

my_data_im <- amelia(data.frame(my_data), m = 5, idvars = "ID", emburn = c(0, 1000), bound = rbind(c(5, 0, 100)))

list_im_prep_A <- lapply(my_data_im$imputations, function(x) x %>% select("X4")) 

I am not exactly sure what the problem in this case is. The provided code does work if you exchange the " with ':

library("Amelia")
#> Lade nötiges Paket: Rcpp
#> ## 
#> ## Amelia II: Multiple Imputation
#> ## (Version 1.8.0, built: 2021-05-26)
#> ## Copyright (C) 2005-2022 James Honaker, Gary King and Matthew Blackwell
#> ## Refer to http://gking.harvard.edu/amelia/ for more information
#> ##
library("tidyverse")
library('wrapr')
#> 
#> Attache Paket: 'wrapr'
#> Das folgende Objekt ist maskiert 'package:dplyr':
#> 
#>     coalesce
#> Die folgenden Objekte sind maskiert von 'package:tidyr':
#> 
#>     pack, unpack
#> Das folgende Objekt ist maskiert 'package:tibble':
#> 
#>     view

my_data <- wrapr::build_frame(
  "ID"   , "1", "2", "3", "4"     , "5" |
    "P1" , 100, 100, 100, 40      , 60  |
    "P2" , 100, 60 , 80 , 80      , 40  |
    "P3" , 100, 100, 100, 100     , 100 |
    "P4" , 40 , 90 , 60 , 40      , 0   |
    "P5" , 60 , 80 , 80 , 80      , 0   |
    "P6" , 100, 100, 100, 100     , 25  |
    "P7" , 90 , 100, 80 , NA_real_, 50  |
    "P8" , 80 , 100, 100, 80      , 75  |
    "P9" , 25 , 90 , 98 , 95      , 90  |
    "P10", 26 , 35 , 40 , 50      , 35  |
    "P11", 100, 100, 100, 80      , 30  )

my_data_im <- amelia(data.frame(my_data), m = 5, idvars = "ID", emburn = c(0, 1000), bound = rbind(c(5, 0, 100)))
#> Warning in amelia.prep(x = x, m = m, idvars = idvars, empri = empri, ts = ts, :
#> You have a small number of observations, relative to the number, of variables in
#> the imputation model. Consider removing some variables, or reducing the order of
#> time polynomials to reduce the number of parameters.
#> -- Imputation 1 --
#> 
#>  No missing data in bootstrapped sample:  EM chain unnecessary
#> -- Imputation 2 --
#> 
#>   1  2  3  4  5
#> 
#> -- Imputation 3 --
#> 
#>   1  2  3
#> 
#> -- Imputation 4 --
#> 
#>   1  2  3  4  5
#> 
#> -- Imputation 5 --
#> 
#>   1  2  3  4  5  6  7

list_im_prep_A <- lapply(my_data_im$imputations, function(x) x %>% select('X4')) 
list_im_prep_A
#> $imp1
#>           X4
#> 1   40.00000
#> 2   80.00000
#> 3  100.00000
#> 4   40.00000
#> 5   80.00000
#> 6  100.00000
#> 7   33.96579
#> 8   80.00000
#> 9   95.00000
#> 10  50.00000
#> 11  80.00000
#> 
#> $imp2
#>           X4
#> 1   40.00000
#> 2   80.00000
#> 3  100.00000
#> 4   40.00000
#> 5   80.00000
#> 6  100.00000
#> 7   87.40881
#> 8   80.00000
#> 9   95.00000
#> 10  50.00000
#> 11  80.00000
#> 
#> $imp3
#>             X4
#> 1   40.0000000
#> 2   80.0000000
#> 3  100.0000000
#> 4   40.0000000
#> 5   80.0000000
#> 6  100.0000000
#> 7    0.4941639
#> 8   80.0000000
#> 9   95.0000000
#> 10  50.0000000
#> 11  80.0000000
#> 
#> $imp4
#>           X4
#> 1   40.00000
#> 2   80.00000
#> 3  100.00000
#> 4   40.00000
#> 5   80.00000
#> 6  100.00000
#> 7   73.48801
#> 8   80.00000
#> 9   95.00000
#> 10  50.00000
#> 11  80.00000
#> 
#> $imp5
#>           X4
#> 1   40.00000
#> 2   80.00000
#> 3  100.00000
#> 4   40.00000
#> 5   80.00000
#> 6  100.00000
#> 7   74.45767
#> 8   80.00000
#> 9   95.00000
#> 10  50.00000
#> 11  80.00000

Created on 2022-08-24 by the reprex package (v2.0.1)

Hope this is helpful

Hi @FactOREO,

Thank you for giving me a hand. The problem is still occuring... As i said, sometimes it does'nt work and gets me the following error message, and sometimes it just does work.

> list_im_prep_A <- lapply(my_data_im$imputations, function(x) x%>% select('X4')) 
Error in UseMethod("select") : 
  no applicable method for 'select' applied to an object of class "logical"

The reprex, provided doesnt seem to reproduce your issue.
We are left with assumptions; there must a be failure case that does not produce a data.frame from which a select can be made. I have manufactured this arbitrarily. A solution might be to wrap the selecting function in a purrr::safely, so at least you get the other results.


my_data <- wrapr::build_frame(
  "ID"   , "1", "2", "3", "4"     , "5" |
    "P1" , 100, 100, 100, 40      , 60  |
    "P2" , 100, 60 , 80 , 80      , 40  |
    "P3" , 100, 100, 100, 100     , 100 |
    "P4" , 40 , 90 , 60 , 40      , 0   |
    "P5" , 60 , 80 , 80 , 80      , 0   |
    "P6" , 100, 100, 100, 100     , 25  |
    "P7" , 90 , 100, 80 , NA_real_, 50  |
    "P8" , 80 , 100, 100, 80      , 75  |
    "P9" , 25 , 90 , 98 , 95      , 90  |
    "P10", 26 , 35 , 40 , 50      , 35  |
    "P11", 100, 100, 100, 80      , 30  )

my_data_im <- amelia(data.frame(my_data), m = 5, idvars = "ID", emburn = c(0, 1000), bound = rbind(c(5, 0, 100)))

my_data_im$imputations[[1]] <- FALSE

list_im_prep_A <- lapply(my_data_im$imputations, function(x) x %>% select("X4")) 
library(purrr)
myselector <-  safely(.f = function(x) x %>% select("X4"),
                      otherwise = NA)

(list_im_prep_A <- lapply(my_data_im$imputations, 
                          myselector))
1 Like

I found the solution:

list_im_prep_A <- lapply(my_data_im$imputations, function(x) x%>% select('X4')) doesn't work when the list of imputed datasets my_data_im$imputations contains NA value (should be an issue from the imputation)... Then instead of:

list_im_prep_A <- lapply(my_data_im$imputations, function(x) x %>% select("X4"))

i did:

xo <- as.data.frame(my_data_im$imputations) #converts the list my_data_im$imputations into a dataframe xo

while (sum(is.na(xo))>0) {
  my_data_im <- amelia(data.frame(my_data), m = 5, idvars = "ID", emburn = c(0, 1000), bound = rbind(c(5, 0, 100)))
  xo <- as.data.frame(my_data_im$imputations)
} # run the imputation until the imputed datasets dataframe xo doesn't contain NA value anymore

list_im_prep_A <- select(xo, contains('X4')) #  in a cleaner way, creating a dataframe from xo, containing all the columns named "X4"

which works perfectly fine. The code doesn't gets the error message anymore.

Thank you all!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.