Calling gqs() via stanmodels.R in a package fails

Operating System: Ubuntu 18.04.3 LTS
Interface Version: rstan from development branch, version 2.19.9
Compiler/Toolkit: g++

I’m trying to incorporate the standalone generated quantities gqs() in a package.
In my src/stan_files I have my model.stan files and a forecasting model, let’s say forecast.stan

I followed the same logic as with the sampling statement - that is, in one of my R-package functions I call
the named object of the stanmodels list.

out =  rstan::gqs(stanmodels$forecast,
                                 draws =  as.matrix(object$model_fit),
                                 data = standat,
                                 seed =  seed)

This, however, returns an error:

Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : 
  ‘standalone_gqs’ is not a valid field or method name for reference class “Rcpp_model_forecast”
error occurred during calling the sampler; sampling not done

Running the same model by first constructing the DSO via stan_model works.
That is, doing this works flawlessly:

frcst <- rstan::stan_model(file = "../src/stan_files/forecast.stan")
out <- rstan::gqs(frcst, draws = as.matrix(bekk_fit$model_fit), data =  standata)

However, this means that each time I load the package, the frcst object needs to be created first - which is not what I want.

I’d appreciate any workaround (or insight), thanks!

Can you do a traceback() after that error?

There seems to be no traceback available:

> traceback()
No traceback available 

I noticed that in my src/stan_files folder, all the other stan models have an associated .cc, and .hpp, file. My forecasting model only creates a forecast.hpp file.
Is this a problem?

Maybe. Is it on GitHub?

No, not yet - I can upload it if it helps

OK, then I can clone it.

I think traceback() should be called immediately after the error, as if you have other commands in between in my experience it will not report the errors, unlike what happens in case of warnings.

1 Like

here’s the repo: https://github.com/ph-rast/bmgarch

I can confirm this behavior.
I’m unsure where this occurs, but when using stan_model, the cpp module includes the standalone_gqs method. But the cpp module in the stanmodels$forecastGQ model compiled via rstantools does not include the standalone_gqs method.

From using stan_models:

> names(stan_fit_cpp_module@methods)
 [1] "call_sampler"              "constrain_pars"           
 [3] "constrained_param_names"   "grad_log_prob"            
 [5] "log_prob"                  "num_pars_unconstrained"   
 [7] "param_dims"                "param_dims_oi"            
 [9] "param_fnames_oi"           "param_names"              
[11] "param_names_oi"            "param_oi_tidx"            
[13] "standalone_gqs"            "unconstrain_pars"         
[15] "unconstrained_param_names" "update_param_oi" 

When compiled in the package, and accessed via stanmodels$forecastGQ:

> names(stan_fit_cpp_module.bmgarch@methods)
 [1] "call_sampler"              "constrain_pars"           
 [3] "constrained_param_names"   "grad_log_prob"            
 [5] "log_prob"                  "num_pars_unconstrained"   
 [7] "param_dims"                "param_dims_oi"            
 [9] "param_fnames_oi"           "param_names"              
[11] "param_names_oi"            "param_oi_tidx"            
[13] "unconstrain_pars"          "unconstrained_param_names"
[15] "update_param_oi"    

It is due to make_cc.R in tools/:

  Rcpp::exposeClass(class = paste0("model_", f),
                    constructors = list(c("SEXP", "SEXP", "SEXP")), fields = character(),
                    methods = c("call_sampler", 
                                "param_names", "param_names_oi", "param_fnames_oi", 
                                "param_dims",  "param_dims_oi", "update_param_oi", "param_oi_tidx", 
                                "grad_log_prob", "log_prob", 
                                "unconstrain_pars", "constrain_pars", "num_pars_unconstrained", 
                                "unconstrained_param_names", "constrained_param_names"), 

I suspect by adding “standalone_gqs” to that vector will fix it.

Edit: Confirmed. Just changing it to include “standalone_gqs” resolved the problem.

1 Like

Thanks Stephen! Problem solved