Call GNU Scientific Library from stan


#4

Hi all,
I found on the github of yixuan ( https://github.com/yixuan/RcppNumerical) a function to compute the integral using Gauss Kronrod method but the code was written in Rcpp.
Here is the code:

// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::depends(RcppNumerical)]]
#include <RcppNumerical.h>
using namespace Numer;
class function: public Func
{
private:
double nu;
public:
function(double nu_) : nu(nu_) {}

double operator()(const double& x) const
{
return exp(x*nu);
}
};

// [[Rcpp::export]]
double integrate_GK(double ll, double ul,double nu)
{
function f(nu);
double err_est;
int err_code;
const double res = integrate(f, ll, ul, err_est, err_code);
return res;
}
/*** R
integrate_GK(0,1,1)
*/

library(rstan)
mc <-

functions {

real integrate_GK(real ll, real ul,real nu);

}
model {

} // use the fib() function somehow

stan_model(model_code = mc, model_name = “external”, allow_undefined = TRUE,
includes = paste0(’\n#include “’,
file.path(getwd(), ‘integrate_GK.cpp’), '”\n’))

I want to use the function integrate_GK in the stan model. But I still dont know how to do it. I try to save the code in .cpp and using the instruction https://cran.r-project.org/web/packages/rstan/vignettes/external.html but I cannot sucess to make the model compile. The error are “Error in .Call(“CPP_stanc280”, model_code, model_cppname, allow_undefined, : “CPP_stanc280” not resolved from current namespace (rstan)”.

My main question is: can we use a defined function in Rcpp and call it a stan model?
Does anyone know how handle this issue? Could you please help me to solve it!

Thank you very much!
Nhat


#5

Your rstan does not seem to be installed properly.


#6

Thanks bgoodri!

You mean that if I install my rstan properly. It is feasible that I can call a Rcpp defined function in a stan model?

Nhat


#7

That was already feasible. You just need to install rstan properly to avoid this error:


#8

Thanks Ben!

I will fix my rstan.

Best,
Nhat


#9

Hi all,

I tried a simple example
mc5 <-

functions {
vector timesTwo(vector x);
}

model_tmp4<-stan_model(model_code = mc5, model_name = “externa4”, allow_undefined = TRUE,
includes = paste0(’\n#include “’,
file.path(getwd(), ‘timesTwo.hpp’), '”\n’))

here is the timesTwo.hpp file

#include <Rcpp.h>
using namespace Rcpp;
NumericVector timesTwo(NumericVector x);
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}

I have error:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.3/BH/include/boost/config.hpp:39:0,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.3/BH/include/boost/math/tools/config.hpp:13,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core.hpp:12,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/mat.hpp:4,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.3/StanHeaders/include/stan/math.hpp:4,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-
In addition: Warning message:
running command ‘C:/PROGRA~1/R/R-33~1.3/bin/x64/R CMD SHLIB file1f4849c31000.cpp 2> file1f4849c31000.cpp.err.txt’ had status 1

Does anyone know why Rcpp function doesn’t work on the stan model?

Thanks!
Nhat


#10

It is supposed to be

model_tmp4 <- stan_model(model_code = mc5, model_name = “externa4”, allow_undefined = TRUE,
                includes = paste0('\n#include "', file.path(getwd(), "TimesTwo.hpp"), '"\n'))

but I doubt it is going to work with that NumericVector typedef.


#11

Dear Ben,

I tried another example without NumericVector typedef.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]

double timesTwo(double x) {
return x * 2;
}

I have the same error as before. But if I removed the first three lines
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]

the code worked. I think the main issue is these three lines. Do you have any idea to fix this?

Many thanks!
Nhat


#12

I imagine it is the using namespace Rcpp; that clashes with something. If you have to use Rcpp functions, you should prefix them with Rcpp::.


#13

Hi Ben,

I re-install rtstan and rtools. When I tested Rtools with the code Sys.getenv(‘PATH’)
the output is:
“C:\Program Files\R\R-3.5.0\bin\x64;c:\Rtools\bin;c:\Rtools\mingw_32\bin;c:\Rtools\bin;c:\Rtools\gcc-4.6.3\bin;c:\Rtools\bin;c:\Rtools\bin;c:\Rtools\gcc-4.6.3\bin;c:\Rtools\bin;c:\Rtools\bin;C:\Program Files\Microsoft MPI\Bin\;c:\Rtools\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\PuTTY\;C:\Strawberry\c\bin;C:\Strawberry\perl\site\bin;C:\Strawberry\perl\bin;C:\Users\Nhat Le Thanh Hoang\AppData\Local\Microsoft\WindowsApps;”

It begins with C:\Program Files\R\R-3.5.0\bin\x64; not with c:\Rtools\bin;c:\Rtools\mingw_32…
Does it mean I didn’t install rtools properly? Hence rstan is not correct as well?

Thanks,
Nhat


#14

I think it is OK for the PATH to begin with the directory to R, but you have some duplicative entries. Not sure if that makes any difference.


#15

Dear Ben,
I tried to make a file.hpp with the library Rcpp. I never success to make the code run. Do you have any example that you included the user-defined function like timesTwo to the stan model?

Many thanks!
Nhat


#16

There are two examples in
https://cran.r-project.org/web/packages/rstan/vignettes/external.html


#17

Hi Ben,
I follow your instruction 1D numerical integration (again) and https://www.boost.org/doc/libs/1_67_0/libs/math/doc/html/quadrature.html to use boost library to write a function in .cpp with Rcpp to compute a integral of exp(x) function.
Here is the code.

#include <Rcpp.h>
#include <boost/math/quadrature/gauss_kronrod.hpp>
using namespace Rcpp;

// [[Rcpp::plugins(cpp14)]]
// [[Rcpp::depends(BH)]]

// [[Rcpp::export]]
double integral_GK(double ll, double ul,double nu) {
boost::math::quadrature::gauss_kronrod<double, 15> integrator;
auto f1 = [&](double x) {
return std::exp(x);
};
double error;
double ll_new=ll * nu;
double ul_new=ul * nu;
double Q = integrator.integrate(f1, ll_new, ul_new, 0, 0, &error)/nu;
return Q;
}

The code works well. What I’m trying is to translate this code to .hpp code as your instruction in https://cran.r-project.org/web/packages/rstan/vignettes/external.html so that I can inlcude in stan model, but I’m not successful to make the code run.

Here is the .hpp code that I tried:
template <typename T0__, typename T1__,typename T2__>
typename boost::math::tools::promote_args<T0__, T1__,T2__>::type
integral_GK(const T0__& ll, const T1__& ul,const T2__& nu, std::ostream* pstream__) {
boost::math::quadrature::gauss_kronrod<double, 15> integrator;
auto f1 = [&](double x) {
return std::exp(x);
};
double error;
double ll_new=ll * nu;
double ul_new=ul * nu;
double Q = integrator.integrate(f1, ll_new, ul_new, 0, 0, &error)/nu;
return Q;
}

and I go to the model_header.hpp and I included the header
#include <boost/math/quadrature/gauss_kronrod.hpp>

Here is a part of the error:
In file included from C:/Users/nhatlth/Documents/R/win-library/3.3/BH/include/boost/config.hpp:39:0,
from C:/Users/nhatlth/Documents/R/win-library/3.3/BH/include/boost/math/tools/config.hpp:13,
from C:/Users/nhatlth/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from C:/Users/nhatlth/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from C:/Users/nhatlth/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core.hpp:12,
from C:/Users/nhatlth/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/mat.hpp:4,
from C:/Users/nhatlth/Documents/R/win-library/3.3/StanHeaders/include/stan/math.hpp:4,
from C:/Users/nhatlth/Documents/R/win-library/3.3/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file29e417e718b4.cpp:8:
C:/Users/nhatlth/Documents/R/win-library/3.3/BH/include/boost/config/compiler/gcc.hpp:186:0: warning: “BOOST_NO_CXX11_RVALUE_REFERENCES” redefined

define BOOST_NO_CXX11_RVALUE_REFERENCES

^
:0:0: note: this is the location of the previous definition
In file included from C:/Users/nhatlth/Documents/R/win-library/3.3/BH/include/boost/math/quadrature/gauss_kronrod.hpp:18:0,
from C:/Users/nhatlth/Documents/R/win-library/3.3/StanHeaders/include/src/stan/model/model_header.hpp:20,
from file29e417e718b4.cpp:8:
C:/Users/nhatlth/Documents/R/win-library/3.3/BH/include/boost/math/special_functions/legendre_stieltjes.hpp: In member function ‘std::vector boost::math::legendre_stieltjes::zeros() const’:
C:/Users/nhatlth/Documents/R/win-library/3.3/BH/include/boost/math/special_functions/legendre_stieltjes.hpp:205:18: error: ‘g’ does not name a type
auto g = [&](Real t) { return this->operator()(t); };
^
C:/Users/nhatlth/Documents/R/win-library/3.3/BH/include/boost/math/special_functions/legendre_stieltjes.hpp:206:18: error: ‘p’ does not name a type
auto p = boost::math::tools::bisect(g, lower_bound, upper_bound, tol);
^
C:/Users/nhatlth/Documents/R/win-library/3.3/BH/include/boost/math/special_functions/legendre_stieltjes.hpp:208:31: error: ‘p’ was not declared in this scope
Real x_nk_guess = p.first + (p.second - p.first)*half();
^
C:/Users/nhatlth/Documents/R/win-library/3.3/BH/include/boost/math/special_functions/legendre_stieltjes.hpp:211:18: error: ‘f’ does not name a type
auto f = [&] (Real x) { Real Pn = this->operator()(x);
^…

Can you please help me to translate this code into a code.hpp so that I can run in stan model. I know that I ask too much from you. But it seems that I’m stuck here since my C++ knowledge is very limited.

Thank you very much in advance!
Nhat


#18

Judging from error messages like this:

error: ‘g’ does not name a type

where g was declared with auto, I suspect that part of the problem is that you are trying to compile C++11 code with a compiler that is expecting older C++98 syntax (which, AFAIK, uses auto in a very different and mostly useless way that’s a holdover from legacy C).

What compiler are you using, and what flags are you passing to it?


#19

Hi James,

What I used is rstan and the code was in C++. I checked that in my computer there are many the C++ complier: MV C++ 2005 restributable to MV C++ 2015 Restributable and DEV C++ as well. I don’t know what flag means.
The thing is if I run the code in Rcpp format, it works well. So I think, the computer understands the syntax and know all the library. But if I include the code in the stan model then for some reason they cannot link to the correct library.

In the intsruction https://cran.r-project.org/web/packages/rstan/vignettes/external.html. They say that we can call a function in Boost Library Math whose header are pulled by the Stan Math Library by using the prefix boost::math.

The same thing that I want to use is to call the class gauss_kronrod<double, 15> from the boost::math::quadrature library by defining a new class integrator as boost::math::quadrature::gauss_kronrod<double, 15> integrator;

I think at this step the stan don’t know where the library boost::math::quadrature comes from. My question is what should I do to be able to add the line using namespace boost::math::quadrature;
in the program source:
"Program source:
1:
2: // includes from the plugin
3:
4:
5: // user includes
6: #define STAN__SERVICES__COMMAND_HPP// Code generated by Stan version 2.17.0
7:
8: #include <stan/model/model_header.hpp>
9:
10: namespace model30c83e0371b6_external_namespace {
11:
12: using std::istream;
13: using std::string;
14: using std::stringstream;
15: using std::vector;
16: using stan::io::dump;
17: using stan::math::lgamma;
18: using stan::model::prob_grad;
19: using namespace stan::math;
20:
21: typedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;
22: typedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;
23: typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;
24:
25: static int current_statement_begin__;
26:
27: stan::io::program_reader prog_reader__() {
28: stan::io::program_reader reader;
29: reader.add_event(0, 0, “start”, “model30c83e0371b6_external”);
30: reader.add_event(9, 9, “end”, “model30c83e0371b6_external”);
31: return reader;
32: }
33:
34: template <typename T0__, typename T1__, typename T2__>
35: typename boost::math::tools::promote_args<T0__, T1__, T2__>::type
36: integral_GK(const T0__& ll,
37: const T1__& ul,
38: const T2__& nu, std::ostream* pstream__);
39:
40:
41: #include “C:/Users/nhatlth/Documents/exp_GK.hpp”
42: class model30c83e0371b6_external : public prob_grad {
43: private:
44: public:
45: model30c83e0371b6_external(stan::io::var_context& context__,
46: std::ostream* pstream__ = 0)
47: : prob_grad(0) {
48: ctor_body(context__, 0, pstream__);
49: }
50:
51: model30c83e0371b6_external(stan::io::var_context& context__,
52: unsigned int random_seed__,
53: std::ostream* pstream__ = 0)
54: : prob_grad(0) {
55: ctor_body(context__, random_seed__, pstream__);
56: }

"

Thanks,
Nhat


#20

A flag is a command-line option. For example, if you were compiling C++11 code with an older g++ compiler, you might need to compile it like this:

g++ -std=c++11 -c my_code.cpp

not like this:

g++ -c my_code.cpp

Here, -std=c++11 is a flag.

What that probably really means is that the code that you used with Rcpp was compiled with a different compiler than the one that is giving you the error.

In any case, you really should be showing us exactly what steps you took that led to the error. Chances are the error is due to some step that you haven’t yet mentioned.


#21

Hi James,

Thanks alot for help me! I run the model as follows:
mc2 <-

functions { real integral_GK(real ll, real ul, real nu); }
model {} // use the integral_GK() function somehow

model_tmp2<-stan_model(model_code = mc2, model_name = “external”, allow_undefined = TRUE,
includes = paste0(’\n#include “’,
file.path(getwd(), ‘exp_GK.hpp’), '”\n’))

and the exp_GK.hpp file is:

template <typename T0__, typename T1__,typename T2__>
typename boost::math::tools::promote_args<T0__, T1__,T2__>::type
integral_GK(const T0__& ll, const T1__& ul,const T2__& nu, std::ostream* pstream__) {
boost::math::quadrature::gauss_kronrod<double, 15> integrator;
auto f1 = [&](double x) {
return std::exp(x);
};
double error;
double ll_new=ll * nu;
double ul_new=ul * nu;
double Q = integrator.integrate(f1, ll_new, ul_new, 0, 0, &error)/nu;
return Q;
}

and the error is:
In file included from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/BH/include/boost/config.hpp:39:0,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/BH/include/boost/math/tools/config.hpp:13,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core.hpp:12,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/mat.hpp:4,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math.hpp:4,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/src/stan/model/model_header.hpp:4,
from filec2060e74890.cpp:8:
C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/BH/include/boost/config/compiler/gcc.hpp:186:0: warning: “BOOST_NO_CXX11_RVALUE_REFERENCES” redefined

define BOOST_NO_CXX11_RVALUE_REFERENCES

^
:0:0: note: this is the location of the previous definition
In file included from filec2060e74890.cpp:41:0:
C:/Users/Nhat Le Thanh Hoang/Documents/exp_GK.hpp: In function ‘typename boost::math::tools::promote_args<RT1, RT2, A>::type modelc204f092276_external_namespace::integral_GK(const T0__&, const T1__&, const T2__&, std::ostream*)’:
C:/Users/Nhat Le Thanh Hoang/Documents/exp_GK.hpp:4:16: error: ‘boost::math::quadrature’ has not been declared
boost::math::quadrature::gauss_kronrod<double, 15> integrator;
^
C:/Users/Nhat Le Thanh Hoang/Documents/exp_GK.hpp:4:42: error: expected primary-expression before ‘double’
boost::math::quadrature::gauss_kronrod<double, 15> integrator;
^
C:/Users/Nhat Le Thanh Hoang/Documents/exp_GK.hpp:5:8: error: ‘f1’ does not name a type
auto f1 = [&](double x) {
^
C:/Users/Nhat Le Thanh Hoang/Documents/exp_GK.hpp:11:14: error: ‘integrator’ was not declared in this scope
double Q = integrator.integrate(f1, ll_new, ul_new, 0, 0, &error)/nu;
^
C:/Users/Nhat Le Thanh Hoang/Documents/exp_GK.hpp:11:35: error: ‘f1’ was not declared in this scope
double Q = integrator.integrate(f1, ll_new, ul_new, 0, 0, &error)/nu;
^
make: *** [filec2060e74890.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1:
2: // includes from the plugin
3:
4:
5: // user includes
6: #define STAN__SERVICES__COMMAND_HPP// Code generated by Stan version 2.17.0
7:
8: #include <stan/model/model_header.hpp>
9:
10: namespace modelc204f092276_external_namespace {
11:
12: using std::istream;
13: using std::string;
14: using std::stringstream;
15: using std::vector;
16: using stan::io::dump;
17: using stan::math::lgamma;
18: using stan::model::prob_grad;
19: using namespace stan::math;
20:
21: typedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;
22: typedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;
23: typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;
24:
25: static int current_statement_begin__;
26:
27: stan::io::program_reader prog_reader__() {
28: stan::io::program_reader reader;
29: reader.add_event(0, 0, “start”, “modelc204f092276_external”);
30: reader.add_event(3, 3, “end”, “modelc204f092276_external”);
31: return reader;
32: }
33:
34: template <typename T0__, typename T1__, typename T2__>
35: typename boost::math::tools::promote_args<T0__, T1__, T2__>::type
36: integral_GK(const T0__& ll,
37: const T1__& ul,
38: const T2__& nu, std::ostream* pstream__);
39:
40:
41: #include “C:/Users/Nhat Le Thanh Hoang/Documents/exp_GK.hpp”
42: class modelc204f092276_external : public prob_grad {
43: private:
44: public:
45: modelc204f092276_external(stan::io::var_context& context__,
46: std::ostream* pstream__ = 0)
47: : prob_grad(0) {
48: ctor_body(context__, 0, pstream__);
49: }
50:
51: modelc204f092276_external(stan::io::var_context& context__,
52: unsigned int random_seed__,
53: std::ostream* pstream__ = 0)
54: : prob_grad(0) {
55: ctor_body(context__, random_seed__, pstream__);
56: }
57:
58: void ctor_body(stan::io::var_context& context__,
59: unsigned int random_seed__,
60: std::ostream* pstream__) {
61: boost::ecuyer1988 base_rng__ =
62: stan::services::util::create_rng(random_seed__, 0);
63: (void) base_rng__; // suppress unused var warning
64:
65: current_statement_begin__ = -1;
66:
67: static const char* function__ = “modelc204f092276_external_namespace::modelc204f092276_external”;
68: (void) function__; // dummy to suppress unused var warning
69: size_t pos__;
70: (void) pos__; // dummy to suppress unused var warning
71: std::vector vals_i__;
72: std::vector vals_r__;
73: double DUMMY_VAR__(std::numeric_limits::quiet_NaN());
74: (void) DUMMY_VAR__; // suppress unused var warning
75:
76: // initialize member variables
77: try {
78:
79: // validate, data variables
80: // initialize data variables
81:
82:
83: // validate transformed data
84:
85: // validate, set parameter ranges
86: num_params_r__ = 0U;
87: param_ranges_i__.clear();
88: } catch (const std::exception& e) {
89: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
90: // Next line prevents compiler griping about no return
91: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
92: }
93: }
94:
95: ~modelc204f092276_external() { }
96:
97:
98: void transform_inits(const stan::io::var_context& context__,
99: std::vector& params_i__,
100: std::vector& params_r__,
101: std::ostream
pstream__) const {
102: stan::io::writer writer__(params_r__,params_i__);
103: size_t pos__;
104: (void) pos__; // dummy call to supress warning
105: std::vector vals_r__;
106: std::vector vals_i__;
107:
108: params_r__ = writer__.data_r();
109: params_i__ = writer__.data_i();
110: }
111:
112: void transform_inits(const stan::io::var_context& context,
113: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
114: std::ostream
pstream__) const {
115: std::vector params_r_vec;
116: std::vector params_i_vec;
117: transform_inits(context, params_i_vec, params_r_vec, pstream__);
118: params_r.resize(params_r_vec.size());
119: for (int i = 0; i < params_r.size(); ++i)
120: params_r(i) = params_r_vec[i];
121: }
122:
123:
124: template <bool propto__, bool jacobian__, typename T__>
125: T__ log_prob(vector<T__>& params_r__,
126: vector& params_i__,
127: std::ostream
pstream__ = 0) const {
128:
129: T__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
130: (void) DUMMY_VAR__; // suppress unused var warning
131:
132: T__ lp__(0.0);
133: stan::math::accumulator<T__> lp_accum__;
134:
135: try {
136: // model parameters
137: stan::io::reader<T__> in__(params_r__,params_i__);
138:
139:
140: // transformed parameters
141:
142:
143:
144: // validate transformed parameters
145:
146: const char* function__ = “validate transformed params”;
147: (void) function__; // dummy to suppress unused var warning
148:
149: // model body
150:
151:
152: } catch (const std::exception& e) {
153: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
154: // Next line prevents compiler griping about no return
155: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
156: }
157:
158: lp_accum__.add(lp__);
159: return lp_accum__.sum();
160:
161: } // log_prob()
162:
163: template <bool propto, bool jacobian, typename T_>
164: T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
165: std::ostream
pstream = 0) const {
166: std::vector<T_> vec_params_r;
167: vec_params_r.reserve(params_r.size());
168: for (int i = 0; i < params_r.size(); ++i)
169: vec_params_r.push_back(params_r(i));
170: std::vector vec_params_i;
171: return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
172: }
173:
174:
175: void get_param_names(std::vectorstd::string& names__) const {
176: names__.resize(0);
177: }
178:
179:
180: void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
181: dimss__.resize(0);
182: std::vector<size_t> dims__;
183: }
184:
185: template
186: void write_array(RNG& base_rng__,
187: std::vector& params_r__,
188: std::vector& params_i__,
189: std::vector& vars__,
190: bool include_tparams__ = true,
191: bool include_gqs__ = true,
192: std::ostream
pstream__ = 0) const {
193: vars__.resize(0);
194: stan::io::reader in__(params_r__,params_i__);
195: static const char
function__ = “modelc204f092276_external_namespace::write_array”;
196: (void) function__; // dummy to suppress unused var warning
197: // read-transform, write parameters
198:
199: if (!include_tparams__) return;
200: // declare and define transformed parameters
201: double lp__ = 0.0;
202: (void) lp__; // dummy to suppress unused var warning
203: stan::math::accumulator lp_accum__;
204:
205: double DUMMY_VAR__(std::numeric_limits::quiet_NaN());
206: (void) DUMMY_VAR__; // suppress unused var warning
207:
208: try {
209:
210:
211:
212: // validate transformed parameters
213:
214: // write transformed parameters
215:
216: if (!include_gqs__) return;
217: // declare and define generated quantities
218:
219:
220:
221: // validate generated quantities
222:
223: // write generated quantities
224: } catch (const std::exception& e) {
225: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
226: // Next line prevents compiler griping about no return
227: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
228: }
229: }
230:
231: template
232: void write_array(RNG& base_rng,
233: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
234: Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
235: bool include_tparams = true,
236: bool include_gqs = true,
237: std::ostream
pstream = 0) const {
238: std::vector params_r_vec(params_r.size());
239: for (int i = 0; i < params_r.size(); ++i)
240: params_r_vec[i] = params_r(i);
241: std::vector vars_vec;
242: std::vector params_i_vec;
243: write_array(base_rng,params_r_vec,params_i_vec,vars_vec,include_tparams,include_gqs,pstream);
244: vars.resize(vars_vec.size());
245: for (int i = 0; i < vars.size(); ++i)
246: vars(i) = vars_vec[i];
247: }
248:
249: static std::string model_name() {
250: return “modelc204f092276_external”;
251: }
252:
253:
254: void constrained_param_names(std::vectorstd::string& param_names__,
255: bool include_tparams__ = true,
256: bool include_gqs__ = true) const {
257: std::stringstream param_name_stream__;
258:
259: if (!include_gqs__ && !include_tparams__) return;
260:
261: if (!include_gqs__) return;
262: }
263:
264:
265: void unconstrained_param_names(std::vectorstd::string& param_names__,
266: bool include_tparams__ = true,
267: bool include_gqs__ = true) const {
268: std::stringstream param_name_stream__;
269:
270: if (!include_gqs__ && !include_tparams__) return;
271:
272: if (!include_gqs__) return;
273: }
274:
275: }; // model
276:
277: }
278:
279: typedef modelc204f092276_external_namespace::modelc204f092276_external stan_model;
280:
281: #include <rstan/rstaninc.hpp>
282: /

283: * Define Rcpp Module to expose stan_fit’s functions to R.
284: */
285: RCPP_MODULE(stan_fit4modelc204f092276_external_mod){
286: Rcpp::class_<rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external,
287: boost::random::ecuyer1988> >(“stan_fit4modelc204f092276_external”)
288: // .constructorRcpp::List()
289: .constructor<SEXP, SEXP, SEXP>()
290: // .constructor<SEXP, SEXP>()
291: .method(“call_sampler”,
292: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::call_sampler)
293: .method(“param_names”,
294: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::param_names)
295: .method(“param_names_oi”,
296: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::param_names_oi)
297: .method(“param_fnames_oi”,
298: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::param_fnames_oi)
299: .method(“param_dims”,
300: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::param_dims)
301: .method(“param_dims_oi”,
302: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::param_dims_oi)
303: .method(“update_param_oi”,
304: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::update_param_oi)
305: .method(“param_oi_tidx”,
306: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::param_oi_tidx)
307: .method(“grad_log_prob”,
308: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::grad_log_prob)
309: .method(“log_prob”,
310: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::log_prob)
311: .method(“unconstrain_pars”,
312: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::unconstrain_pars)
313: .method(“constrain_pars”,
314: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::constrain_pars)
315: .method(“num_pars_unconstrained”,
316: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::num_pars_unconstrained)
317: .method(“unconstrained_param_names”,
318: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::unconstrained_param_names)
319: .method(“constrained_param_names”,
320: &rstan::stan_fit<modelc204f092276_external_namespace::modelc204f092276_external, boost::random::ecuyer1988>::constrained_param_names)
321: ;
322: }
323:
324: // declarations
325: extern “C” {
326: SEXP filec2060e74890( ) ;
327: }
328:
329: // definition
330:
331: SEXP filec2060e74890( ){
332: return Rcpp::wrap(“external”);
333: }
334:
335:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/BH/include/boost/config.hpp:39:0,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/BH/include/boost/math/tools/config.hpp:13,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/core.hpp:12,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math/rev/mat.hpp:4,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-library/3.5/StanHeaders/include/stan/math.hpp:4,
from C:/Users/Nhat Le Thanh Hoang/Documents/R/win-
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-35~1.0/bin/x64/R CMD SHLIB filec2060e74890.cpp 2> filec2060e74890.cpp.err.txt’ had status 1

Nhat


#22

Offhand, I think this error is due to not having #include <boost/math/quadrature/gauss_kronrod.hpp> at the top of the file exp_GK.hpp, since the compiler doesn’t recognize boost::math::quadrature.

This error is almost certainly due to RStan not using a compiler that accepts C++11 by default. See here: http://www.cplusplus.com/forum/beginner/173056/

I’m not entirely sure how to solve this, but judging from First steps in using C++11 with Rcpp, it looks like there are two possible solutions, either adding the comment // [[Rcpp::plugins(cpp11)]] to the top of of the file exp_GK.hpp or running the command Sys.setenv("PKG_CXXFLAGS"="-std=c++11") before invoking stan_model. I’d try the comment first to see what happens.


#23

Hi James,

Adding the comment // [[Rcpp::plugins(cpp11)]] to the top of the file make the code works however I’m still unable to include the #include <boost/math/quadrature/gauss_kronrod.hpp> on the top of the file.
Accordding to the instruction https://cran.r-project.org/web/packages/rstan/vignettes/external.html in the example of besselK function. this function in the library boost::math whose header are pulled in the stan math library. The fact that I cannot call the class gauss_kronrod of the library boost/math/quadrature is due to the header are not pulled in the stan math library.

How can I fix this?

Thanks alot!
Nhat