do.call Error for Function Not Present When Manually Called

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

do.call Error for Function Not Present When Manually Called

Dario Strbenac-2
Hello,

The e1071 function naiveBayes gives an error when called by do.call, although it doesn't give any error if I call it manually.

Browse[1]> trainParams@classifier
function (x, ...)
UseMethod("naiveBayes")
<environment: namespace:e1071>
Browse[1]> trained <- do.call(trainParams@classifier, paramList)
Error in names(dimnames(tables[[i]])) <- c(Yname, colnames(x)[i]) :
  'names' attribute [4] must be the same length as the vector [2]
Browse[1]> summary(paramList)
        Length Class  Mode  
        925    -none- numeric
         37    factor numeric
verbose   1    -none- logical
Browse[1]> class(trainParams@classifier(paramList[[1]], paramList[[2]], paramList[[3]]))
[1] "naiveBayes"

What is the explanation for this ? I read that the behaviour of do.call is currently undefined and subject to change.

Browse[1]> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8    
 [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8  
 [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C                
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C      

attached base packages:
 [1] stats4    splines   parallel  grid      stats     graphics  grDevices
 [8] utils     datasets  methods   base    

other attached packages:
 [1] robustbase_0.91-1         e1071_1.6-3              
 [3] car_2.0-20                edgeR_3.6.2              
 [5] statmod_1.4.19            PoiClaClu_1.0.2          
 [7] lawstat_2.4.1             VGAM_0.9-4              
 [9] mvtnorm_0.9-99992         sparsediscrim_0.2        
[11] MASS_7.3-33               curatedOvarianData_1.2.0
[13] affy_1.40.0               lumiHumanIDMapping_1.10.0
[15] illuminaHumanv3.db_1.20.0 org.Hs.eg.db_2.10.1      
[17] RSQLite_0.11.4            DBI_0.2-7                
[19] AnnotationDbi_1.24.0      lumi_2.14.2              
[21] Biobase_2.22.0            BiocGenerics_0.8.0      
[23] limma_3.18.13             ROCR_1.0-5              
[25] gplots_2.13.0             gridExtra_0.9.1          
[27] ggplot2_1.0.0            

loaded via a namespace (and not attached):
 [1] affyio_1.30.0          annotate_1.40.1        AnnotationForge_1.4.4
 [4] base64_1.1             bdsmatrix_1.3-1        beanplot_1.1          
 [7] BiocInstaller_1.14.2   biomaRt_2.18.0         Biostrings_2.30.1    
[10] bitops_1.0-6           BSgenome_1.30.0        bumphunter_1.2.0      
[13] caTools_1.17           class_7.3-10           codetools_0.2-8      
[16] colorspace_1.2-4       corpcor_1.6.6          DEoptimR_1.0-1        
[19] digest_0.6.4           doRNG_1.6              foreach_1.4.2        
[22] gdata_2.13.3           genefilter_1.44.0      GenomicFeatures_1.14.5
[25] GenomicRanges_1.14.4   gtable_0.1.2           gtools_3.4.0          
[28] illuminaio_0.4.0       IRanges_1.20.7         iterators_1.0.7      
[31] itertools_0.1-3        KernSmooth_2.23-12     labeling_0.2          
[34] lattice_0.20-29        locfit_1.5-9.1         Matrix_1.1-3          
[37] matrixStats_0.8.14     mclust_4.3             methylumi_2.8.0      
[40] mgcv_1.7-29            minfi_1.8.9            multtest_2.18.0      
[43] munsell_0.4.2          nleqslv_2.1.1          nlme_3.1-117          
[46] nnet_7.3-8             nor1mix_1.1-4          pkgmaker_0.22        
[49] plyr_1.8.1             preprocessCore_1.24.0  proto_0.3-10          
[52] RColorBrewer_1.0-5     Rcpp_0.11.1            RCurl_1.95-4.1        
[55] registry_0.2           reshape_0.8.5          reshape2_1.4          
[58] R.methodsS3_1.6.1      rngtools_1.2.4         Rsamtools_1.14.3      
[61] rtracklayer_1.22.7     scales_0.2.4           siggenes_1.36.0      
[64] stringr_0.6.2          survival_2.37-7        tcltk_3.1.0          
[67] tools_3.1.0            XML_3.98-1.1           xtable_1.7-3          
[70] XVector_0.2.0          zlibbioc_1.8.0        

--------------------------------------
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: do.call Error for Function Not Present When Manually Called

Peter Dalgaard-2

On 12 Jun 2014, at 09:00 , Dario Strbenac <[hidden email]> wrote:

> Hello,
>
> The e1071 function naiveBayes gives an error when called by do.call, although it doesn't give any error if I call it manually.
>
> Browse[1]> trainParams@classifier
> function (x, ...)
> UseMethod("naiveBayes")
> <environment: namespace:e1071>
> Browse[1]> trained <- do.call(trainParams@classifier, paramList)
> Error in names(dimnames(tables[[i]])) <- c(Yname, colnames(x)[i]) :
>  'names' attribute [4] must be the same length as the vector [2]
> Browse[1]> summary(paramList)
>        Length Class  Mode  
>        925    -none- numeric
>         37    factor numeric
> verbose   1    -none- logical
> Browse[1]> class(trainParams@classifier(paramList[[1]], paramList[[2]], paramList[[3]]))
> [1] "naiveBayes"
>
> What is the explanation for this ? I read that the behaviour of do.call is currently undefined and subject to change.


Read where? Not in the documentation, as far as I can see.

Problems with do.call are usually related to lack of lazy evaluation since "args" in do.call is a list of _evaluated_ arguments.

You're not giving sufficient context, but I would conjecture that "Yname" is computed via deparse(substitute(Y)) or something like that. If so, you will run into effects like this (notice the y axis title):

> x <- rnorm(3)
> do.call("plot", list(x))

or, possibly the exact problem for you:

> y <- rnorm(10)
> yname <- function(y)deparse(substitute(y))
> do.call("yname", list(y))
[1] "c(-0.750056154880415, -1.70241489930364, 2.36590757548367, -1.3989317670186, "
[2] "0.125698210447379, 0.446013380855154, -0.949447325668307, 0.388833055449324, "
[3] "1.61595795424046, -0.657071605757215)"                                        

in which the "name" of the vector becomes a character vector of length 3, not 1, because of line breaks.

Using list(quote(y)) sometimes helps, but you may need some care that y gets evaluated in the correct environment.


>
> Browse[1]> sessionInfo()
> R version 3.1.0 (2014-04-10)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
> [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C              
> [3] LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8    
> [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8  
> [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C                
> [9] LC_ADDRESS=C               LC_TELEPHONE=C            
> [11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C      
>
> attached base packages:
> [1] stats4    splines   parallel  grid      stats     graphics  grDevices
> [8] utils     datasets  methods   base    
>
> other attached packages:
> [1] robustbase_0.91-1         e1071_1.6-3              
> [3] car_2.0-20                edgeR_3.6.2              
> [5] statmod_1.4.19            PoiClaClu_1.0.2          
> [7] lawstat_2.4.1             VGAM_0.9-4              
> [9] mvtnorm_0.9-99992         sparsediscrim_0.2        
> [11] MASS_7.3-33               curatedOvarianData_1.2.0
> [13] affy_1.40.0               lumiHumanIDMapping_1.10.0
> [15] illuminaHumanv3.db_1.20.0 org.Hs.eg.db_2.10.1      
> [17] RSQLite_0.11.4            DBI_0.2-7                
> [19] AnnotationDbi_1.24.0      lumi_2.14.2              
> [21] Biobase_2.22.0            BiocGenerics_0.8.0      
> [23] limma_3.18.13             ROCR_1.0-5              
> [25] gplots_2.13.0             gridExtra_0.9.1          
> [27] ggplot2_1.0.0            
>
> loaded via a namespace (and not attached):
> [1] affyio_1.30.0          annotate_1.40.1        AnnotationForge_1.4.4
> [4] base64_1.1             bdsmatrix_1.3-1        beanplot_1.1          
> [7] BiocInstaller_1.14.2   biomaRt_2.18.0         Biostrings_2.30.1    
> [10] bitops_1.0-6           BSgenome_1.30.0        bumphunter_1.2.0      
> [13] caTools_1.17           class_7.3-10           codetools_0.2-8      
> [16] colorspace_1.2-4       corpcor_1.6.6          DEoptimR_1.0-1        
> [19] digest_0.6.4           doRNG_1.6              foreach_1.4.2        
> [22] gdata_2.13.3           genefilter_1.44.0      GenomicFeatures_1.14.5
> [25] GenomicRanges_1.14.4   gtable_0.1.2           gtools_3.4.0          
> [28] illuminaio_0.4.0       IRanges_1.20.7         iterators_1.0.7      
> [31] itertools_0.1-3        KernSmooth_2.23-12     labeling_0.2          
> [34] lattice_0.20-29        locfit_1.5-9.1         Matrix_1.1-3          
> [37] matrixStats_0.8.14     mclust_4.3             methylumi_2.8.0      
> [40] mgcv_1.7-29            minfi_1.8.9            multtest_2.18.0      
> [43] munsell_0.4.2          nleqslv_2.1.1          nlme_3.1-117          
> [46] nnet_7.3-8             nor1mix_1.1-4          pkgmaker_0.22        
> [49] plyr_1.8.1             preprocessCore_1.24.0  proto_0.3-10          
> [52] RColorBrewer_1.0-5     Rcpp_0.11.1            RCurl_1.95-4.1        
> [55] registry_0.2           reshape_0.8.5          reshape2_1.4          
> [58] R.methodsS3_1.6.1      rngtools_1.2.4         Rsamtools_1.14.3      
> [61] rtracklayer_1.22.7     scales_0.2.4           siggenes_1.36.0      
> [64] stringr_0.6.2          survival_2.37-7        tcltk_3.1.0          
> [67] tools_3.1.0            XML_3.98-1.1           xtable_1.7-3          
> [70] XVector_0.2.0          zlibbioc_1.8.0        
>
> --------------------------------------
> Dario Strbenac
> PhD Student
> University of Sydney
> Camperdown NSW 2050
> Australia
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

--
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: do.call Error for Function Not Present When Manually Called

Dario Strbenac-2
I had a look at the source code, and your prediction is right. There is line at the beginning of the naiveBayes function :

    Yname <- deparse(substitute(y))

When I manually edited it to list(quote(y)), it works.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel