Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"etsTargetFunctionInit" not resolved from current namespace (forecast) #378

Closed
SteveBronder opened this issue Oct 16, 2016 · 3 comments
Closed
Assignees

Comments

@SteveBronder
Copy link

I'm currently working to implement forecast within mlr. When writing my unit tests for ets a strange error will happen semi-sporadically. With a clean restart of R the following code will not produce an error

# set seed, make data
set.seed(1234)
fcregr.xts <- arima.sim(model = list(ar = c(.5,.2), ma = c(.4), order = c(2,0,1)), n = 300)
times <- (as.POSIXlt("1992-01-14")) + lubridate::days(1:300)
fcregr.xts <- xts::xts(fcregr.xts,order.by = times, frequency = 1L)
colnames(fcregr.xts) = "test_data"
parset.list = list(
    list(),
    list( model = "ANN"),
    list( model = "ZAZ", ic = "bic"),
    list(opt.crit = "amse", bounds = "usual"),
    list( model = "AZZ", lambda = 1)
  )
  old.predicts.list = list()

  for (i in 1:length(parset.list)) {
    parset = parset.list[[i]]
    pars = list(y = ts(fcregr.train, start = 1, frequency = 1L))
    pars = c(pars, parset)
    set.seed(getOption("mlr.debug.seed"))
    capture.output({
      m = do.call(forecast::ets, pars)
    })
    set.seed(getOption("mlr.debug.seed"))
    p = as.numeric(forecast::forecast(m, h = 1L)$mean)
    old.predicts.list[[i]] = p
  }

below is the session Info for this

sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS

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

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

loaded via a namespace (and not attached):
 [1] multicool_0.1-9             zoo_1.7-13                  ks_1.10.4                  
 [4] splines_3.3.1               lattice_0.20-34             expm_0.999-0               
 [7] colorspace_1.2-7            SkewHyperbolic_0.3-2        htmltools_0.3.5            
[10] chron_2.3-47                survival_2.39-4             nloptr_1.0.4               
[13] DBI_0.5-1                   forecast_7.3                plyr_1.8.4                 
[16] stringr_1.1.0               timeDate_3012.100           munsell_0.4.3              
[19] gtable_0.2.0                htmlwidgets_0.7             mvtnorm_1.0-5              
[22] codetools_0.2-14            misc3d_0.8-4                knitr_1.14                 
[25] tseries_0.10-35             parallelMap_1.3             httpuv_1.3.3               
[28] parallel_3.3.1              xts_0.9-7                   Rcpp_0.12.7                
[31] KernSmooth_2.23-15          xtable_1.8-2                scales_0.4.0               
[34] backports_1.0.3             rugarch_1.3-6               checkmate_1.8.1            
[37] DistributionUtils_0.5-1     jsonlite_1.1                truncnorm_1.0-7            
[40] mime_0.5                    fracdiff_1.4-2              spd_2.0-1                  
[43] ggplot2_2.1.0               ggvis_0.4.3                 digest_0.6.10              
[46] stringi_1.1.2               BBmisc_1.10                 dplyr_0.5.0                
[49] shiny_0.14.1                numDeriv_2016.8-1           grid_3.3.1                 
[52] quadprog_1.5-5              tools_3.3.1                 magrittr_1.5               
[55] rgl_0.96.0                  Rsolnp_1.16                 tibble_1.2                 
[58] mlr_2.10                    GeneralizedHyperbolic_0.8-1 Matrix_1.2-7.1             
[61] data.table_1.9.6            ParamHelpers_1.9            lubridate_1.6.0            
[64] assertthat_0.1              R6_2.2.0                    nnet_7.3-12  

Now when I attempt to run my test we encounter the error. The test can be run by cloning
https://github.com/Stevo15025/mlr
and running the following code

devtools::test(filter = "ets")
# Loading mlr
# Loading required package: testthat
# Loading required package: ParamHelpers
# Testing mlr
# fcregr_ets: 1
# 
# Failed --------------------------------------------------------------------------------------------------
#1. Error: fcregr_ets (@test_fcregr_ets.R#20) ------------------------------------------------------------
# "etsTargetFunctionInit" not resolved from current namespace (forecast)
# ...

And now when we run the first block of code we also receive the same error

# set seed, make data
set.seed(1234)
fcregr.xts <- arima.sim(model = list(ar = c(.5,.2), ma = c(.4), order = c(2,0,1)), n = 300)
times <- (as.POSIXlt("1992-01-14")) + lubridate::days(1:300)
fcregr.xts <- xts::xts(fcregr.xts,order.by = times, frequency = 1L)
colnames(fcregr.xts) = "test_data"
parset.list = list(
  list(),
  list( model = "ANN"),
  list( model = "ZAZ", ic = "bic"),
  list(opt.crit = "amse", bounds = "usual"),
  list( model = "AZZ", lambda = 1)
)
old.predicts.list = list()

for (i in 1:length(parset.list)) {
  parset = parset.list[[i]]
  pars = list(y = ts(fcregr.train, start = 1, frequency = 1L))
  pars = c(pars, parset)
  set.seed(getOption("mlr.debug.seed"))
  capture.output({
    m = do.call(forecast::ets, pars)
  })
  set.seed(getOption("mlr.debug.seed"))
  p = as.numeric(forecast::forecast(m, h = 1L)$mean)
  old.predicts.list[[i]] = p
}
# Error in .Call("etsTargetFunctionInit", y = y, nstate = nstate, errortype = switch(errortype,  : 
#   "etsTargetFunctionInit" not resolved from current namespace (forecast)

Below is the post-test session info

R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS

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

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

other attached packages:
[1] checkmate_1.8.1  mlr_2.10         ParamHelpers_1.9 testthat_1.0.2  

loaded via a namespace (and not attached):
 [1] rgl_0.96.0                  Rcpp_0.12.7                 lubridate_1.6.0            
 [4] mvtnorm_1.0-5               lattice_0.20-34             zoo_1.7-13                 
 [7] assertthat_0.1              digest_0.6.10               mime_0.5                   
[10] truncnorm_1.0-7             R6_2.2.0                    plyr_1.8.4                 
[13] chron_2.3-47                backports_1.0.3             ggplot2_2.1.0              
[16] misc3d_0.8-4                data.table_1.9.6            fracdiff_1.4-2             
[19] nloptr_1.0.4                SkewHyperbolic_0.3-2        Matrix_1.2-7.1             
[22] devtools_1.12.0             splines_3.3.1               stringr_1.1.0              
[25] htmlwidgets_0.7             munsell_0.4.3               shiny_0.14.1               
[28] numDeriv_2016.8-1           httpuv_1.3.3                DistributionUtils_0.5-1    
[31] forecast_7.3                BBmisc_1.10                 htmltools_0.3.5            
[34] nnet_7.3-12                 Rsolnp_1.16                 multicool_0.1-9            
[37] tibble_1.2                  expm_0.999-0                roxygen2_5.0.1             
[40] quadprog_1.5-5              codetools_0.2-14            crayon_1.3.2               
[43] dplyr_0.5.0                 withr_1.0.2                 GeneralizedHyperbolic_0.8-1
[46] grid_3.3.1                  jsonlite_1.1                xtable_1.8-2               
[49] gtable_0.2.0                DBI_0.5-1                   magrittr_1.5               
[52] ggvis_0.4.3                 scales_0.4.0                KernSmooth_2.23-15         
[55] stringi_1.1.2               tseries_0.10-35             parallelMap_1.3            
[58] timeDate_3012.100           xts_0.9-7                   spd_2.0-1                  
[61] tools_3.3.1                 rugarch_1.3-6               ks_1.10.4                  
[64] parallel_3.3.1              survival_2.39-4             colorspace_1.2-7           
[67] memoise_1.0.0               knitr_1.14    

Could the problem be that there are too many packages loaded? Why would etsTargetFunctionInit disappear?

@cbergmeir
Copy link
Collaborator

cbergmeir commented Oct 17, 2016

Hi,

I tried this but couldn't reproduce the problem:

 devtools::test("/home/bergmeir/code-other/mlr", filter = "ets")
 Loading mlr
 Loading required package: testthat
 Loading required package: ParamHelpers
 Testing mlr
 fcregr_ets : .....

 DONE 

Session as follows:

R version 3.2.0 (2015-04-16)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Fedora 20 (Heisenbug)

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

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

other attached packages:
[1] checkmate_1.8.1  mlr_2.10         ParamHelpers_1.9 testthat_0.11.0  rj_2.0.4-2      
[6] vimcom_1.2-5     setwidth_1.0-4   colorout_1.1-0  

loaded via a namespace (and not attached):
 [1] parallelMap_1.3   Rcpp_0.12.7       plyr_1.8.4        tseries_0.10-35   xts_0.9-7        
 [6] tools_3.2.0       digest_0.6.10     rj.gd_2.0.0-1     lubridate_1.5.6   lattice_0.20-33  
[11] memoise_1.0.0     tibble_1.1        gtable_0.2.0      shiny_0.13.2      DBI_0.4-1        
[16] parallel_3.2.0    withr_1.0.1       stringr_1.1.0     dplyr_0.5.0       roxygen2_5.0.1   
[21] ggvis_0.4.2       devtools_1.10.0   nnet_7.3-12       forecast_7.3      grid_3.2.0       
[26] data.table_1.9.6  R6_2.1.2          tcltk_3.2.0       survival_2.38-3   ggplot2_2.1.0    
[31] magrittr_1.5      splines_3.2.0     backports_1.0.3   scales_0.4.0      BBmisc_1.10      
[36] htmltools_0.3.5   assertthat_0.1    timeDate_3012.100 mime_0.4          colorspace_1.2-6 
[41] xtable_1.8-2      fracdiff_1.4-2    httpuv_1.3.3      quadprog_1.5-5    stringi_1.1.1    
[46] munsell_0.4.3     chron_2.3-47      crayon_1.3.1      zoo_1.7-13      `

Does it make a difference if you explicitly load the package with a library(forecast) ?

As a side note, your code didn't work, you have fcregr.train which I assumed is fcregr.xts

Regards,
Christoph

@SteveBronder
Copy link
Author

@cbergmeir I should have mentioned this, the error only happens if you run the test twice. Which is very odd.

For example, with a fresh R session

# devtools::test(filter = "ets")
# Loading mlr
# Loading required package: testthat
# Loading required package: ParamHelpers
# Testing mlr
# fcregr_ets: .....

# DONE ===================================================================================================================

# Running a second time
devtools::test(filter = "ets")
# Loading mlr
# Testing mlr
# fcregr_ets: 1

# Failed -----------------------------------------------------------------------------------------------------------------
#   1. Error: fcregr_ets (@test_fcregr_ets.R#20) ---------------------------------------------------------------------------
#      "etsTargetFunctionInit" not resolved from current namespace (forecast)

# Loading forecast 
library(forecast)
devtools::test(filter = "ets")
# Failed -----------------------------------------------------------------------------------------------------------------
#   1. Error: fcregr_ets (@test_fcregr_ets.R#20) ---------------------------------------------------------------------------
#      "etsTargetFunctionInit" not resolved from current namespace (forecast)

However! If I start a fresh R session and load forecast first, the tests run fine.

library(forecast)
devtools::test(filter = "ets")
# Loading mlr
# Loading required package: testthat
# Loading required package: ParamHelpers
# Testing mlr
# fcregr_ets: .....

# DONE ===================================================================================================================

devtools::test(filter = "ets")
# Loading mlr
# Testing mlr
# fcregr_ets: .....

# DONE ===================================================================================================================

devtools::test(filter = "ets")
# Loading mlr
# Testing mlr
# fcregr_ets: .....

# DONE ===================================================================================================================

After speaking with @berndbischl, who I have cc'd here as this is slightly out of my depth and they have had this problem with mlr before, the problem comes from .Call()

From: http://r.789695.n4.nabble.com/question-re-error-message-package-error-functionName-not-resolved-from-current-namespace-tp4663892p4663950.html

A foreign function call (.C() etc) in a package without a PACKAGE
argument will only look in the first DLL specified in the NAMESPACE
file of the package rather than searching all loaded DLLs. A few
packages needed PACKAGE arguments added.

Looking at when etsTargetFunctionInit is called in ets() we see

res <- .Call("etsTargetFunctionInit", y=y, nstate=nstate, errortype=switch(errortype,"A"=1,"M"=2),
      trendtype=switch(trendtype,"N"=0,"A"=1,"M"=2), seasontype=switch(seasontype,"N"=0,"A"=1,"M"=2),
      damped=damped, lowerb=lowerb, upperb=upperb,
      opt.crit=opt.crit, nmse=as.integer(nmse), bounds=bounds, m=m,
      optAlpha, optBeta, optGamma, optPhi,
      givenAlpha, givenBeta, givenGamma, givenPhi,
alpha, beta, gamma, phi, env, package="forecast")

This might sound silly, but does it matter that it is lower case package and not PACKAGE?

Another possible point of interest from the same link, but rewritten for forecast

When .Call has a PACKAGE argument, or when R tries to guess the DLL from the
fact that .Call is from within a NAMESPACE, only one DLL is found (from
R_FindNativeSymbolFromDLL in main/dotcode.c:1331, which calls getCallingDLLe).
the function "antsImageRead" is not in the DLL returned by getCallingDLLe, so
not found.

 getNativeSymbolInfo("etsTargetFunctionInit")$package
# DLL name: forecast
# Filename: /home/steve/R/x86_64-pc-linux-gnu-library/3.3/forecast/libs/forecast.so
# Dynamic lookup: TRUE
getCallingDLLe(getNamespace("forecast")) 
# DLL name: forecast
# Filename: /home/steve/R/x86_64-pc-linux-gnu-library/3.3/forecast/libs/forecast.so
# Dynamic lookup: TRUE

cbergmeir added a commit that referenced this issue Nov 1, 2016
@cbergmeir
Copy link
Collaborator

Hi, thanks a lot for spotting the problem. I could reproduce the error and changing "package" to "PACKAGE" fixes it. Fixed with commit bbf87bf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants