Problem with using rstan

You need to fix it yourself

ok how can do that ?

I told you, twice. Delete the non-ASCII quotation marks that look like

and replace them with ASCII quotation marks that look like

print("prob is ", prob);

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);
  • print(“prob is”, prob);
    Error: unexpected symbol in:
    "prob=sum(log_lik);
    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: object ‘shape1’ not found

}
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: object ‘shape2’ not found

}
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 “}”
"

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);
  • print(“prob is”, prob);
    Error: unexpected symbol in:
    "prob=sum(log_lik);
    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: object ‘shape1’ not found

}
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: object ‘shape2’ not found

}
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 “}”
"

It keeps changing your quotation marks when you keep pasting. Try this attached file underflow.stan (2.0 KB)
.

same error

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_inf(prob)) {
    
  •   print("prob is", prob);
    

Error: unexpected symbol in:
" if (is_inf(prob)) {
print(“prob”

  // print more stuff

Error: unexpected ‘/’ in " /"

}

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: object ‘shape1’ not found
}
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: object ‘shape2’ not found

}
Error: unexpected ‘}’ in “}”

model {
Error: unexpected ‘{’ in “model {”
//shape1 ~ uniform(0, 10);
Error: unexpected ‘/’ in " /"
beta ~ normal(0, 1);
beta ~ normal(0, 1)
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 “}”

"

Try using the file rather than pasting it is a string.

The same error, i do not know why!!!
Please find my solve for this problem .
thanks for you

I don’t have this problem. It parses fine for me.

you run it using RStudio

Or in a shell.

Ok but your file not run with my RStudio

If you load the attachment to my previous post in the editor window and click on the Check icon, what is the message produced?

Type of file : STAN File (stan)
Opens with: RStudio

Once you have it opened in RStudio, click on the Check icon in the top right of the editor pane.

rstan:::rstudio_stanc(“C:/Users/hp/Downloads/underflow.stan”)
C:/Users/hp/Downloads/underflow.stan is syntactically correct.

Right, so if you call

stan("C:/Users/hp/Downloads/underflow.stan", data = dat)

it will run and print out the value of prob whenever it is not finite. But you are going to have to print out more stuff and do more digging to figure out why.

Same first error that i have before:

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.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”

It didn’t do anything to fix the problem; it just printed out the value of prob when it is not finite.