facebook / prophet

Tool for producing high quality forecasts for time series data that has multiple seasonality with linear or non-linear growth.
https://facebook.github.io/prophet
MIT License
18.34k stars 4.52k forks source link

Rcpp error when using prophet::prophet() instead of library(prophet) #285

Open z1lc opened 7 years ago

z1lc commented 7 years ago

When I don't use library(prophet) and instead use the fully-qualified prophet::prophet, I get the following in my console:

ds <- seq(as.Date('2005-04-01'), as.Date('2005-09-03'), by='day') x <- (1:length(ds))/length(ds) y <- rnorm(length(ds), x * (1 - x) + 0.5, 0.1) prophet::prophet(data.frame(ds = ds, y = y), yearly.seasonality = FALSE)

Error in .Object$initialize(...) : could not find function "cpp_object_initializer" failed to create the optimizer; optimization not done Error in matrix(m$params[[name]], nrow = n.iteration) : 'data' must be of a vector type, was 'NULL'

I'm using the latest revision from master (via devtools::install_github('facebookincubator/prophet', subdir='R'))

bletham commented 7 years ago

From searching for this issue it seems to be related to Rcpp, but we have already done the thing that I could find as a solution (include Rcpp in the Depends). We should see how rstanarm does this.

danielmcauley commented 7 years ago

@z1lc @bletham

is this the root cause?

https://github.com/stan-dev/rstan/blob/d84eb5ebd9bcfc0f3bfe6455402678b04a740485/rstan/rstan/NAMESPACE#L11

bgoodri commented 7 years ago

Even rstan::sampling throws the same error if you don't do library(rstan) first. The only known way to get foo::bar to work when bar is calling Stan is for foo to depend (not import from) Rcpp so that Rcpp gets loaded before the Module magic happens.

megan-stamper commented 5 years ago

Any updates on this? Having the same issue while writing a package using prophet's functions.

bletham commented 5 years ago

@megan-stamper I haven't looked into this any further. @bgoodri would certainly know, though we do have Rcpp in the Depends so there must be something else going on. I think looking at other packages that call Stan might reveal if there is something that can be done to resolve this.

bgoodri commented 5 years ago

If you have Rcpp in Depends, then I think it should work.

bletham commented 5 years ago

@bgoodri We have Rcpp in depends:

Depends: R (>= 3.2.3), Rcpp (>= 0.12.0), rlang (>= 0.3.0.1)
Imports: dplyr (>= 0.5.0), dygraphs (>= 1.1.1.4), extraDistr, ggplot2,
        grid, rstan (>= 2.14.0), scales, stats, tidyr (>= 0.6.1), xts
Suggests: knitr, testthat, readr

NAMESPACE has these imports:

import(Rcpp)
import(rlang)
importFrom(dplyr,"%>%")

But still get the error when we call rstan::optimizing internally in the package:

> ds <- seq(as.Date('2005-04-01'), as.Date('2005-09-03'), by='day')
> x <- (1:length(ds))/length(ds)
> y <- rnorm(length(ds), x * (1 - x) + 0.5, 0.1)
> m <- prophet::prophet(data.frame(ds = ds, y = y), yearly.seasonality=FALSE, daily.seasonality=FALSE)
Error in .Object$initialize(...) : 
  could not find function "cpp_object_initializer"
failed to create the optimizer; optimization not done

Based on your comment in https://discourse.mc-stan.org/t/strange-new-error-when-running-code-that-has-always-worked-before/6846/2 I replaced rstan::optimizing with an importFrom(rstan, optimizing) in the NAMESPACE and directly calling optimizing in the package, but we still get the error.

Based on the comments in https://github.com/RcppCore/Rcpp/issues/168 I also tried adding importFrom(Rcpp, cpp_object_initializer) to the NAMESPACE but that didn't do anything.

Do you have any suggestions on further directions to look?