Error in sink(type = "output") : invalid connection in map2stan in Windows 10

Hey guys, I met a problem to use map2stan for R modeling. Error in sink(type = “output”) : invalid connection. R version is 3.5.3. And I attached my code. as below,

library(rethinking)
data(reedfrogs)
d <- reedfrogs

make the tank cluster variable

d$tank <- 1:nrow(d)

fit

m12.1 <- map2stan(
alist(
surv ~ dbinom( density , p ) ,
logit§ <- a_tank[tank] ,
a_tank[tank] ~ dnorm( 0 , 5 )
),
data=d )
precis(m12.1, depth = 2)

Hope some guys can help me to fix it. I am urgent to solve it because I need to use map2stan to finish my exam questions.

Can you tell us what the line starting with error: (including the colon) is when you do

library(rstan)
stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
mod <- stan_model(model_code = stancode, verbose = TRUE)

This is the whole details of the error.

data(reedfrogs)
d <- reedfrogs
d$tank <- 1:nrow(d)
m12.1 <- map2stan(

  • alist(
  • surv ~ dbinom( density , p ) ,
    
  • logit(p) <- a_tank[tank] ,
    
  • a_tank[tank] ~ dnorm( 0 , 5 )
    
  • ),
  • data=d )
    Error in sink(type = “output”) : invalid connection
    Error in map2stan(alist(surv ~ dbinom(density, p), logit§ <- a_tank[tank], :
    Something went wrong, when calling Stan. Check any debug messages for clues, detective.
    invalid connection

precis(m12.1, depth = 2)
Error in precis(m12.1, depth = 2) : object ‘m12.1’ not found

We still need

library(rstan)
stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
mod <- stan_model(model_code = stancode, verbose = TRUE)

TRANSLATING MODEL ‘73fc79f8b1915e8208c736914c86d1a1’ FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model ‘73fc79f8b1915e8208c736914c86d1a1’.
COMPILING THE C++ CODE FOR MODEL ‘73fc79f8b1915e8208c736914c86d1a1’ NOW.
OS: x86_64, mingw32; rstan: 2.18.2; Rcpp: 1.0.1; inline: 0.3.15

setting environment variables:
PKG_LIBS = -L"D:/master course/R lanuage/software/R-3.5.3/library/StanHeaders/libs/x64" -lStanHeaders
PKG_CPPFLAGS = -I"D:/master course/R lanuage/software/R-3.5.3/library/Rcpp/include/" -I"D:/master course/R lanuage/software/R-3.5.3/library/RcppEigen/include/" -I"D:/master course/R lanuage/software/R-3.5.3/library/RcppEigen/include/unsupported" -I"D:/master course/R lanuage/software/R-3.5.3/library/BH/include" -I"D:/master course/R lanuage/software/R-3.5.3/library/StanHeaders/include/src/" -I"D:/master course/R lanuage/software/R-3.5.3/library/StanHeaders/include/" -I"D:/master course/R lanuage/software/R-3.5.3/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -std=c++1y

Program source :

1 :
2 : // includes from the plugin
3 : // [[Rcpp::plugins(cpp14)]]
4 :
5 : // user includes
6 : #define STAN__SERVICES__COMMAND_HPP// Code generated by Stan version 2.18.1
7 :
8 : #include <stan/model/model_header.hpp>
9 :
10 : namespace model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_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 : static int current_statement_begin__;
22 :
23 : stan::io::program_reader prog_reader__() {
24 : stan::io::program_reader reader;
25 : reader.add_event(0, 0, “start”, “model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1”);
26 : reader.add_event(3, 1, “end”, “model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1”);
27 : return reader;
28 : }
29 :
30 : class model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1 : public prob_grad {
31 : private:
32 : double y_mean;
33 : public:
34 : model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1(stan::io::var_context& context__,
35 : std::ostream* pstream__ = 0)
36 : : prob_grad(0) {
37 : ctor_body(context__, 0, pstream__);
38 : }
39 :
40 : model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1(stan::io::var_context& context__,
41 : unsigned int random_seed__,
42 : std::ostream* pstream__ = 0)
43 : : prob_grad(0) {
44 : ctor_body(context__, random_seed__, pstream__);
45 : }
46 :
47 : void ctor_body(stan::io::var_context& context__,
48 : unsigned int random_seed__,
49 : std::ostream* pstream__) {
50 : typedef double local_scalar_t__;
51 :
52 : boost::ecuyer1988 base_rng__ =
53 : stan::services::util::create_rng(random_seed__, 0);
54 : (void) base_rng__; // suppress unused var warning
55 :
56 : current_statement_begin__ = -1;
57 :
58 : static const char* function__ = “model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1”;
59 : (void) function__; // dummy to suppress unused var warning
60 : size_t pos__;
61 : (void) pos__; // dummy to suppress unused var warning
62 : std::vector vals_i__;
63 : std::vector vals_r__;
64 : local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
65 : (void) DUMMY_VAR__; // suppress unused var warning
66 :
67 : // initialize member variables
68 : try {
69 : current_statement_begin__ = 1;
70 : context__.validate_dims(“data initialization”, “y_mean”, “double”, context__.to_vec());
71 : y_mean = double(0);
72 : vals_r__ = context__.vals_r(“y_mean”);
73 : pos__ = 0;
74 : y_mean = vals_r__[pos__++];
75 :
76 : // validate, data variables
77 : current_statement_begin__ = 1;
78 : // initialize data variables
79 :
80 :
81 : // validate transformed data
82 :
83 : // validate, set parameter ranges
84 : num_params_r__ = 0U;
85 : param_ranges_i__.clear();
86 : current_statement_begin__ = 1;
87 : ++num_params_r__;
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 : ~model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1() { }
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 : if (!(context__.contains_r(“y”)))
109 : throw std::runtime_error(“variable y missing”);
110 : vals_r__ = context__.vals_r(“y”);
111 : pos__ = 0U;
112 : context__.validate_dims(“initialization”, “y”, “double”, context__.to_vec());
113 : double y(0);
114 : y = vals_r__[pos__++];
115 : try {
116 : writer__.scalar_unconstrain(y);
117 : } catch (const std::exception& e) {
118 : throw std::runtime_error(std::string("Error transforming variable y: ") + e.what());
119 : }
120 :
121 : params_r__ = writer__.data_r();
122 : params_i__ = writer__.data_i();
123 : }
124 :
125 : void transform_inits(const stan::io::var_context& context,
126 : Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
127 : std::ostream
pstream__) const {
128 : std::vector params_r_vec;
129 : std::vector params_i_vec;
130 : transform_inits(context, params_i_vec, params_r_vec, pstream__);
131 : params_r.resize(params_r_vec.size());
132 : for (int i = 0; i < params_r.size(); ++i)
133 : params_r(i) = params_r_vec[i];
134 : }
135 :
136 :
137 : template <bool propto__, bool jacobian__, typename T__>
138 : T__ log_prob(vector<T__>& params_r__,
139 : vector& params_i__,
140 : std::ostream
pstream__ = 0) const {
141 :
142 : typedef T__ local_scalar_t__;
143 :
144 : local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
145 : (void) DUMMY_VAR__; // suppress unused var warning
146 :
147 : T__ lp__(0.0);
148 : stan::math::accumulator<T__> lp_accum__;
149 :
150 : try {
151 : // model parameters
152 : stan::io::reader<local_scalar_t__> in__(params_r__,params_i__);
153 :
154 : local_scalar_t__ y;
155 : (void) y; // dummy to suppress unused var warning
156 : if (jacobian__)
157 : y = in__.scalar_constrain(lp__);
158 : else
159 : y = in__.scalar_constrain();
160 :
161 :
162 : // transformed parameters
163 :
164 :
165 :
166 : // validate transformed parameters
167 :
168 : const char* function__ = “validate transformed params”;
169 : (void) function__; // dummy to suppress unused var warning
170 :
171 : // model body
172 :
173 : current_statement_begin__ = 1;
174 : lp_accum__.add(normal_log<propto__>(y, y_mean, 1));
175 :
176 : } catch (const std::exception& e) {
177 : stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
178 : // Next line prevents compiler griping about no return
179 : throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
180 : }
181 :
182 : lp_accum__.add(lp__);
183 : return lp_accum__.sum();
184 :
185 : } // log_prob()
186 :
187 : template <bool propto, bool jacobian, typename T_>
188 : T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
189 : std::ostream
pstream = 0) const {
190 : std::vector<T_> vec_params_r;
191 : vec_params_r.reserve(params_r.size());
192 : for (int i = 0; i < params_r.size(); ++i)
193 : vec_params_r.push_back(params_r(i));
194 : std::vector vec_params_i;
195 : return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
196 : }
197 :
198 :
199 : void get_param_names(std::vectorstd::string& names__) const {
200 : names__.resize(0);
201 : names__.push_back(“y”);
202 : }
203 :
204 :
205 : void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
206 : dimss__.resize(0);
207 : std::vector<size_t> dims__;
208 : dims__.resize(0);
209 : dimss__.push_back(dims__);
210 : }
211 :
212 : template
213 : void write_array(RNG& base_rng__,
214 : std::vector& params_r__,
215 : std::vector& params_i__,
216 : std::vector& vars__,
217 : bool include_tparams__ = true,
218 : bool include_gqs__ = true,
219 : std::ostream
pstream__ = 0) const {
220 : typedef double local_scalar_t__;
221 :
222 : vars__.resize(0);
223 : stan::io::reader<local_scalar_t__> in__(params_r__,params_i__);
224 : static const char
function__ = “model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::write_array”;
225 : (void) function__; // dummy to suppress unused var warning
226 : // read-transform, write parameters
227 : double y = in__.scalar_constrain();
228 : vars__.push_back(y);
229 :
230 : // declare and define transformed parameters
231 : double lp__ = 0.0;
232 : (void) lp__; // dummy to suppress unused var warning
233 : stan::math::accumulator lp_accum__;
234 :
235 : local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
236 : (void) DUMMY_VAR__; // suppress unused var warning
237 :
238 : try {
239 :
240 :
241 :
242 : // validate transformed parameters
243 :
244 : // write transformed parameters
245 : if (include_tparams__) {
246 : }
247 : if (!include_gqs__) return;
248 : // declare and define generated quantities
249 :
250 :
251 :
252 : // validate generated quantities
253 :
254 : // write generated quantities
255 : } catch (const std::exception& e) {
256 : stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
257 : // Next line prevents compiler griping about no return
258 : throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
259 : }
260 : }
261 :
262 : template
263 : void write_array(RNG& base_rng,
264 : Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
265 : Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
266 : bool include_tparams = true,
267 : bool include_gqs = true,
268 : std::ostream
pstream = 0) const {
269 : std::vector params_r_vec(params_r.size());
270 : for (int i = 0; i < params_r.size(); ++i)
271 : params_r_vec[i] = params_r(i);
272 : std::vector vars_vec;
273 : std::vector params_i_vec;
274 : write_array(base_rng,params_r_vec,params_i_vec,vars_vec,include_tparams,include_gqs,pstream);
275 : vars.resize(vars_vec.size());
276 : for (int i = 0; i < vars.size(); ++i)
277 : vars(i) = vars_vec[i];
278 : }
279 :
280 : static std::string model_name() {
281 : return “model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1”;
282 : }
283 :
284 :
285 : void constrained_param_names(std::vectorstd::string& param_names__,
286 : bool include_tparams__ = true,
287 : bool include_gqs__ = true) const {
288 : std::stringstream param_name_stream__;
289 : param_name_stream__.str(std::string());
290 : param_name_stream__ << “y”;
291 : param_names__.push_back(param_name_stream__.str());
292 :
293 : if (!include_gqs__ && !include_tparams__) return;
294 :
295 : if (include_tparams__) {
296 : }
297 :
298 :
299 : if (!include_gqs__) return;
300 : }
301 :
302 :
303 : void unconstrained_param_names(std::vectorstd::string& param_names__,
304 : bool include_tparams__ = true,
305 : bool include_gqs__ = true) const {
306 : std::stringstream param_name_stream__;
307 : param_name_stream__.str(std::string());
308 : param_name_stream__ << “y”;
309 : param_names__.push_back(param_name_stream__.str());
310 :
311 : if (!include_gqs__ && !include_tparams__) return;
312 :
313 : if (include_tparams__) {
314 : }
315 :
316 :
317 : if (!include_gqs__) return;
318 : }
319 :
320 : }; // model
321 :
322 : }
323 :
324 : typedef model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1 stan_model;
325 :
326 : #include <rstan/rstaninc.hpp>
327 : /

328 : * Define Rcpp Module to expose stan_fit’s functions to R.
329 : */
330 : RCPP_MODULE(stan_fit4model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_mod){
331 : Rcpp::class_<rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1,
332 : boost::random::ecuyer1988> >(“stan_fit4model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1”)
333 : // .constructorRcpp::List()
334 : .constructor<SEXP, SEXP, SEXP>()
335 : // .constructor<SEXP, SEXP>()
336 : .method(“call_sampler”,
337 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::call_sampler)
338 : .method(“param_names”,
339 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_names)
340 : .method(“param_names_oi”,
341 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_names_oi)
342 : .method(“param_fnames_oi”,
343 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_fnames_oi)
344 : .method(“param_dims”,
345 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_dims)
346 : .method(“param_dims_oi”,
347 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_dims_oi)
348 : .method(“update_param_oi”,
349 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::update_param_oi)
350 : .method(“param_oi_tidx”,
351 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_oi_tidx)
352 : .method(“grad_log_prob”,
353 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::grad_log_prob)
354 : .method(“log_prob”,
355 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::log_prob)
356 : .method(“unconstrain_pars”,
357 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::unconstrain_pars)
358 : .method(“constrain_pars”,
359 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::constrain_pars)
360 : .method(“num_pars_unconstrained”,
361 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::num_pars_unconstrained)
362 : .method(“unconstrained_param_names”,
363 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::unconstrained_param_names)
364 : .method(“constrained_param_names”,
365 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::constrained_param_names)
366 : .method(“standalone_gqs”,
367 : &rstan::stan_fit<model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1_namespace::model1db8168c1528_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::standalone_gqs)
368 : ;
369 : }
370 :
371 : // declarations
372 : extern “C” {
373 : SEXP file1db831697ea5( ) ;
374 : }
375 :
376 : // definition
377 :
378 : SEXP file1db831697ea5( ){
379 : return Rcpp::wrap(“73fc79f8b1915e8208c736914c86d1a1”);
380 : }
381 :
382 :
Compilation argument:
D:/master course/R lanuage/software/R-3.5.3/bin/x64/R CMD SHLIB file1db831697ea5.cpp 2> file1db831697ea5.cpp.err.txt
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘file1db831697ea5.cpp.err.txt’: No such file or directory

I see spaces in one of the compilation arguments. That usually causes problems.

Thank you so much!!!!!!!!!!!!!I fixed it by deleting the spaces

I believe I am having the same issue with map2stan in rethinking. I am getting an identical error. As this solution seems to have worked for @houshengyi , could you share the steps you took to edit the compilation arguments to remove spaces?