Hi, I am working on an survival analysis model, and used an example online (link posted below) . The professor who posted it said when he wrote this code in 2016 with older version of Stan, it worked without any problems. but with the newest version of Stan, I got the “initial value rejected” error. I wrote out Jags code, and it can sample successfully. I have been working on this example for weeks. Feel like a burnout. I have came to ask here, and got some suggestions. tried several ways to make it start sampling.
In this mode, Y is a matrix that contains only 0 or 1. dL0 follows gamma distribution. exp() won’t be a negative number. Therefore, Idt should be greater or equal to 0.
Idt[i, j] = Y[i, j] * exp(beta[1]*pscenter[i] + beta[2]*hhcenter[i] + beta[3]*ncomact[i] + beta[4]*rleader[i] + beta[5]*dleader[i] + beta[6]*inter1[i] + beta[7]*inter2[i]) * dL0[j];
However, if the absolute value is set around Idt, it starts sampling. This is weird to me. Apparently Stan did something in the background, but not sure what it is.
dN[i, j] ~ poisson(fabs(Idt[i, j]));
Or, if I exclude the 0s, it can also start sampling.
if(Y[i,j]==1){
dN[i, j] ~ poisson(Idt[i, j]); }
I am really confused here. Could anyone help me explain what Stan did to make it difficult to sample? Any suggestions are appreciated, thanks!
library(ggplot2)
library(plyr)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
cox_model <-
"data {
int<lower=0> Nsubj;
int<lower=1> T;
vector[Nsubj] pscenter;
vector[Nsubj] hhcenter;
int<lower=0,upper=1> ncomact[Nsubj];
int<lower=0,upper=1> rleader[Nsubj];
int<lower=0,upper=1> dleader[Nsubj];
vector[Nsubj] inter1;
vector[Nsubj] inter2;
int<lower=0,upper=1> FAIL[Nsubj];
int<lower=0> obs_t[Nsubj];
int<lower=0> t[T+1];
}
transformed data {
int<lower=0> Y[Nsubj, T];
int<lower=0> dN[Nsubj, T];
for(i in 1:Nsubj) {
for(j in 1:T) {
Y[i,j] =int_step(obs_t[i] - t[j]);
dN[i,j] =Y[i, j] * int_step(t[j + 1] - obs_t[i]) * FAIL[i];
}
}
}
parameters {
vector[7] beta;
real<lower=0> c;
real<lower=0> r;
vector[T] dL0;
}
transformed parameters {
vector[T] mu;
matrix[Nsubj, T] Idt;
vector[T] dL0_star;
for(j in 1:T) {
for(i in 1:Nsubj) {
Idt[i, j] = Y[i, j] * exp(beta[1]*pscenter[i] + beta[2]*hhcenter[i] + beta[3]*ncomact[i]
+ beta[4]*rleader[i] + beta[5]*dleader[i] + beta[6]*inter1[i] + beta[7]*inter2[i]) * dL0[j];
}
dL0_star[j]= r * (t[j + 1] - t[j]);
mu[j] <- dL0_star[j] * c;
}
}
model {
for(j in 1:T) {
for(i in 1:Nsubj) {
dN[i, j] ~ poisson(Idt[i, j]);
}
dL0[j] ~ gamma(mu[j], c);
}
c ~ gamma(0.0001, 0.00001);
r ~ gamma(0.001, 0.0001);
beta ~ normal(0.0, 100000);
}"
data <- read_rdump('survival.data.r')
inits <- list(c1=list(beta=c(-.36,-.26,-.29,-.22,-.61,-9.73,-.23), c=0.01, r=0.01,
dL0=c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)))
fit <- stan(model_code=cox_model, data=data, init=inits, iter=2000, chains=1, verbose=F)
data:
code: https://github.com/fonnesbeck/stan_workshop_2016/blob/master/code/survival.r