stan-dev / stan-dev/rstan

Code from Manual 2.17 section 9.2 fails to compile

Open
#477 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

I tried to compile the code from section 9.2 in the stan-reference 2.17 ,i.e., the QR reparameterization of multiple linear regression and it fails with error message:

error: call of overloaded ‘sqrt(int)’ is ambiguous

Description:

The following code is compiled:

data {
    int<lower=0> N; // number of data items
    int<lower=0> K; // number of predictors
    matrix[N, K] x; // predictor matrix
    vector[N] y;    // outcome vector
}
transformed data {
    matrix[N, K] Q_ast;
    matrix[K, K] R_ast;
    matrix[K, K] R_ast_inverse;
    // thin and scale the QR decomposition
    Q_ast = qr_Q(x)[, 1:K] * sqrt(N - 1);
    R_ast = qr_R(x)[1:K, ] / sqrt(N - 1);
    R_ast_inverse = inverse(R_ast);
}
parameters {
    real alpha;          // intercept
    vector[K] theta;     // coefficients on Q_ast
    real<lower=0> sigma; // error scale
}
model {
    y ~ normal(Q_ast * theta + alpha, sigma); // likelihood
}
generated quantities {
    vector[K] beta;
    beta = R_ast_inverse * theta; // coefficients on x
}

which yields the following output.

file269b5443f783.cpp: In member function ‘void model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr::ctor_body(stan::io::var_context&, unsigned int, std::ostream*)’:
file269b5443f783.cpp:142:240: error: call of overloaded ‘sqrt(int)’ is ambiguous
stan::math::assign(Q_ast, multiply(stan::model::rvalue(qr_Q(x), stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::nil_index_list())), "qr_Q(x)"),sqrt((N - 1))));
^
In file included from /usr/include/features.h:419:0,
from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
from /usr/include/stdint.h:26,
from /usr/lib/gcc/x86_64-linux-gnu/7/include/stdint.h:9,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/memory/stack_alloc.hpp:6,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file269b5443f783.cpp:8:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:149:1: note: candidate: double sqrt(double)
__MATHCALL (sqrt,, (Mdouble __x));
^
In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/config/no_tr1/cmath.hpp:21:0,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/math/tools/config.hpp:19,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core.hpp:12,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file269b5443f783.cpp:8:
/usr/include/c++/7/cmath:463:3: note: candidate: constexpr float std::sqrt(float)
sqrt(float __x)
^~~~
/usr/include/c++/7/cmath:467:3: note: candidate: constexpr long double std::sqrt(long double)
sqrt(long double __x)
^~~~
/usr/include/c++/7/cmath:475:5: note: candidate: constexpr typename __gnu_cxx::__enable_if<std::__is_integer<_Tp>::__value, double>::__type std::sqrt(_Tp) [with _Tp = int; typename __gnu_cxx::__enable_if<std::__is_integer<_Tp>::__value, double>::__type = double]
sqrt(_Tp __x)
^~~~
In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/pow.hpp:5:0,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/inc_beta.hpp:7,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/grad_inc_beta.hpp:12,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal.hpp:50,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/arr.hpp:9,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:13,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file269b5443f783.cpp:8:
/home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/sqrt.hpp:50:16: note: candidate: stan::math::var stan::math::sqrt(const stan::math::var&)
inline var sqrt(const var& a) {
^~~~
In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/prim/mat.hpp:209:0,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:12,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file269b5443f783.cpp:8:
/home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/prim/mat/fun/sqrt.hpp:32:5: note: candidate: typename stan::math::apply_scalar_unary<stan::math::sqrt_fun, T>::return_t stan::math::sqrt(const T&) [with T = int; typename stan::math::apply_scalar_unary<stan::math::sqrt_fun, T>::return_t = double]
sqrt(const T& x) {
^~~~
file269b5443f783.cpp:144:238: error: call of overloaded ‘sqrt(int)’ is ambiguous
stan::math::assign(R_ast, divide(stan::model::rvalue(qr_R(x), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::cons_list(stan::model::index_omni(), stan::model::nil_index_list())), "qr_R(x)"),sqrt((N - 1))));
^
In file included from /usr/include/features.h:419:0,
from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
from /usr/include/stdint.h:26,
from /usr/lib/gcc/x86_64-linux-gnu/7/include/stdint.h:9,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/memory/stack_alloc.hpp:6,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file269b5443f783.cpp:8:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:149:1: note: candidate: double sqrt(double)
__MATHCALL (sqrt,, (Mdouble __x));
^
In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/config/no_tr1/cmath.hpp:21:0,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/math/tools/config.hpp:19,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core.hpp:12,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file269b5443f783.cpp:8:
/usr/include/c++/7/cmath:463:3: note: candidate: constexpr float std::sqrt(float)
sqrt(float __x)
^~~~
/usr/include/c++/7/cmath:467:3: note: candidate: constexpr long double std::sqrt(long double)
sqrt(long double __x)
^~~~
/usr/include/c++/7/cmath:475:5: note: candidate: constexpr typename __gnu_cxx::__enable_if<std::__is_integer<_Tp>::__value, double>::__type std::sqrt(_Tp) [with _Tp = int; typename __gnu_cxx::__enable_if<std::__is_integer<_Tp>::__value, double>::__type = double]
sqrt(_Tp __x)
^~~~
In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/pow.hpp:5:0,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/inc_beta.hpp:7,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/grad_inc_beta.hpp:12,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal.hpp:50,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/arr.hpp:9,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:13,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file269b5443f783.cpp:8:
/home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/sqrt.hpp:50:16: note: candidate: stan::math::var stan::math::sqrt(const stan::math::var&)
inline var sqrt(const var& a) {
^~~~
In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/prim/mat.hpp:209:0,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:12,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4,
from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file269b5443f783.cpp:8:
/home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/prim/mat/fun/sqrt.hpp:32:5: note: candidate: typename stan::math::apply_scalar_unary<stan::math::sqrt_fun, T>::return_t stan::math::sqrt(const T&) [with T = int; typename stan::math::apply_scalar_unary<stan::math::sqrt_fun, T>::return_t = double]
sqrt(const T& x) {
^~~~
make: *** [file269b5443f783.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 model269b4e3a3303_multiple_linear_regression_qr_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", "model269b4e3a3303_multiple_linear_regression_qr");
30: reader.add_event(31, 31, "end", "model269b4e3a3303_multiple_linear_regression_qr");
31: return reader;
32: }
33:
34: class model269b4e3a3303_multiple_linear_regression_qr : public prob_grad {
35: private:
36: int N;
37: int K;
38: matrix_d x;
39: vector_d y;
40: matrix_d Q_ast;
41: matrix_d R_ast;
42: matrix_d R_ast_inverse;
43: public:
44: model269b4e3a3303_multiple_linear_regression_qr(stan::io::var_context& context__,
45: std::ostream* pstream__ = 0)
46: : prob_grad(0) {
47: ctor_body(context__, 0, pstream__);
48: }
49:
50: model269b4e3a3303_multiple_linear_regression_qr(stan::io::var_context& context__,
51: unsigned int random_seed__,
52: std::ostream* pstream__ = 0)
53: : prob_grad(0) {
54: ctor_body(context__, random_seed__, pstream__);
55: }
56:
57: void ctor_body(stan::io::var_context& context__,
58: unsigned int random_seed__,
59: std::ostream* pstream__) {
60: boost::ecuyer1988 base_rng__ =
61: stan::services::util::create_rng(random_seed__, 0);
62: (void) base_rng__; // suppress unused var warning
63:
64: current_statement_begin__ = -1;
65:
66: static const char* function__ = "model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr";
67: (void) function__; // dummy to suppress unused var warning
68: size_t pos__;
69: (void) pos__; // dummy to suppress unused var warning
70: std::vector vals_i__;
71: std::vector vals_r__;
72: double DUMMY_VAR__(std::numeric_limits::quiet_NaN());
73: (void) DUMMY_VAR__; // suppress unused var warning
74:
75: // initialize member variables
76: try {
77: current_statement_begin__ = 6;
78: context__.validate_dims("data initialization", "N", "int", context__.to_vec());
79: N = int(0);
80: vals_i__ = context__.vals_i("N");
81: pos__ = 0;
82: N = vals_i__[pos__++];
83: current_statement_begin__ = 7;
84: context__.validate_dims("data initialization", "K", "int", context__.to_vec());
85: K = int(0);
86: vals_i__ = context__.vals_i("K");
87: pos__ = 0;
88: K = vals_i__[pos__++];
89: current_statement_begin__ = 8;
90: validate_non_negative_index("x", "N", N);
91: validate_non_negative_index("x", "K", K);
92: context__.validate_dims("data initialization", "x", "matrix_d", context__.to_vec(N,K));
93: validate_non_negative_index("x", "N", N);
94: validate_non_negative_index("x", "K", K);
95: x = matrix_d(static_castEigen::VectorXd::Index(N),static_castEigen::VectorXd::Index(K));
96: vals_r__ = context__.vals_r("x");
97: pos__ = 0;
98: size_t x_m_mat_lim__ = N;
99: size_t x_n_mat_lim__ = K;
100: for (size_t n_mat__ = 0; n_mat__ < x_n_mat_lim__; ++n_mat__) {
101: for (size_t m_mat__ = 0; m_mat__ < x_m_mat_lim__; ++m_mat__) {
102: x(m_mat__,n_mat__) = vals_r__[pos__++];
103: }
104: }
105: current_statement_begin__ = 9;
106: validate_non_negative_index("y", "N", N);
107: context__.validate_dims("data initialization", "y", "vector_d", context__.to_vec(N));
108: validate_non_negative_index("y", "N", N);
109: y = vector_d(static_castEigen::VectorXd::Index(N));
110: vals_r__ = context__.vals_r("y");
111: pos__ = 0;
112: size_t y_i_vec_lim__ = N;
113: for (size_t i_vec__ = 0; i_vec__ < y_i_vec_lim__; ++i_vec__) {
114: y[i_vec__] = vals_r__[pos__++];
115: }
116:
117: // validate, data variables
118: current_statement_begin__ = 6;
119: check_greater_or_equal(function__,"N",N,0);
120: current_statement_begin__ = 7;
121: check_greater_or_equal(function__,"K",K,0);
122: current_statement_begin__ = 8;
123: current_statement_begin__ = 9;
124: // initialize data variables
125: current_statement_begin__ = 12;
126: validate_non_negative_index("Q_ast", "N", N);
127: validate_non_negative_index("Q_ast", "K", K);
128: Q_ast = matrix_d(static_castEigen::VectorXd::Index(N),static_castEigen::VectorXd::Index(K));
129: stan::math::fill(Q_ast,DUMMY_VAR__);
130: current_statement_begin__ = 13;
131: validate_non_negative_index("R_ast", "K", K);
132: validate_non_negative_index("R_ast", "K", K);
133: R_ast = matrix_d(static_castEigen::VectorXd::Index(K),static_castEigen::VectorXd::Index(K));
134: stan::math::fill(R_ast,DUMMY_VAR__);
135: current_statement_begin__ = 14;
136: validate_non_negative_index("R_ast_inverse", "K", K);
137: validate_non_negative_index("R_ast_inverse", "K", K);
138: R_ast_inverse = matrix_d(static_castEigen::VectorXd::Index(K),static_castEigen::VectorXd::Index(K));
139: stan::math::fill(R_ast_inverse,DUMMY_VAR__);
140:
141: current_statement_begin__ = 16;
142: stan::math::assign(Q_ast, multiply(stan::model::rvalue(qr_Q(x), stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::nil_index_list())), "qr_Q(x)"),sqrt((N - 1))));
143: current_statement_begin__ = 17;
144: stan::math::assign(R_ast, divide(stan::model::rvalue(qr_R(x), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::cons_list(stan::model::index_omni(), stan::model::nil_index_list())), "qr_R(x)"),sqrt((N - 1))));
145: current_statement_begin__ = 18;
146: stan::math::assign(R_ast_inverse, inverse(R_ast));
147:
148: // validate transformed data
149: current_statement_begin__ = 12;
150: current_statement_begin__ = 13;
151: current_statement_begin__ = 14;
152:
153: // validate, set parameter ranges
154: num_params_r__ = 0U;
155: param_ranges_i__.clear();
156: current_statement_begin__ = 21;
157: ++num_params_r__;
158: current_statement_begin__ = 22;
159: validate_non_negative_index("theta", "K", K);
160: num_params_r__ += K;
161: current_statement_begin__ = 23;
162: ++num_params_r__;
163: } catch (const std::exception& e) {
164: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
165: // Next line prevents compiler griping about no return
166: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
167: }
168: }
169:
170: ~model269b4e3a3303_multiple_linear_regression_qr() { }
171:
172:
173: void transform_inits(const stan::io::var_context& context__,
174: std::vector& params_i__,
175: std::vector& params_r__,
176: std::ostream
pstream__) const {
177: stan::io::writer writer__(params_r__,params_i__);
178: size_t pos__;
179: (void) pos__; // dummy call to supress warning
180: std::vector vals_r__;
181: std::vector vals_i__;
182:
183: if (!(context__.contains_r("alpha")))
184: throw std::runtime_error("variable alpha missing");
185: vals_r__ = context__.vals_r("alpha");
186: pos__ = 0U;
187: context__.validate_dims("initialization", "alpha", "double", context__.to_vec());
188: double alpha(0);
189: alpha = vals_r__[pos__++];
190: try {
191: writer__.scalar_unconstrain(alpha);
192: } catch (const std::exception& e) {
193: throw std::runtime_error(std::string("Error transforming variable alpha: ") + e.what());
194: }
195:
196: if (!(context__.contains_r("theta")))
197: throw std::runtime_error("variable theta missing");
198: vals_r__ = context__.vals_r("theta");
199: pos__ = 0U;
200: validate_non_negative_index("theta", "K", K);
201: context__.validate_dims("initialization", "theta", "vector_d", context__.to_vec(K));
202: vector_d theta(static_castEigen::VectorXd::Index(K));
203: for (int j1__ = 0U; j1__ < K; ++j1__)
204: theta(j1__) = vals_r__[pos__++];
205: try {
206: writer__.vector_unconstrain(theta);
207: } catch (const std::exception& e) {
208: throw std::runtime_error(std::string("Error transforming variable theta: ") + e.what());
209: }
210:
211: if (!(context__.contains_r("sigma")))
212: throw std::runtime_error("variable sigma missing");
213: vals_r__ = context__.vals_r("sigma");
214: pos__ = 0U;
215: context__.validate_dims("initialization", "sigma", "double", context__.to_vec());
216: double sigma(0);
217: sigma = vals_r__[pos__++];
218: try {
219: writer__.scalar_lb_unconstrain(0,sigma);
220: } catch (const std::exception& e) {
221: throw std::runtime_error(std::string("Error transforming variable sigma: ") + e.what());
222: }
223:
224: params_r__ = writer__.data_r();
225: params_i__ = writer__.data_i();
226: }
227:
228: void transform_inits(const stan::io::var_context& context,
229: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
230: std::ostream
pstream__) const {
231: std::vector params_r_vec;
232: std::vector params_i_vec;
233: transform_inits(context, params_i_vec, params_r_vec, pstream__);
234: params_r.resize(params_r_vec.size());
235: for (int i = 0; i < params_r.size(); ++i)
236: params_r(i) = params_r_vec[i];
237: }
238:
239:
240: template <bool propto__, bool jacobian__, typename T__>
241: T__ log_prob(vector<T__>& params_r__,
242: vector& params_i__,
243: std::ostream
pstream__ = 0) const {
244:
245: T__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
246: (void) DUMMY_VAR__; // suppress unused var warning
247:
248: T__ lp__(0.0);
249: stan::math::accumulator<T__> lp_accum__;
250:
251: try {
252: // model parameters
253: stan::io::reader<T__> in__(params_r__,params_i__);
254:
255: T__ alpha;
256: (void) alpha; // dummy to suppress unused var warning
257: if (jacobian__)
258: alpha = in__.scalar_constrain(lp__);
259: else
260: alpha = in__.scalar_constrain();
261:
262: Eigen::Matrix<T__,Eigen::Dynamic,1> theta;
263: (void) theta; // dummy to suppress unused var warning
264: if (jacobian__)
265: theta = in__.vector_constrain(K,lp__);
266: else
267: theta = in__.vector_constrain(K);
268:
269: T__ sigma;
270: (void) sigma; // dummy to suppress unused var warning
271: if (jacobian__)
272: sigma = in__.scalar_lb_constrain(0,lp__);
273: else
274: sigma = in__.scalar_lb_constrain(0);
275:
276:
277: // transformed parameters
278:
279:
280:
281: // validate transformed parameters
282:
283: const char* function__ = "validate transformed params";
284: (void) function__; // dummy to suppress unused var warning
285:
286: // model body
287:
288: current_statement_begin__ = 26;
289: lp_accum__.add(normal_log<propto__>(y, add(multiply(Q_ast,theta),alpha), sigma));
290:
291: } catch (const std::exception& e) {
292: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
293: // Next line prevents compiler griping about no return
294: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
295: }
296:
297: lp_accum__.add(lp__);
298: return lp_accum__.sum();
299:
300: } // log_prob()
301:
302: template <bool propto, bool jacobian, typename T_>
303: T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
304: std::ostream
pstream = 0) const {
305: std::vector<T_> vec_params_r;
306: vec_params_r.reserve(params_r.size());
307: for (int i = 0; i < params_r.size(); ++i)
308: vec_params_r.push_back(params_r(i));
309: std::vector vec_params_i;
310: return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
311: }
312:
313:
314: void get_param_names(std::vectorstd::string& names__) const {
315: names__.resize(0);
316: names__.push_back("alpha");
317: names__.push_back("theta");
318: names__.push_back("sigma");
319: names__.push_back("beta");
320: }
321:
322:
323: void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
324: dimss__.resize(0);
325: std::vector<size_t> dims__;
326: dims__.resize(0);
327: dimss__.push_back(dims__);
328: dims__.resize(0);
329: dims__.push_back(K);
330: dimss__.push_back(dims__);
331: dims__.resize(0);
332: dimss__.push_back(dims__);
333: dims__.resize(0);
334: dims__.push_back(K);
335: dimss__.push_back(dims__);
336: }
337:
338: template
339: void write_array(RNG& base_rng__,
340: std::vector& params_r__,
341: std::vector& params_i__,
342: std::vector& vars__,
343: bool include_tparams__ = true,
344: bool include_gqs__ = true,
345: std::ostream
pstream__ = 0) const {
346: vars__.resize(0);
347: stan::io::reader in__(params_r__,params_i__);
348: static const char
function__ = "model269b4e3a3303_multiple_linear_regression_qr_namespace::write_array";
349: (void) function__; // dummy to suppress unused var warning
350: // read-transform, write parameters
351: double alpha = in__.scalar_constrain();
352: vector_d theta = in__.vector_constrain(K);
353: double sigma = in__.scalar_lb_constrain(0);
354: vars__.push_back(alpha);
355: for (int k_0__ = 0; k_0__ < K; ++k_0__) {
356: vars__.push_back(theta[k_0__]);
357: }
358: vars__.push_back(sigma);
359:
360: if (!include_tparams__) return;
361: // declare and define transformed parameters
362: double lp__ = 0.0;
363: (void) lp__; // dummy to suppress unused var warning
364: stan::math::accumulator lp_accum__;
365:
366: double DUMMY_VAR__(std::numeric_limits::quiet_NaN());
367: (void) DUMMY_VAR__; // suppress unused var warning
368:
369: try {
370:
371:
372:
373: // validate transformed parameters
374:
375: // write transformed parameters
376:
377: if (!include_gqs__) return;
378: // declare and define generated quantities
379: current_statement_begin__ = 29;
380: validate_non_negative_index("beta", "K", K);
381: vector_d beta(static_castEigen::VectorXd::Index(K));
382: (void) beta; // dummy to suppress unused var warning
383:
384: stan::math::initialize(beta, std::numeric_limits::quiet_NaN());
385: stan::math::fill(beta,DUMMY_VAR__);
386:
387:
388: current_statement_begin__ = 30;
389: stan::math::assign(beta, multiply(R_ast_inverse,theta));
390:
391: // validate generated quantities
392: current_statement_begin__ = 29;
393:
394: // write generated quantities
395: for (int k_0__ = 0; k_0__ < K; ++k_0__) {
396: vars__.push_back(beta[k_0__]);
397: }
398:
399: } catch (const std::exception& e) {
400: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
401: // Next line prevents compiler griping about no return
402: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
403: }
404: }
405:
406: template
407: void write_array(RNG& base_rng,
408: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
409: Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
410: bool include_tparams = true,
411: bool include_gqs = true,
412: std::ostream
pstream = 0) const {
413: std::vector params_r_vec(params_r.size());
414: for (int i = 0; i < params_r.size(); ++i)
415: params_r_vec[i] = params_r(i);
416: std::vector vars_vec;
417: std::vector params_i_vec;
418: write_array(base_rng,params_r_vec,params_i_vec,vars_vec,include_tparams,include_gqs,pstream);
419: vars.resize(vars_vec.size());
420: for (int i = 0; i < vars.size(); ++i)
421: vars(i) = vars_vec[i];
422: }
423:
424: static std::string model_name() {
425: return "model269b4e3a3303_multiple_linear_regression_qr";
426: }
427:
428:
429: void constrained_param_names(std::vectorstd::string& param_names__,
430: bool include_tparams__ = true,
431: bool include_gqs__ = true) const {
432: std::stringstream param_name_stream__;
433: param_name_stream__.str(std::string());
434: param_name_stream__ << "alpha";
435: param_names__.push_back(param_name_stream__.str());
436: for (int k_0__ = 1; k_0__ <= K; ++k_0__) {
437: param_name_stream__.str(std::string());
438: param_name_stream__ << "theta" << '.' << k_0__;
439: param_names__.push_back(param_name_stream__.str());
440: }
441: param_name_stream__.str(std::string());
442: param_name_stream__ << "sigma";
443: param_names__.push_back(param_name_stream__.str());
444:
445: if (!include_gqs__ && !include_tparams__) return;
446:
447: if (!include_gqs__) return;
448: for (int k_0__ = 1; k_0__ <= K; ++k_0__) {
449: param_name_stream__.str(std::string());
450: param_name_stream__ << "beta" << '.' << k_0__;
451: param_names__.push_back(param_name_stream__.str());
452: }
453: }
454:
455:
456: void unconstrained_param_names(std::vectorstd::string& param_names__,
457: bool include_tparams__ = true,
458: bool include_gqs__ = true) const {
459: std::stringstream param_name_stream__;
460: param_name_stream__.str(std::string());
461: param_name_stream__ << "alpha";
462: param_names__.push_back(param_name_stream__.str());
463: for (int k_0__ = 1; k_0__ <= K; ++k_0__) {
464: param_name_stream__.str(std::string());
465: param_name_stream__ << "theta" << '.' << k_0__;
466: param_names__.push_back(param_name_stream__.str());
467: }
468: param_name_stream__.str(std::string());
469: param_name_stream__ << "sigma";
470: param_names__.push_back(param_name_stream__.str());
471:
472: if (!include_gqs__ && !include_tparams__) return;
473:
474: if (!include_gqs__) return;
475: for (int k_0__ = 1; k_0__ <= K; ++k_0__) {
476: param_name_stream__.str(std::string());
477: param_name_stream__ << "beta" << '.' << k_0__;
478: param_names__.push_back(param_name_stream__.str());
479: }
480: }
481:
482: }; // model
483:
484: }
485:
486: typedef model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr stan_model;
487:
488: #include <rstan/rstaninc.hpp>
489: /

490: * Define Rcpp Module to expose stan_fit's functions to R.
491: /
492: RCPP_MODULE(stan_fit4model269b4e3a3303_multiple_linear_regression_qr_mod){
493: Rcpp::class_<rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr,
494: boost::random::ecuyer1988> >("stan_fit4model269b4e3a3303_multiple_linear_regression_qr")
495: // .constructorRcpp::List()
496: .constructor<SEXP, SEXP, SEXP>()
497: // .constructor<SEXP, SEXP>()
498: .method("call_sampler",
499: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::call_sampler)
500: .method("param_names",
501: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_names)
502: .method("param_names_oi",
503: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_names_oi)
504: .method("param_fnames_oi",
505: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_fnames_oi)
506: .method("param_dims",
507: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_dims)
508: .method("param_dims_oi",
509: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_dims_oi)
510: .method("update_param_oi",
511: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::update_param_oi)
512: .method("param_oi_tidx",
513: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_oi_tidx)
514: .method("grad_log_prob",
515: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::grad_log_prob)
516: .method("log_prob",
517: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::log_prob)
518: .method("unconstrain_pars",
519: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::unconstrain_pars)
520: .method("constrain_pars",
521: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::constrain_pars)
522: .method("num_pars_unconstrained",
523: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::num_pars_unconstrained)
524: .method("unconstrained_param_names",
525: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::unconstrained_param_names)
526: .method("constrained_param_names",
527: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::constrained_param_names)
528: ;
529: }
530:
531: // declarations
532: extern "C" {
533: SEXP file269b5443f783( ) ;
534: }
535:
536: // definition
537:
538: SEXP file269b5443f783( ){
539: return Rcpp::wrap("multiple_linear_regression_qr");
540: }
541:
542:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! file269b5443f783.cpp: In member function ‘void model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr::ctor_body(stan::io::var_context&, unsigned int, std::ostream
)’:
file269b5443f783.cpp:142:240: error: call of overloaded ‘sqrt(int)’ is ambiguous
stan::math::assign(Q_ast, multiply(stan::model::rvalue(qr_Q(x), stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::nil_index_list())), "qr_Q(x)"),sqrt((N - 1))));
^
In file included from /usr/include/features.h:419:0,
from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,

In addition: Warning message:
running command '/usr/lib/R/bin/R CMD SHLIB file269b5443f783.cpp 2> file269b5443f783.cpp.err.txt' had status 1

Reproducible Steps:

Run the following R script

library(rstan)
library(Rcpp)
library(bayesplot)
library(ggplot2)
library(dautility)
m <- stan_model(file = "multiple_linear_regression_qr.stan")

where "multiple_linear_regression_qr.stan" contains the code given above.

RStan Version:

The version of RStan is 2.17.2

R Version:

R version 3.4.3 (2017-11-30)

package * version date source
abind 1.4-5 2016-07-21 CRAN (R 3.4.2)
assertthat 0.1 2013-12-06 CRAN (R 3.4.2)
base * 3.4.3 2017-12-01 local
bayesplot * 1.4.0 2017-09-12 cran (@1.4.0)
bindr 0.1 2016-11-13 CRAN (R 3.4.2)
bindrcpp 0.2 2017-06-17 CRAN (R 3.4.2)
colorspace 1.3-2 2016-12-14 CRAN (R 3.4.2)
compiler 3.4.3 2017-12-01 local
cowplot 0.9.1 2017-11-16 CRAN (R 3.4.2)
data.table 1.10.0 2016-12-03 CRAN (R 3.4.2)
datasets * 3.4.3 2017-12-01 local
dautility * 2.0.0 2017-11-19 local
devtools * 1.13.4 2017-11-09 CRAN (R 3.4.2)
digest 0.6.12 2017-01-27 CRAN (R 3.4.2)
dplyr * 0.7.4 2017-09-28 CRAN (R 3.4.2)
ellipse 0.3-8 2013-04-13 CRAN (R 3.4.2)
entropy 1.2.1 2014-11-14 CRAN (R 3.4.2)
fortunes 1.5-4 2016-12-29 CRAN (R 3.4.2)
GGally 1.3.2 2017-08-02 CRAN (R 3.4.2)
ggmcmc 1.1 2016-06-28 CRAN (R 3.4.2)
ggplot2 * 2.2.1 2016-12-30 CRAN (R 3.4.2)
glue 1.2.0 2017-10-29 CRAN (R 3.4.2)
graphics * 3.4.3 2017-12-01 local
grDevices * 3.4.3 2017-12-01 local
grid 3.4.3 2017-12-01 local
gridExtra 2.3 2017-09-09 cran (@2.3)
gtable 0.2.0 2016-02-26 CRAN (R 3.4.2)
httr 1.2.1 2016-07-03 CRAN (R 3.4.2)
inline 0.3.14 2015-04-13 CRAN (R 3.4.2)
ISOweek 0.6-2 2011-09-07 CRAN (R 3.4.2)
jsonlite 1.5 2017-06-01 cran (@1.5)
lattice 0.20-35 2017-03-25 CRAN (R 3.4.2)
lazyeval 0.2.0 2016-06-12 CRAN (R 3.4.2)
lubridate 1.6.0 2016-09-13 CRAN (R 3.4.2)
magrittr 1.5 2014-11-22 CRAN (R 3.4.2)
MASS * 7.3-47 2017-04-21 CRAN (R 3.4.2)
mclust 5.3 2017-05-21 CRAN (R 3.4.2)
memoise 1.0.0 2016-01-29 CRAN (R 3.4.2)
methods * 3.4.3 2017-12-01 local
munsell 0.4.3 2016-02-13 CRAN (R 3.4.2)
parallel 3.4.3 2017-12-01 local
pkgconfig 2.0.1 2017-03-21 CRAN (R 3.4.2)
plyr * 1.8.4 2016-06-08 CRAN (R 3.4.2)
R6 2.2.2 2017-06-17 CRAN (R 3.4.2)
RColorBrewer 1.1-2 2014-12-07 CRAN (R 3.4.2)
Rcpp * 0.12.14 2017-11-23 CRAN (R 3.4.3)
reshape 0.8.6 2016-10-21 CRAN (R 3.4.2)
reshape2 1.4.2 2016-10-22 CRAN (R 3.4.2)
rlang 0.1.4 2017-11-05 CRAN (R 3.4.2)
rstan * 2.17.2 2017-12-21 CRAN (R 3.4.3)
scales 0.4.1 2016-11-09 CRAN (R 3.4.2)
slackr 1.4.2 2016-07-20 CRAN (R 3.4.2)
ssh.utils 1.0 2014-07-24 CRAN (R 3.4.2)
StanHeaders * 2.17.1 2017-12-20 CRAN (R 3.4.3)
stats * 3.4.3 2017-12-01 local
stats4 3.4.3 2017-12-01 local
stringi 1.1.6 2017-11-17 cran (@1.1.6)
stringr 1.2.0 2017-02-18 CRAN (R 3.4.2)
tibble 1.3.4 2017-08-22 CRAN (R 3.4.2)
tidyr * 0.6.1 2017-01-10 CRAN (R 3.4.2)
timeDate 3012.100 2015-01-23 CRAN (R 3.4.2)
tools 3.4.3 2017-12-01 local
utils * 3.4.3 2017-12-01 local
withr 2.1.0 2017-11-01 CRAN (R 3.4.2)
yaml 2.1.15 2017-12-01 cran (@2.1.15)
zoo 1.8-0 2017-04-12 CRAN (R 3.4.2)

Operating System:

Linux einstein 4.13.0-21-generic #24-Ubuntu SMP Mon Dec 18 17:29:16 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

g++ (Ubuntu 7.2.0-8ubuntu3) 7.2.0

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure using the Stan program from Manual 2.17 section 9.2, then inspect the generated C++ around the two sqrt(N - 1) calls shown in the compiler output. Done means the example compiles without the ambiguous sqrt errors and still represents the documented QR reparameterization.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.