# Problem with using rstan

library(rstan)
model_code1="
functions{
//defined survival
vector log_s(vector t, real shape1, vector shape2){
vector[num_elements(t)] log_s;
for(i in 1:num_elements(t)){
log_s[i]=log(1-(1-(1+t[i])^(-shape2[i]) )^(shape1));

//log_s[i]=log(((1+t[i])^(scale[i]*shape) - ((1+t[i])^(scale[i]) -1)^shape )/((1 // +t[i])^(scale[i]*shape)));
}
return log_s;
}

//define log_ft
vector log_ft(vector t, real shape1, vector shape2){
vector[num_elements(t)] log_ft;
for(i in 1:num_elements(t)){
log_ft[i]=log((shape1 * shape2[i]) * (1-(1+t[i])^(shape2[i]))^(shape1-1) * (1+t[i])^-(shape2[i]+1));
}
return log_ft;
}
//define log hazard
vector log_h(vector t, real shape1, vector shape2){
vector[num_elements(t)] log_h;
vector[num_elements(t)] logft;
vector[num_elements(t)] logs;
logft=log_ft(t,shape1,shape2);
logs=log_s(t,shape1,shape2);
log_h=logft-logs;
return log_h;
}

//define the sampling distribution
real surv_expP_lpdf(vector t, vector d, real shape1 , vector shape2){
vector[num_elements(t)] log_lik;
real prob;
log_lik=d .* log_h(t,shape1,shape2)+log_s(t,shape1,shape2);
prob=sum(log_lik);
return prob;
}
}
//data block

data {
int N; // number of observations
vector<lower=0>[N] y; // observed times
vector<lower=0,upper=1>[N] censor;//censoring indicator (1=observed, 0=censored)
int M; // number of covariates
matrix[N, M] x; // matrix of covariates (with N rows and M columns)
}

parameters {
vector[M] beta; // Coefficients in the linear predictor (including intercept)
real<lower=0> shape1; // shape parameter

}

transformed parameters {
vector[N] linpred;
vector[N] shape2;
linpred = x * beta;
for (i in 1:N) {
shape2[i] = exp(linpred[i]);
}
}

model {
shape1 ~ uniform(0, 10);
beta ~ normal(0, 1);
y ~ surv_expP (censor, shape1, shape2);
}
generated quantities{
real dev;
dev=0;
dev=dev + (-2)*surv_expP_lpdf (y|censor,shape1,shape2);
}
"

# data creation in R

y<-c(23,47,69,70,71,100,101,148,181,198,208,212,224,5,8,10,13,18,24,26,26,31,35,40,41,48,50,59,61,68,71,76,105,107,109,113,116,118,143,154,162,188,212,217,225)
x1<-c(rep(0,13), rep(1,32))
censor<-c(rep(1,3),rep(0,4),rep(1,2),rep(0,4),rep(1,18),rep(0,4),1,0,1,1,rep(0,6))
x <- cbind(1,x1)
N = nrow(x)
M = ncol(x)
event=censor
dat <- list( y=y, x=x, event=event, N=N, M=M)
M1<-stan(model_code=model_code1,data=dat,init=list(list(beta=c(-0.6,0.5))),iter=1000,chains=1)

I find this error!!!

Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.
Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.

Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
 “Error in sampler\$call_sampler(args_list[[i]]) : Initialization failed.”
 “error occurred during calling the sampler; sampling not done”

The initialization error is presumably due to the fact that you declare `shape1` to not have an upper bound but put zero density for all values greater than 10. So, start by getting rid of that uniform prior, which is a bad idea anyway. There may be other problems.

Thanks for you
But I tried to use
shape1 ~ cauchy(0,5);
and
shape1~ normal (0,10);
i get the same problem

Then try specifying `init_r = 0.5` or some number less than 2 when you call Stan.

i do it, i get the same error…

and i use `init="random"` when i call Stan, but get same problem

I would put

``````if (!is_finite(prob)) {
print("prob is ", prob);
// print more stuff
}
``````

into the end of `surv_expP_lpdf` to figure out what goes wrong.

Sorry but I did not understand your intention.

Debug by print. Put print statements into your code to figure out where things become infinite. It’s the only way to track things down.

Thanks Bob, but where can i but that code ?

Before the return statement in your `surv_expP_lpdf` function.

I run it but i find this
`> model_code1="

• functions{
• //defined survival
• vector log_s(vector t, real shape1, vector shape2){
• vector[num_elements(t)] log_s;
• for(i in 1:num_elements(t)){
• log_s[i]=log(1-(1-(1+t[i])^(-shape2[i]) )^(shape1));
• //log_s[i]=log(((1+t[i])^(scale[i]*shape) - ((1+t[i])^(scale[i]) -1)^shape )/((1 // +t[i])^(scale[i]*shape)));
• }
• return log_s;
• }
• //define log_ft
• vector log_ft(vector t, real shape1, vector shape2){
• vector[num_elements(t)] log_ft;
• for(i in 1:num_elements(t)){
• log_ft[i]=log((shape1 * shape2[i]) * (1-(1+t[i])^(shape2[i]))^(shape1-1) * (1+t[i])^-(shape2[i]+1));
• }
• return log_ft;
• }
• //define log hazard
• vector log_h(vector t, real shape1, vector shape2){
• vector[num_elements(t)] log_h;
• vector[num_elements(t)] logft;
• vector[num_elements(t)] logs;
• logft=log_ft(t,shape1,shape2);
• logs=log_s(t,shape1,shape2);
• log_h=logft-logs;
• return log_h;
• }
• //define the sampling distribution
• real surv_expP_lpdf(vector t, vector d, real shape1 , vector shape2){
• vector[num_elements(t)] log_lik;
• real prob;
• log_lik=d .* log_h(t,shape1,shape2)+log_s(t,shape1,shape2);
• prob=sum(log_lik);
• if (!is_finite(prob)) { print(“prob is”, prob); // print more stuff }
Error: unexpected symbol in:
"prob=sum(log_lik);
if (!is_finite(prob)) { print(“prob”

return prob;
Error: unexpected symbol in “return prob”
}
Error: unexpected ‘}’ in “}”
}
Error: unexpected ‘}’ in “}”
//data block
Error: unexpected ‘/’ in “/”

data {
Error: unexpected ‘{’ in “data {”
int N; // number of observations
Error: unexpected symbol in “int N”
vector<lower=0>[N] y; // observed times
Error: unexpected ‘[’ in “vector<lower=0>[”
vector<lower=0,upper=1>[N] censor;//censoring indicator (1=observed, 0=censored)
Error: unexpected ‘,’ in “vector<lower=0,”
int M; // number of covariates
Error: unexpected symbol in “int M”
matrix[N, M] x; // matrix of covariates (with N rows and M columns)
Error: unexpected symbol in “matrix[N, M] x”
}
Error: unexpected ‘}’ in “}”

parameters {
Error: unexpected ‘{’ in “parameters {”
vector[M] beta; // Coefficients in the linear predictor (including intercept)
Error: unexpected symbol in “vector[M] beta”
real<lower=0> shape1; // shape parameter

}
Error: unexpected ‘}’ in “}”

transformed parameters {
Error: unexpected symbol in “transformed parameters”
vector[N] linpred;
Error: unexpected symbol in “vector[N] linpred”
vector[N] shape2;
Error: unexpected symbol in “vector[N] shape2”
linpred = x * beta;
for (i in 1:N) {

• shape2[i] = exp(linpred[i]);
• }

}
Error: unexpected ‘}’ in “}”

model {
Error: unexpected ‘{’ in “model {”
shape1 ~ normal(0, 2);
shape1 ~ normal(0, 2)
beta ~ normal(0, 2);
beta ~ normal(0, 2)
y ~ surv_expP (censor, shape1, shape2);
y ~ surv_expP(censor, shape1, shape2)
}
Error: unexpected ‘}’ in “}”
generated quantities{
Error: unexpected symbol in “generated quantities”
real dev;
Error: unexpected symbol in “real dev”
dev=0;
dev=dev + (-2)*surv_expP_lpdf (y|censor,shape1,shape2);
Error in surv_expP_lpdf(y | censor, shape1, shape2) :
could not find function “surv_expP_lpdf”

}
Error: unexpected ‘}’ in “}”
"`

You commented out the closing `}`. Also, you should print more stuff in that block in order to figure out under what conditions it goes wrong.

then the same
`> model_code1="

• functions{
• //defined survival
• vector log_s(vector t, real shape1, vector shape2){
• vector[num_elements(t)] log_s;
• for(i in 1:num_elements(t)){
• log_s[i]=log(1-(1-(1+t[i])^(-shape2[i]) )^(shape1));
• //log_s[i]=log(((1+t[i])^(scale[i]*shape) - ((1+t[i])^(scale[i]) -1)^shape )/((1 // +t[i])^(scale[i]*shape)));
• }
• return log_s;
• }
• //define log_ft
• vector log_ft(vector t, real shape1, vector shape2){
• vector[num_elements(t)] log_ft;
• for(i in 1:num_elements(t)){
• log_ft[i]=log((shape1 * shape2[i]) * (1-(1+t[i])^(shape2[i]))^(shape1-1) * (1+t[i])^-(shape2[i]+1));
• }
• return log_ft;
• }
• //define log hazard
• vector log_h(vector t, real shape1, vector shape2){
• vector[num_elements(t)] log_h;
• vector[num_elements(t)] logft;
• vector[num_elements(t)] logs;
• logft=log_ft(t,shape1,shape2);
• logs=log_s(t,shape1,shape2);
• log_h=logft-logs;
• return log_h;
• }
• //define the sampling distribution
• real surv_expP_lpdf(vector t, vector d, real shape1 , vector shape2){
• vector[num_elements(t)] log_lik;
• real prob;
• log_lik=d .* log_h(t,shape1,shape2)+log_s(t,shape1,shape2);
• prob=sum(log_lik);
• if (!is_finite(prob)) { print(“prob is”, prob);} // print more stuff
Error: unexpected symbol in:
"prob=sum(log_lik);
if (!is_finite(prob)) { print(“prob”

return prob;
Error: unexpected symbol in “return prob”
}
Error: unexpected ‘}’ in “}”
}
Error: unexpected ‘}’ in “}”
//data block
Error: unexpected ‘/’ in “/”

data {
Error: unexpected ‘{’ in “data {”
int N; // number of observations
Error: unexpected symbol in “int N”
vector<lower=0>[N] y; // observed times
Error: unexpected ‘[’ in “vector<lower=0>[”
vector<lower=0,upper=1>[N] censor;//censoring indicator (1=observed, 0=censored)
Error: unexpected ‘,’ in “vector<lower=0,”
int M; // number of covariates
Error: unexpected symbol in “int M”
matrix[N, M] x; // matrix of covariates (with N rows and M columns)
Error: unexpected symbol in “matrix[N, M] x”
}
Error: unexpected ‘}’ in “}”

parameters {
Error: unexpected ‘{’ in “parameters {”
vector[M] beta; // Coefficients in the linear predictor (including intercept)
Error: unexpected symbol in “vector[M] beta”
real<lower=0> shape1; // shape parameter

}
Error: unexpected ‘}’ in “}”

transformed parameters {
Error: unexpected symbol in “transformed parameters”
vector[N] linpred;
Error: unexpected symbol in “vector[N] linpred”
vector[N] shape2;
Error: unexpected symbol in “vector[N] shape2”
linpred = x * beta;
for (i in 1:N) {

• shape2[i] = exp(linpred[i]);
• }

}
Error: unexpected ‘}’ in “}”

model {
Error: unexpected ‘{’ in “model {”
shape1 ~ normal(0, 2);
shape1 ~ normal(0, 2)
beta ~ normal(0, 2);
beta ~ normal(0, 2)
y ~ surv_expP (censor, shape1, shape2);
y ~ surv_expP(censor, shape1, shape2)
}
Error: unexpected ‘}’ in “}”
generated quantities{
Error: unexpected symbol in “generated quantities”
real dev;
Error: unexpected symbol in “real dev”
dev=0;
dev=dev + (-2)*surv_expP_lpdf (y|censor,shape1,shape2);
Error in surv_expP_lpdf(y | censor, shape1, shape2) :
could not find function “surv_expP_lpdf”

}
Error: unexpected ‘}’ in “}”
"

• `

I DONT know what is this error?

I think it misencoded the quotation marks. Moreover, there actually isn’t an `is_finite` function in the Stan language, so you should use

``````if (is_inf(prob)) {
print("prob is ", prob);
// print more stuff
}
``````

this is i get

``````    `Error: unexpected symbol in:
``````

"if (is_inf(prob)) {
print(“prob”

}// print more stuff
Error: unexpected ‘}’ in “}”
return prob;
Error: unexpected symbol in “return prob”
}
Error: unexpected ‘}’ in “}”
}
Error: unexpected ‘}’ in “}”
//data block
Error: unexpected ‘/’ in “/”

data {
Error: unexpected ‘{’ in “data {”
int N; // number of observations
Error: unexpected symbol in “int N”
vector<lower=0>[N] y; // observed times
Error: unexpected ‘[’ in “vector<lower=0>[”
vector<lower=0,upper=1>[N] censor;//censoring indicator (1=observed, 0=censored)
Error: unexpected ‘,’ in “vector<lower=0,”
int M; // number of covariates
Error: unexpected symbol in “int M”
matrix[N, M] x; // matrix of covariates (with N rows and M columns)
Error: unexpected symbol in “matrix[N, M] x”
}
Error: unexpected ‘}’ in “}”

parameters {
Error: unexpected ‘{’ in “parameters {”
vector[M] beta; // Coefficients in the linear predictor (including intercept)
Error: unexpected symbol in “vector[M] beta”
real<lower=0> shape1; // shape parameter

}
Error: unexpected ‘}’ in “}”

transformed parameters {
Error: unexpected symbol in “transformed parameters”
vector[N] linpred;
Error: unexpected symbol in “vector[N] linpred”
vector[N] shape2;
Error: unexpected symbol in “vector[N] shape2”
linpred = x * beta;
for (i in 1:N) {

• shape2[i] = exp(linpred[i]);
• }

}
Error: unexpected ‘}’ in “}”

model {
Error: unexpected ‘{’ in “model {”
shape1 ~ normal(0, 2);
shape1 ~ normal(0, 2)
beta ~ normal(0, 2);
beta ~ normal(0, 2)
y ~ surv_expP (censor, shape1, shape2);
y ~ surv_expP(censor, shape1, shape2)
}
Error: unexpected ‘}’ in “}”
generated quantities{
Error: unexpected symbol in “generated quantities”
real dev;
Error: unexpected symbol in “real dev”
dev=0;
dev=dev + (-2)*surv_expP_lpdf (y|censor,shape1,shape2);
Error in surv_expP_lpdf(y | censor, shape1, shape2) :
could not find function “surv_expP_lpdf”

}
Error: unexpected ‘}’ in “}”
"`

if you can run it, and get my the solu ,
thanks for you

Those quotation marks are not ASCII. If you copy-and-paste, you need to delete them and put in plain quotation marks.

if you want i can send the file to you e mail to run it in your system