bluefoxr / COINr

COINr
https://bluefoxr.github.io/COINr/
Other
25 stars 8 forks source link

Impute( impute_by = "df") throws error: #34

Closed OscarSmallenbroek closed 2 years ago

OscarSmallenbroek commented 2 years ago

Hi William,

We've noticed that the impute_by = "df" option of the Impute() function throws an error. See code below.

library(COINr)
library(tidyverse)
data<-ASEM_iData
meta<-ASEM_iMeta

# build a new coin using example data
coin <- new_coin(iData = ASEM_iData,
                 iMeta = ASEM_iMeta,
                 level_names = c("Indicator", "Pillar", "Sub-index", "Index"))
coin<-Impute(coin, dset = "Raw",f_i = 'i_mean', impute_by = "df")

Output: coin<-Impute(coin, dset = "Raw",f_i = 'i_mean', impute_by = "df") Error in i_mean(x = list(LPI = c(3.793385, 4.097985, 4.108538, 2.663902, : is.numeric(x) is not TRUE

bluefoxr commented 2 years ago

Hi, yes it's because you are passing a whole data frame to the function i_mean() which only accepts numerical vectors. You should either change the function to one that accepts data frames, or else use impute_by = "column". This is explained in the function documentation: https://bluefoxr.github.io/COINr/reference/Impute.coin.html

OscarSmallenbroek commented 2 years ago

Okay! sorry for the false flag.

We have tried to pass a function to Impute which that takes a data frame and returns a data frame, but we got an error. We are not sure about the source of the error. I will open an issue when we have a better idea where things are going wrong.

require( "VIM")
library(COINr)
library(tidyverse)
data<-ASEM_iData
meta<-ASEM_iMeta

# build a new coin using example data
coin <- new_coin(iData = ASEM_iData,
                 iMeta = ASEM_iMeta,
                 level_names = c("Indicator", "Pillar", "Sub-index", "Index"))

knncus <- function(data){
  return(kNN(data, imp_var = FALSE)[-1])
}
# testing the knncus function
knncus(get_data(coin , dset="Raw"))

# testing knncus function inside Impute
Impute(coin, dset = "Raw", f_i = "knncus",impute_by = "df" )

Error in knncus(x = list(LPI = c(3.793385, 4.097985, 4.108538, 2.663902, : unused argument (x = list(c(3.793385, 4.097985, 4.108538, 2.663902, 2.807685, 2.870492, 3.987158, 3.661104, 2.999061, 3.674309, 4.225967, 3.815794, 3.727412, 3.363489, 3.920745, 3.900953, 4.069669, 3.239516, 3.160829, 3.428968, 2.984537, 3.420043, 3.794886, 3.755414, 3.970464, 2.751998, 2.80059, 3.717126, 2.067254, 3.631688, 4.219409, 3.327107, 3.069256, 2.458571, 2.506056, 3.426307, 4.18753, 3.732163, 3.388, 2.923219, 2.856259, 3.425877, 3.409367, 2.99312, 2.570864, 4.143632, 3.336895, 3.184508, 4.204593, 3.2551, 2.976629), c(36.05498, 29.01725, 31.88546, 4.27955, 9.23588, 2.019, 51.78846, 114.2008, 8.75467, 15.30953, 174.3588, 32.7705, 170.9628, 3.12946, 18.85806, 97.63489, 210.8244, 34.83849, 9.24529, 13.01196, 34.6802, 25.2643, 34.17721, 110.2755, 69.00941, 4.02303, 9.5212, 69.84827, 3.0744, 5.37919, 4.84458, 6.77976, 6.75251, 6.695, 0.98951, 53.33988, 63.59241, 25.64994, 13.37242, 2.21146, 19.43838, 33.79735, 40.46484, 18.79894, 34.06447, 67.86156, 2.07397, 1.51736, 31.7

bluefoxr commented 2 years ago

Try naming the data argument of your function as x?

OscarSmallenbroek commented 2 years ago

That works! However, there is still a warning. I've proposed a change to the code for the Impute function .


require( "VIM")
library(COINr)
library(tidyverse)
data<-ASEM_iData
meta<-ASEM_iMeta

# build a new coin using example data
coin <- new_coin(iData = ASEM_iData,
                 iMeta = ASEM_iMeta,
                 level_names = c("Indicator", "Pillar", "Sub-index", "Index"))

# does not work becasue i_mean takes collumnns as input
## coin<-Impute(coin, dset = "Raw",f_i = 'i_mean', impute_by = "df")

knncus <- function(x){

  return(kNN(x, imp_var = FALSE))
}

test <- Impute(coin, dset = "Raw", f_i = "knncus",impute_by = "df" )

In if (dim(x_imp) != dim(x)) { : the condition has length > 1 and only the first element will be used

bluefoxr commented 2 years ago

Hi, this is also fixed now. Try downloading the latest version.