Hi
I’m trying to run a model but it crashes RStudio with a message I can’t read in full (see below) and also don’t understand. The model is probably wrong, but based on this error message(s) I can’t figure out what I did wrong.
data{
int N;
int adopted[N];
vector[N] days_to_event;
int color_id[N];
}
parameters{
vector[2] a;
real<lower=0> k;
real beta;
}
transformed parameters{
vector[N] lambda;
for ( i in 1:N ) {
lambda[i] = exp((a[color_id[i]]*beta)*k);
}
}
model{
a~exponential(0.4);
beta~exponential(0.4);
k~exponential(1);
for ( i in 1:N )
if ( adopted[i] == 0 ) target += weibull_lccdf(days_to_event[i] | k,lambda[i]);
for ( i in 1:N )
if ( adopted[i] == 1 ) days_to_event[i] ~ weibull( k,lambda[i]);
}
generated quantities{
real pred[N];
for ( i in 1:N )
if ( adopted[i] == 1 ) pred[i] = weibull_rng( k,lambda[i] );
for ( i in 1:N )
if ( adopted[i] == 0 ) pred[i] = weibull_lccdf(days_to_event[i] | k,lambda[i]);
}
@rok_cesnovar I think this is a bug since the declaration of a vector should promote the data to reals.
Other than that you have another issue. The parameters a and beta should be constrained to the positive reals since an exponential prior is only defined for positive reals. However, this is unlikely what you want since you’re creating lambda using exp, which means lambda > 0.
Efficiency stuff. You’re doubling loops. Instead of having to loop through N twice just do
for ( i in 1:N ) {
if ( adopted[i] == 0 ) target += weibull_lccdf( days_to_event[i] | k, lambda[i] );
else days_to_event[i] ~ weibull( k, lambda[i] );
}
The parameter lambda can be declared in one line as the exp() function is vectorized.
@spinkney I run the corrected model but I’m getting the same crash. I also tried to run it directly from R (Ubuntu 20.04) and R crashes with no error messages. Maybe I missed something?
data{
int N;
int adopted[N];
vector[N] days_to_event;
int color_id[N];
}
parameters{
vector[2] a;
real<lower=0> k;
real<lower=0> beta;
}
transformed parameters{
vector[N] lambda = exp( (a[color_id] * beta) * k);
}
model{
a~exponential(0.4);
beta~exponential(0.4);
k~exponential(1);
for ( i in 1:N ) {
if (adopted[i] == 0 ) target += weibull_lccdf( days_to_event[i] | k, lambda[i] );
else days_to_event[i] ~ weibull( k, lambda[i] );
}
}
generated quantities{
real pred[N];
for ( i in 1:N )
if ( adopted[i] == 1 ) pred[i] = weibull_rng( k,lambda[i] );
for ( i in 1:N )
if ( adopted[i] == 0 ) pred[i] = weibull_lccdf(days_to_event[i] | k,lambda[i]);
}
did you remove the rounding? Also you need a lower = 0 on a
# not this
# black<-round(rweibull(1000,1.1,65))
# other<-round(rweibull(1000,1.1,55))
# this
black <- rweibull(1000, 1.1, 65)
other <- rweibull(1000, 1.1, 55)
RStan crashes are commonly caused by a couple of things:
Having -march=native in your Makevars.win
Recently upgrading from R3.x to R4.x and having the old .rds model files get loaded
Not having enough RAM to store all parameter estimates for the model
If none of those are your issue, then restart R (making sure rstan doesn’t get loaded) and try reinstalling rstan and StanHeaders from source via:
# Compile packages using all cores
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))
install.packages(c("StanHeaders","rstan"),type="source")