Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Survey.stan example from Cognitive Modeling - Expected end of file after end of generated quantities block. #209

Closed
fusaroli opened this issue Sep 1, 2021 · 9 comments

Comments

@fusaroli
Copy link

fusaroli commented Sep 1, 2021

I am trying to run the Survey example from the Bayesian Cognitive Modeling examples.

When I try running it as-is on R 4.1.1. and RStan 2.21.2 (the latest), I get:

Error in stanc(file = file, model_code = model_code, model_name = model_name, :
0

Syntax error in 'string', line 32, column 29 to column 30, parsing error:

Expected end of file after end of generated quantities block.

The Stan code contains a few deprecated commands, but even fixing those, the error doesn't change.
I

@avehtari
Copy link
Contributor

avehtari commented Sep 1, 2021

It took some time to find the code, so here's the link for others https://github.com/stan-dev/example-models/blob/6f0ed2269f0b8a39022ac1202e7622fd644904fe/Bayesian_Cognitive_Modeling/ParameterEstimation/Binomial/Survey.stan

Can you show your modified code? That code really is full of old stuff, so I'm not surprised if it has stopped working. All the examples should be updated to more recent language version...

@fusaroli
Copy link
Author

fusaroli commented Sep 1, 2021

sure, sorry, updated code:

// Inferring Return Rate and Number of Surveys from Observed Returns
data { 
  int<lower=0> nmax;
  int<lower=0> m;
  int<lower=0,upper=nmax> k[m];
}
transformed data {
  int<lower=0> nmin;  // Minimal possible n
  
  nmin = max(k);
}
parameters {
  real<lower=0,upper=1> theta;
}
transformed parameters {
  vector[nmax] lp_parts;  // Log probability for each n

  // First part of the trick for mixture model
  for (n in 1:nmax)
    if (n < nmin)
      lp_parts[n] = log(1.0 / nmax) + negative_infinity();  // Zero probability
    else
      lp_parts[n] = log(1.0 / nmax) + binomial_rng(k, n, theta); 
}
model {
  // Second part of the trick for mixture model
  target += log_sum_exp(lp_parts);
}
generated quantities {
  int<lower=1,upper=nmax> n;
  simplex[nmax] prob_n;
  
  // Transforming lp_parts to probabilities of each n
  prob_n = softmax(lp_parts);
  n = categorical_rng(prob_n);
}
`

and the R code:

`
k <- c(16, 18, 22, 25, 27)
m <- 5
nmax <- 500

myinits <- list(
  list(theta=.5))

# parameters to be monitored:  
parameters <- c("theta", "n")

# Fitting the model
samples <- stan(file=here("6_Survey", "Survey.stan"),   
                data=data, 
                init=myinits,  # If not specified, gives random inits
                pars=parameters,
                iter=4000, 
                chains=1, 
                thin=1,
                # warmup = 100,  # Stands for burn-in; Default = iter/2
                # seed = 123  # Setting seed; Default is random seed
)

@avehtari
Copy link
Contributor

avehtari commented Sep 1, 2021

transformed parameters {
  vector[nmax] lp_parts;  // Log probability for each n

  // First part of the trick for mixture model
  for (n in 1:nmax)
    if (n < nmin)
      lp_parts[n] = log(1.0 / nmax) + negative_infinity();  // Zero probability
    else
      lp_parts[n] = log(1.0 / nmax) + binomial_rng(k, n, theta); 
}

That _rng is not ok in the transformed parameters. You had replaced binomial_log with binomial_rng, but should have replaced it with binomial_lpmf.

@fusaroli
Copy link
Author

fusaroli commented Sep 1, 2021

oh yes, I'd tried lpmf, but still same error, so I kept changing it to no avail :-/

@avehtari
Copy link
Contributor

avehtari commented Sep 1, 2021

If I have

lp_parts[n] = log(1.0 / nmax) + binomial_lpmf(k | n, theta); 

it passes RStudio's Stan syntax check (super handy btw) and compiles with CmdStanR + cmdstan-2.27.0

@avehtari
Copy link
Contributor

avehtari commented Sep 1, 2021

Compiles also with rstan version 2.26.2 (Stan version 2.26.1) (see https://mc-stan.org/r-packages/)
(I don't have now time to downgrade to 2.21.2)

@avehtari
Copy link
Contributor

avehtari commented Sep 1, 2021

(I don't have now time to downgrade to 2.21.2)

Hah, I had CmdStan 2.21.0 lying around and it's super fast to switch version with CmdStanR, so tested with that, too, and it works.

@fusaroli
Copy link
Author

fusaroli commented Sep 1, 2021

picked up kids and now upgrading rstan to the github devel version. I'll see if it helps :-)

@fusaroli
Copy link
Author

fusaroli commented Sep 1, 2021

ok, using lpmf and upgrading to the latest rstan works. thanks!

@fusaroli fusaroli closed this as completed Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants