Closed fusaroli closed 3 years ago
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...
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
)
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
.
oh yes, I'd tried lpmf, but still same error, so I kept changing it to no avail :-/
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
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)
(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.
picked up kids and now upgrading rstan to the github devel version. I'll see if it helps :-)
ok, using lpmf and upgrading to the latest rstan works. thanks!
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:
The Stan code contains a few deprecated commands, but even fixing those, the error doesn't change. I