JeffreyRacine / R-Package-np

R package np (Nonparametric Kernel Smoothing Methods for Mixed Data Types)
https://socialsciences.mcmaster.ca/people/racinej
46 stars 18 forks source link

npudens inside a custom function #27

Closed alequech closed 3 years ago

alequech commented 3 years ago

Hi,

I'm trying to write a custom function that includes npudens, but npudens is giving me the following error Error in is.data.frame(data) : object 'df_cat' not found

The following is a reproduction of the error using the iris dataset

library(datasets)
library(np)
data(iris)

variables  <- c("Sepal.Length", "Sepal.Width",  
                "Petal.Length", "Petal.Width")

formula_d <- formula(paste("~", paste(variables, collapse = "+"))) 

myfunction <-  function(df_cat, f){
  bw <- npudensbw(f, data  = df_cat)
  f <- npudens(bw)
  return(f)
}

kernel_iris <-  myfunction(df_cat = iris, f = formula_d)
Error in is.data.frame(data) : object 'df_cat' not found 

on the other hand, if I try the same thing outside of myfunction the calculation is completed properly.

kernel_iris <-  myfunction(df_cat = iris, f = formula_d)

bw_test <- npudensbw(formula_d, data  = iris)
npudens(bw_test)
Density Data: 150 training points, in 4 variable(s)
              Sepal.Length Sepal.Width Petal.Length Petal.Width
Bandwidth(s):    0.2119036   0.2178008    0.1990493  0.09917488

Bandwidth Type: Fixed
Log Likelihood: -118.8163

Continuous Kernel Type: Second-Order Gaussian
No. Continuous Vars.: 4

My final goal is to be able to calculate in parallel a Kernel Density for each factor of the Species column, for this I plan to use myfuntion inside a foreach() or parLapply().

I almost forgot my sessionInfo()

R version 4.0.5 (2021-03-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Pop!_OS 20.04 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=es_MX.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=es_MX.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=es_MX.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=es_MX.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] np_0.60-11

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         conquer_1.0.2      magrittr_2.0.1     tidyselect_1.1.0   lattice_0.20-44    cubature_2.0.4.1   R6_2.5.0          
 [8] quadprog_1.5-8     rlang_0.4.10       fansi_0.4.2        plyr_1.8.6         dplyr_1.0.5        tools_4.0.5        grid_4.0.5        
[15] xfun_0.21          quantreg_5.85      tinytex_0.30       utf8_1.1.4         DBI_1.1.1          matrixStats_0.58.0 MatrixModels_0.5-0
[22] ellipsis_0.3.1     yaml_2.2.1         assertthat_0.2.1   tibble_3.1.0       lifecycle_1.0.0    crayon_1.4.1       Matrix_1.3-3      
[29] purrr_0.3.4        vctrs_0.3.6        glue_1.4.2         compiler_4.0.5     pillar_1.5.1       generics_0.1.0     boot_1.3-27       
[36] SparseM_1.81       pkgconfig_2.0.3   
JeffreyRacine commented 3 years ago

Hi. The following will work... use the data frame interface instead... and calling npudens() will invoke npudensbw() with default (options can be passed through npudens())...

library(datasets)
library(np)
## Turn off I/O
options(np.messages=FALSE)
data(iris)

myfunction <-  function(df_cat, f){
  npudens(tdat  = df_cat)
}

kernel_iris <-  myfunction(df_cat = iris, f = formula_d)