Ah, something else I’d missed. One of the quirks of cmdstanr
's approach to including external c++ is that the function needs to be wrapped in an appropriate namespace
.
This namespace is given by the model name followed by _model_namespace
, so the for model_gamma.stan
example, the functions need to be declared within the model_gamma_model_namespace
.
Here’s an updated version of the header with the functions declared in the model_gamma_model_namespace
:
gamma_icdf.hpp (10.0 KB)
But because that would only allow you use the gamma_icdf
functions with Stan models called gamma_model.stan
, here’s an R function which will take a Stan model name and the path to the hpp file to update that namespace automatically:
prep_hpp = function(stan_filename,hpp_file) {
namespace = paste0(gsub(".stan","",stan_filename),"_model_namespace")
writeLines(gsub('[^ ]*_model_namespace',namespace,readLines(hpp_file)),
con=hpp_file)
}
Which you would use like so:
prep_hpp("model_gamma_new.stan",file.path(getwd(),"gamma_icdf.hpp"))
mod = cmdstan_model("model_gamma_new.stan", include_paths = utils::shortPathName(getwd()),
cpp_options = list(USER_HEADER = file.path(utils::shortPathName(getwd()), "gamma_icdf.hpp")),
stanc_options = list("allow-undefined"))