R aborted when running a copied model

rstan
#1

Hi Staners,

I am new to Stan and I have been having problems running my model. I am trying to apply the a spatial model using the model constructed for the lip cancer dataset.

When I apply the model to my own dataset, it works. However, when I copy, paste and save the codes of the model into a new stan file and run the model with the file, RStudio aborts. I have tried to figured out why but still in vain. I am hoping that someone here could please help me with this…

Here are the structure of these two model (stancar_prec works but not m_s1, which is my copy of it)

Formal class ‘stanmodel’ [package “rstan”] with 5 slots
…@ model_name : chr “stancar_prec”
…@ model_code : chr “data {\n int<lower = 1> n;\n int<lower = 1> p;\n matrix[n, p] X;\n int<lower = 0> y[n];\n vector[n] log_of”| truncated
… …- attr(*, “model_name2”)= chr “stancar_prec”
…@ model_cpp :List of 2
… … model_cppname: chr “model14e06b995cb9_stancar_prec”
… … model_cppcode: chr “// Code generated by Stan version 2.14\n\n#include <stan/model/model_header.hpp>\n\nnamespace model14e06b995cb9”| truncated
…@ mk_cppmodule:function (object)
…@ dso :Formal class ‘cxxdso’ [package “rstan”] with 7 slots
… … …@ sig :List of 1
… … … … file14e07e174f10: chr(0)
… … …@ dso_saved : logi TRUE
… … …@ dso_filename: chr “file14e07e174f10”
… … …@ modulename : chr “stan_fit4model14e06b995cb9_stancar_prec_mod”
… … …@ system : chr “x86_64, mingw32”
… … …@ cxxflags : chr “CXXFLAGS = -O2 -Wall $(DEBUGFLAG) -mtune=core2”
… … …@ .CXXDSOMISC :<environment: 0x00000217512ba7d8>

Formal class ‘stanmodel’ [package “rstan”] with 5 slots
…@ model_name : chr “m_s1”
…@ model_code : chr “data {\n int<lower = 1> n;\n int<lower = 1> p;\n matrix[n, p] X;\n int<lower = 0> y[n];\n vector[n] log_of”| truncated
… …- attr(*, “model_name2”)= chr “m_s1”
…@ model_cpp :List of 2
… … model_cppname: chr “model30287eaf4c0a_m_s1”
… … model_cppcode: chr “// Code generated by Stan version 2.18.1\n\n#include <stan/model/model_header.hpp>\n\nnamespace model30287eaf4c”| truncated
…@ mk_cppmodule:function (object)
…@ dso :Formal class ‘cxxdso’ [package “rstan”] with 7 slots
… … …@ sig :List of 1
… … … … file3028666915dc: chr(0)
… … …@ dso_saved : logi TRUE
… … …@ dso_filename: chr “file3028666915dc”
… … …@ modulename : chr “stan_fit4model30287eaf4c0a_m_s1_mod”
… … …@ system : chr “x86_64, mingw32”
… … …@ cxxflags : chr “CXXFLAGS = -O2 -Wall $(DEBUGFLAG) -mtune=generic”
… … …@ .CXXDSOMISC :<environment: 0x000002176e55a6f8>

I noticed that they have different cxxflags: mtune=core2 in stancar_prec and mtune=generic in m_s1. Can that be the reason? If yes, how can I resolve it? If not, what are other possibilities?

I really appreciate your help!

Regards
Kendy

#2

Can you say more about how you copied the model and how it ended up with different compiler flags? That is not going to work across R sessions because of the dynamic shared library you can write it to the disk in order to avoid recompilation.

#3

Thank you bgoodri for the quick reply!

I opened a new stan script in RStudio, selected the codes, copied with “control C” and pasted with “control V” into the stan script. Then I saved it. I got the results of the str() by reading the two stan files in different environment. I don’t know why it ended up with different compiler flags.

When I read the stancar_prec model with stan_model() first and the m_s1 model, I could run the sampling without any problem (as the model has compiled?). However, If I only read the m_s1 and run the sampling, it will abort right after showing SAMPLING FOR MODEL “m_s1” NOW (CHAIN 1).

Also, when I read the model into RStudio without using stan_model() but
model_lip <- "
data {
int<lower = 1> n;
int<lower = 1> p;
matrix[n, p] X;
int<lower = 0> y[n];
vector[n] log_offset;
matrix<lower = 0, upper = 1>[n, n] W;
}
transformed data{
vector[n] zeros;
matrix<lower = 0>[n, n] D;
{
vector[n] W_rowsums;
for (i in 1:n) {
W_rowsums[i] = sum(W[i, ]);
}
D = diag_matrix(W_rowsums);
}
zeros = rep_vector(0, n);
}
parameters {
vector[p] beta;
vector[n] phi;
real<lower = 0> tau;
real<lower = 0, upper = 1> alpha;
}
model {
phi ~ multi_normal_prec(zeros, tau * (D - alpha * W));
beta ~ normal(0, 1);
tau ~ gamma(2, 2);
y ~ poisson_log(X * beta + phi + log_offset);
}
"

and run the sampling with
full_fit <- stan(model_code = model_lip, data = full_d, iter = 200, chains = 1, verbose = FALSE)
RStutio shows
SAMPLING FOR MODEL “294e8949b66a2d4aa1c272295e98e752” NOW (CHAIN 1)
and then aborts.

#4

That will cause R to crash. What you need to do is compile the model once, by either passing auto_write = TRUE to stan_model (or stan) or by specifying rstan_options(auto_write = TRUE) before calling stan_model or stan. Then the next time you estimate the parameters of the same Stan program (possibly with different data) it will not recompile and not crash.

#5

I’ve always run rstan_options(auto_write = TRUE) before I call stan_model or stan. R crushes every time when I start with stan_model(m_s1) and follow by sampling(m_s1) but not with stan_model(stancar_prec) followed by sampling(stancar_prec), even though they have the same codes. I hope I am making myself clear.

#6

Finally I solved the problem by changing the Makevar file from

CXX14FLAGS=-O3-mtune=native -march=native
CXX14FLAGS=-O3-mtune=native -march=native
to
CXX14FLAGS=-O3 -march=core2 -mtune=generic
CXX11FLAGS=-O3 -march=core2 -mtune=generic

I don’t know if it is the best solution, but at least now I can read the stan file that is compiled by me.

Thank you for the help, bgoodri :).