When running a STAN simulation several time using the stan() function in R, the following warning appears:
Stan recompile to avoid R from crashing.
When replacing the stan() function by the stan_model() and sampling() function (as suggested in link, R crashes.
Interestingly, when removing the “generated quantities” section, there is no warning message anymore.
You can find my STAN and R script below.
STAN script (called ex.stan)
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
vector[K] beta_1;
}
parameters {
real alpha; // intercept
vector[K] beta; // coefficients for predictors
real<lower=0> sigma; // error scale
}
model {
beta ~ normal(0,beta_1);
y ~ normal(x * beta + alpha, sigma); // likelihood
}
generated quantities {
//vector[K] beta2 = beta*2;
}
R script
N=20
K=2
x1=1:N+rnorm(N,0,0.5)
x2=rnorm(N,2,1)
x=cbind(x1,x2)
a=2
b=3
y=ax1+bx2+rnorm(N,0,1)stan_data=list(N=N,
K=K,
x=x,
y=y,
beta_1=rep(5,K))fit=list()
for(i in 1:2){
fit[[i]] ← stan(file = “ex.stan”
data = stan_data,
warmup = 500,
iter = 2000,
chains = 4,
cores = 4,
thin = 1,
control=list(adapt_delta=0.8))
print(i)
}