stan-dev / example-models Goto Github PK
View Code? Open in Web Editor NEWExample models for Stan
Home Page: http://mc-stan.org/
Example models for Stan
Home Page: http://mc-stan.org/
Use the readme as an example.
I'm using rstan_2.4.0, if I leave the model as it is now I get the following error:
data {
int<lower=1> n;
vector[n]
EXPECTED: <identifier> BUT FOUND:
<lower=0> x;
}
I can of course fix it by removing <lower=0> from vector[n]<lower=0> x;
But I was wondering if there's a new way to set a boundary in a vector... Or is this the new way, and it's a problem with my version?
Using log_p_new
instead of the right log_p_news
in the log_sum_exp calc for the "right" way to calculate log expected density.
Add examples that run functions using Torsten (a prototype for pharmacometrics functions in Stan).
@jrnold It appears the "nile.stan" file is missing from the repo "stan-dev/example-models/misc/dlm". Could you upload it or paste the stan code here so that I can replicate your results? thanks.
Warnings and errors. See below
rstan (Version 2.5.0, packaged: 2014-10-22 14:19:22 UTC, GitRev: e52c66f42e81)
to be passed on to Stan
data <- read_rdump("Rate_5.data.R")
myinits <- list(
- list(theta=.5))
parameters to be monitored:
parameters <- c("theta", "postpredk1", "postpredk2")
The following command calls Stan with specific options.
For a detailed description type "?stan".
samples <- stan(file="Rate_5_model.stan",
data=data,
init=myinits, # If not specified, gives random inits
pars=parameters,
iter=10000,
chains=1,
thin=1,
# warmup = 100, # Stands for burn-in; Default = iter/2
# seed = 123 # Setting seed; Default is random seed
- )
TRANSLATING MODEL 'Rate_5_model' FROM Stan CODE TO C++ CODE NOW.
COMPILING THE C++ CODE FOR MODEL 'Rate_5_model' NOW.
In file included from file1cdfd6e9e25.cpp:407:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/rstaninc.hpp:3:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/stan_fit.hpp:732:15: warning: unused variable 'return_code' [-Wunused-variable]
int return_code = stan::common::do_bfgs_optimize(model, lbfgs, base_rng,
^
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/stan_fit.hpp:796:15: warning: unused variable 'return_code' [-Wunused-variable]
int return_code = stan::common::do_bfgs_optimize(model, bfgs, base_rng,
^
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/stan_fit.hpp:616:14: warning: unused variable 'init_log_prob' [-Wunused-variable]
double init_log_prob;
^
In file included from file1cdfd6e9e25.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/model/model_header.hpp:16:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/agrad/rev.hpp:5:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/agrad/rev/chainable.hpp:87:17: warning: 'static' function 'set_zero_all_adjoints' declared in header file should be declared 'static inline' [-Wunneeded-internal-declaration]
static void set_zero_all_adjoints() {
^
In file included from file1cdfd6e9e25.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/model/model_header.hpp:23:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/io/dump.hpp:26:14: warning: function 'product' is not needed and will not be emitted [-Wunneeded-internal-declaration]
size_t product(std::vector<size_t> dims) {
^
5 warnings generated.
SAMPLING FOR MODEL 'Rate_5_model' NOW (CHAIN 1).
Iteration: 1 / 10000 0%
Iteration: 1000 / 10000 10%
Iteration: 2000 / 10000 20%
Iteration: 3000 / 10000 30%
Iteration: 4000 / 10000 40%
Iteration: 5000 / 10000 50%
Iteration: 5001 / 10000 50%
Iteration: 6000 / 10000 60%
Iteration: 7000 / 10000 70%
Iteration: 8000 / 10000 80%
Iteration: 9000 / 10000 90%
Iteration: 10000 / 10000 100%
#0.091544 seconds (Sampling)
#0.179572 seconds (Total)
print(samples)
Inference for Stan model: Rate_5_model.
1 chains, each with iter=10000; warmup=5000; thin=1;
post-warmup draws per chain=5000, total post-warmup draws=5000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
theta 0.50 0.00 0.10 0.31 0.43 0.50 0.57 0.70 1632 1
postpredk1 5.02 0.03 1.88 1.00 4.00 5.00 6.00 9.00 3173 1
postpredk2 4.99 0.03 1.86 1.00 4.00 5.00 6.00 9.00 3026 1
lp__ -15.74 0.01 0.67 -17.65 -15.90 -15.47 -15.30 -15.25 2348 1
Samples were drawn using NUTS(diag_e) at Sat Dec 20 18:23:40 2014.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).
theta <- extract(samples)$theta
postpredk1 <- extract(samples)$postpredk1
postpredk2 <- extract(samples)$postpredk2Two-panel plot.
layout(matrix(c(1,2),1,2))
layout.show(2)First, a histogram for theta.
par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
Nbreaks <- 80font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
y <- hist(theta, Nbreaks, plot=F)
plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="S", lwd=2, lty=1,xlim=c(0,1), ylim=c(0,10), xlab="Theta", ylab="Density")
let's plot a density estimate over this:
lines(density(theta), col="red", lwd=2)Second plot, the data space (predictives)
plot(k1,k2,type="p", pch=4, cex=2, lwd=2, xlab="Success Count 1",
Error in plot(k1, k2, type = "p", pch = 4, cex = 2, lwd = 2, xlab = "Success Count 1", :ylab="Success Count 2", xlim=c(-1, n1+1), ylim=c(-1,n2+1))
error in evaluating the argument 'x' in selecting a method for function 'plot': Error: object 'k1' not found
nsamples <- length(theta)
sc <- 10
for (i in 0:n1)- {
- for (j in 0:n2)
- {
match.preds <- sum(postpredk1==i & postpredk2==j)/nsamples
if (match.preds > 0)
{
points(i,j, pch=0, cex=sc*sqrt(match.preds))
}
- }
- }
Error: object 'n1' not found
It would be very nice if we could auto-generate a doc like the BUGS example model doc, which explains for each model where it came from, what the data and parameters are. It even shows what fits look like in some cases.
For this issue,
Would it make sense to put this all in
Hello.
I've found by chance that some links on the 'BUGS Examples Sorted by Volume' section don't work.
https://github.com/stan-dev/example-models/blob/master/bugs_examples/vol3/funshapes/hsquare.stan.0
Maybe more
This was spotted on the users list by Lukio Olweny.
bugs_examples vol1 dogs doesn't run. I've verified that it results in:
Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error : "
This should be fixed. It used to run a long time ago.
Running example models gives warning
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Since users are likely to copy and paste example models, "<-" should be switched to "=" in all files.
the folders knitr/pool-binary-trials and knitr/pool-repeated-trials look as though they are identical except that only pool-binary-trials has pool-no-pool.html and only pool-repeated-trials has fit.R.
It looks like the use of poisson_log is incorrect, because the expression used (see below) is for the Poisson rate not the Poisson log rate.
Y[i, j] * exp(beta * Z[i]) * dL0[j] # this is Poisson rate, but used as log rate
In non-deprecated Stan code, the revised code would be:
target += poisson_lpmf(dN[i, j] | Y[i, j] * exp(beta * Z[i]) * dL0[j]);
Or perhaps a sampling statement is cleaner:
dN[i, j] ~ poisson(Y[i, j] * exp(beta * Z[i]) * dL0[j]);
intervals shown in display equation for delta and gamma are swapped.
See comment on Discourse here from ermeel:
http://discourse.mc-stan.org/t/lotka-volterra-population-dynamics-case-study/2457/20
some example (eg https://github.com/stan-dev/example-models/blob/master/misc/moving-avg/arma11.stan) still have <- as an assignment, please make sure example conform to latest version
This file
example-models/master/bugs_examples/vol1/rats/rats.stan
needs to be updated to replace the inv_gamma(0.001, 0.001) priors.
The pilot model is in example_models/ARM/Ch.14, but it's an example from Chapter 13.
How to find and install the custom functions described in Appendix C2 i.e. display(), sim(), se.coef() and sigma.hat()?
Thank you for your help.
Drew Yallop
Update the Gaussian process model code in the example models repo to mirror the GP chapter in the manual.
v2.16.0
(originally stan-dev/stan#517)
From Bob:
It's not implemented now because it requires marginalizing out 5 binary discrete parameters which interact. Not impossible, but a pain. For the model, see:
http://www.openbugs.net/Examples/Asia.html
It would go in src/models/bugs_examples/vol2/asia.
hey @bob-carpenter , @lizzyagibson and I have been looking at the lda example code (its nice how it closely maps to the generative description in the LDA journal paper), and theres a few deprecation warnings related to <-
along with the target update increment_log_sum_exp
expression. you may wanna update them :)
thanks for the lovely examples!
ARM/Ch.21/finite_populations.stan
ARM/Ch.21/multiple_comparison.stan
ARM/Ch.21/r_sqr.stan
ARM/Ch.23/electric_1a.stan
ARM/Ch.23/educational_subsidy.stan
bugs_examples/vol2/pines/pines-3.stan
bugs_examples/vol3/fire/fire.stan
error messages:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
variable "a_0" does not exist.
error in 'examples/example-models/ARM/Ch.21/finite_populations.stan' at line 15, column 14
-------------------------------------------------
13:
14: //finite population sd for when group level predictors are present
15: g_hat = a_0 + a_1 * u_1 + a_2 * u;
^
16: }
-------------------------------------------------
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
variable "j" does not exist.
error in 'examples/example-models/ARM/Ch.21/r_sqr.stan' at line 11, column 11
-------------------------------------------------
9: vector[n_county] a;
10: vector[n_county] b;
11: matrix[j,2] B;
^
12: matrix[j,2] B_hat;
-------------------------------------------------
make: *** [examples/example-models/ARM/Ch.21/r_sqr.hpp] Error 253
make: *** Waiting for unfinished jobs....
make: *** [examples/example-models/ARM/Ch.21/finite_populations.hpp] Error 253
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
variable "theta" does not exist.
error in 'examples/example-models/ARM/Ch.21/multiple_comparison.stan' at line 12, column 8
-------------------------------------------------
10: model {
11: mu_theta ~ normal(0, 1);
12: theta ~ normal(100 * mu_theta, sigma_theta);
^
13: y ~ normal(theta, sigma_y);
-------------------------------------------------
make: *** [examples/example-models/ARM/Ch.21/multiple_comparison.hpp] Error 253
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
error in 'examples/example-models/bugs_examples/vol3/fire/fire.stan' at line 49, column 26
-------------------------------------------------
47: for (i in 1:N) {
48: if (theta > x[i])
49: lpa[i] <- ( log_tmp,
^
50: + neg_log1p_temp
-------------------------------------------------
PARSER EXPECTED: ")"
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
variable "g_0" does not exist.
error in 'examples/example-models/ARM/Ch.23/educational_subsidy.stan' at line 26, column 19
-------------------------------------------------
24: + beta[3] * poor[i] + beta[4] * male[i] + beta[5] * age97[i];
25: for (j in 1:J)
26: a_hat[j] = g_0 + g_1 * program[j];
^
27: }
-------------------------------------------------
make: *** [examples/example-models/bugs_examples/vol3/fire/fire.hpp] Error 253
make: *** [examples/example-models/ARM/Ch.23/educational_subsidy.hpp] Error 253
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning: Deprecated function 'normal_log'; please replace suffix '_log' with '_lpdf' for density functions or '_lpmf' for mass functions
Warning: Deprecated function 'normal_log'; please replace suffix '_log' with '_lpdf' for density functions or '_lpmf' for mass functions
Warning: Deprecated function 'normal_log'; please replace suffix '_log' with '_lpdf' for density functions or '_lpmf' for mass functions
Warning: Deprecated function 'gamma_log'; please replace suffix '_log' with '_lpdf' for density functions or '_lpmf' for mass functions
Warning: Deprecated function 'normal_log'; please replace suffix '_log' with '_lpdf' for density functions or '_lpmf' for mass functions
Warning: Deprecated function 'normal_log'; please replace suffix '_log' with '_lpdf' for density functions or '_lpmf' for mass functions
Warning: Deprecated function 'normal_log'; please replace suffix '_log' with '_lpdf' for density functions or '_lpmf' for mass functions
Warning: Deprecated function 'gamma_log'; please replace suffix '_log' with '_lpdf' for density functions or '_lpmf' for mass functions
error in 'examples/example-models/bugs_examples/vol2/pines/pines-3.stan' at line 49, column 17
-------------------------------------------------
47: + normal_log(beta, 0, sqrt(1e4))
48: + gamma_log(tau[1], 0.0001, 0.0001)
49: + normal_log(gamma,
^
50:
-------------------------------------------------
PARSER EXPECTED: "("
make: *** [examples/example-models/bugs_examples/vol2/pines/pines-3.hpp] Error 253
On page
https://github.com/stan-dev/example-models/wiki/BUGS-Examples-Sorted-Alphabetically
under "Stacks: "robust regression"" the link to "Stan code" is
https://github.com/stan-dev/example-models/blob/master/bugs_examples/vol1/stacks/stacks.stan
which gives 404.
Unlike other examples, Stacks has 6 different Stan code files (with different residual models) and none of them is named stacks.stan. To fix this there should be 6 links to these different models, or just a link to the corresponding "Stan files" folder.
See subject. Vol1 BUGS.
Recently, I have been studying the Stan example of Magnesium meta-analysis().
It works well as a whole. However, when I tried to use shrinkage uniform prior only, it didn't work.
Here is the code that I have rewritten according to the Magnesium meta-analysis example.
m.mgo <- 'data {
int N_studies;
int rt[N_studies];
int nt[N_studies];
int rc[N_studies];
int nc[N_studies];
}
transformed data {
real<lower=0> s0_sq;
s0_sq <- 0.1272041;
}
parameters {
real<lower=-10,upper=10> mu;
real theta[N_studies];
real<lower=0,upper=1> pc[N_studies];
real<lower=0,upper=1> B0;
}
transformed parameters {
real<lower=0> tau;
tau <- sqrt(s0_sq * (1-B0) / B0);
}
model {
// Prior 4: Uniform shrinkage on tau.sqrd
B0 ~ uniform(0,1);
vector[N_studies] tmpm;
mu ~ uniform(-10, 10);
pc ~ uniform(0,1);
theta ~ normal(mu, tau);
for (i in 1:N_studies){
tmpm[i] <- theta[i] + logit(pc[i]);
rc ~ binomial(nc, pc);
rt ~ binomial_logit(nt, tmpm);
}
}
mgo <- stan(model_code = m.mgo,data=mgmdt,iter = 3000, cores=1,chains = 2,warmup =500)
This is the warning I have got!
I try to change the position of vector[N_studies] tmpm;
to the first line of the model block.
It still didn't work
This is the warning.
[383] "Rejecting initial value:"
[384] " Error evaluating the log probability at the initial value."
[385] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"
[386] "Exception thrown at line 23: normal_log: Random variable is nan, but must not be nan!"
[387] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"
[388] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."
[389] "Rejecting initial value:"
[390] " Error evaluating the log probability at the initial value."
Can anyone tell me what the problem this is and how to write it correctly?
I will highly appreciate your help.
[edited to escape code]
From @jonwake originally on stan-dev/stan issue tracker: stan-dev/stan#1461
I'm running the survey polls examples from the above chapter.
The samplers are working without problem, but creating the figures is creating errors.
Specifically, running the code here:
https://github.com/stan-dev/example-models/blob/master/ARM/Ch.14/14.1_State-LevelOpinionsFromNationalPolls.R
and running the code in lines142-170 (to create Fig 14.2 of the book) gives the errors:
Error in a.state[4000 - 1, j] : subscript out of bounds
....
Error in a.state[4000 - 18, j] : subscript out of bounds
Error in a.state[4000 - 19, j] : subscript out of bounds
Error in a.state[4000 - 20, j] : subscript out of bounds
Warning message:
In loop_apply(n, do.ply) :
Removed 1 rows containing missing values (geom_point).
Best
Jon
As discussed in the Stan meeting on Tuesday, we're going to create a whitelist of 20 models that will serve as example models for rstan users that we'd actually like users to learn from. The whitelisted models will satisfy the following criteria.:
Now on a blog.
See: #122
The model at
ARM/Ch.19/item_response.stan
is a direct translation of ARM's improper posterior approach, and it won't work in Stan.
or just remove them and wait for the case studies part of the web site and drop them there
I'm going to start with
Hello:
I very much want to start using RSTAN for all of my analyses and have thus far figured out how to implement very basic models. My field commonly uses ANOVA, however, where there are interactions between categorical variables. I was hoping someone could share with me code for a factorial ANOVA (3 x 2, for example), with generated quantities for all means and also estimates for the standard deviations. I have attempted to translate Kruschke's ANOVA, which was implemented in RJAGS, to RSTAN but have run into difficulties.
Gertjan Verhoeven pointed out a bug in exercise 5. That was based on exercise 5.9(b) of BDA 3 (p. 136). But the phrasing was botched int he text and exercise. It should be as follows.
BDA 3, Problem 5.9(b):
Assume p(alpha / (alpha + beta), alpha + beta)
is uniform and show that
p(alpha, beta) propto (alpha + beta)^{-5/2}.
First note that alpha, beta > 0
.
Step 1. Let
f(kappa) = kappa^{-0.5} = gamma
so that
f^{-1}(gamma) = gamma^{-2} = kappa
and assume a uniform so that that p_G(gamma) = const
. Then with the usual change of variables, we get
p_K(kappa) = p_G(f^{-1}(kappa)) | d/d.kappa f^{-1}(kappa) |
= const | d/d.kappa kappa^{-0.5} |
propto kappa^{-1.5}
Step 2. Compute the Jacobian of the inverse transform h
, defined by
h(alpha, beta) = (alpha / (alpha + beta), (alpha + beta))
the Jacobian of which is
J_h
= | d/d.alpha (alpha / (alpha + beta)) d/d.alpha (alpha + beta) |
| d/d.beta (alpha / (alpha + beta)) d/d.beta (alpha + beta) |
= | beta / (alpha + beta)^2 1 |
| - alpha / (alpha + beta)^2 1 |
which using the rule for determinants for 2 x 2 matrices, provide an absolute Jacobian determinant of:
|J_h| = (alpha + beta)^{-1}
Step 3. Multiply the distribution from Steps 1 and 2 by the absolute Jacobian determinant to adjust for change of variables, and the final result is
p(alpha, beta)
= (alpha + beta)^{-3/2} * (alpha + beta)^{-1}
= (alpha + beta)^{-5/2}
Or you could just put the two bits together and derive the compound Jacobian, but it seems clearer this way in two steps.
fix text motivation for prior
fix exercise and give a big hint
Running the example from the documentation gets:
rats_fit <- stan(file='rats.stan', verbose=FALSE);
COMPILING THE C++ CODE FOR MODEL 'rats' NOW.
Error : mismatch in dimension declared and found in context; processing stage=data initialization; variable name=x; position=0; dims declared=(1); dims found=(5)
failed to create the sampler; sampling not done
Possibly related to #25?
In example-models/master/misc/hmm/hmm-semisup.stan, there's an uppercase K rather than a lowercase one when best_logp is initialized. I'm hoping to fit a 2-category HMM that chooses between Gaussian generalized linear models.
--- hmm-semisup.stan 2015-08-14 00:18:17.000000000 -0400
+++ hmm-semisup.stan2 2015-08-14 00:18:04.000000000 -0400
@@ -48,7 +48,7 @@
real best_logp[T_unsup,K];
real best_total_logp;
for (k in 1:K)
best_logp[1,K] <- log(phi[k,u[1]]);
best_logp[1,k] <- log(phi[k,u[1]]);
Does not plot. Console ...
rm(list=ls())
library(rstan)
Loading required package: Rcpp
Loading required package: inline
Attaching package: ‘inline’
The following object is masked from ‘package:Rcpp’:
registerPlugin
rstan (Version 2.5.0, packaged: 2014-10-22 14:19:22 UTC, GitRev: e52c66f42e81)
to be passed on to Stan
data <- read_rdump("Survey.data.R")
myinits <- list(
- list(theta=.5))
parameters to be monitored:
parameters <- c("theta", "n")
The following command calls Stan with specific options.
For a detailed description type "?stan".
samples <- stan(file="Survey_model.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
- )
TRANSLATING MODEL 'Survey_model' FROM Stan CODE TO C++ CODE NOW.
COMPILING THE C++ CODE FOR MODEL 'Survey_model' NOW.
In file included from file29b37e27a154.cpp:466:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/rstaninc.hpp:3:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/stan_fit.hpp:732:15: warning: unused variable 'return_code' [-Wunused-variable]
int return_code = stan::common::do_bfgs_optimize(model, lbfgs, base_rng,
^
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/stan_fit.hpp:796:15: warning: unused variable 'return_code' [-Wunused-variable]
int return_code = stan::common::do_bfgs_optimize(model, bfgs, base_rng,
^
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/stan_fit.hpp:616:14: warning: unused variable 'init_log_prob' [-Wunused-variable]
double init_log_prob;
^
In file included from file29b37e27a154.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/model/model_header.hpp:16:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/agrad/rev.hpp:5:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/agrad/rev/chainable.hpp:87:17: warning: 'static' function 'set_zero_all_adjoints' declared in header file should be declared 'static inline' [-Wunneeded-internal-declaration]
static void set_zero_all_adjoints() {
^
In file included from file29b37e27a154.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/model/model_header.hpp:23:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/io/dump.hpp:26:14: warning: function 'product' is not needed and will not be emitted [-Wunneeded-internal-declaration]
size_t product(std::vector<size_t> dims) {
^
5 warnings generated.
SAMPLING FOR MODEL 'Survey_model' NOW (CHAIN 1).
Iteration: 1 / 4000 0%
Iteration: 400 / 4000 10%
Iteration: 800 / 4000 20%
Iteration: 1200 / 4000 30%
Iteration: 1600 / 4000 40%
Iteration: 2000 / 4000 50%
Iteration: 2001 / 4000 50%
Iteration: 2400 / 4000 60%
Iteration: 2800 / 4000 70%
Iteration: 3200 / 4000 80%
Iteration: 3600 / 4000 90%
Iteration: 4000 / 4000 100%
#15.1188 seconds (Sampling)
#28.6422 seconds (Total)
Now the values for the monitored parameters are in the "samples" object,
ready for inspection.
theta <- extract(samples)$theta
n <- extract(samples)$nFirst calculate MLE:
cc <- -Inf
ind <- 0for (i in 1:length(n)) {
logL <- 0
for(j in 1:m) {
logL <- logL+lgamma(n[i]+1)-lgamma(k[j]+1)-lgamma(n[i]-k[j]+1)
logL <- logL+k[j]_log(theta[i])+(n[i]-k[j])_log(1-theta[i])
}
if (logL>cc) {
ind <- i
cc <- logL
}
- }
Error: object 'm' not foundend MLE
################Plots
layout(matrix(c(2,0,1,3),2,2,byrow=T), width=c(2/3, 1/3), heights=c(1/3, 2/3))
xhist <- hist(n, plot=F)
yhist <- hist(theta, plot=F)
top <- max(c(xhist$counts, yhist$counts))
xrange <- c(0, nmax)
Error: object 'nmax' not found
yrange <- c(0, 1)par(mar=c(5,5,1,1))
plot(n,theta,xlim=xrange, ylim=yrange,ylab="", xlab="")
Error in plot.default(n, theta, xlim = xrange, ylim = yrange, ylab = "", :
object 'xrange' not found
axis(1)
Error in axis(1) : plot.new has not been called yet
mtext("Number of Surveys", side=1,line=2.25, cex=1.2)
Error in mtext("Number of Surveys", side = 1, line = 2.25, cex = 1.2) :
plot.new has not been called yet
axis(2, cex=1.2)
Error in axis(2, cex = 1.2) : plot.new has not been called yet
las=0
mtext("Rate of Return", side=2 ,line=2.25, cex=1.2)
Error in mtext("Rate of Return", side = 2, line = 2.25, cex = 1.2) :
plot.new has not been called yet
las=1
points(mean(n),mean(theta), col="red", lwd=3, pch=4) #expectation
Error in plot.xy(xy.coords(x, y), type = type, ...) :
plot.new has not been called yet
points(n[ind],theta[ind], col="green", lwd=3, pch=10) #Maximum Likelihood
Error in plot.xy(xy.coords(x, y), type = type, ...) :
plot.new has not been called yetpar(mar=c(0,4,1,1))
barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0,col="lightblue")par(mar=c(4,0,1,3))
barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE,
col="lightblue")
I noticed that the version of the soil model at the mc-stan Case Studies page shows some inconsistencies:
For the measurement error model, the stan call and the output show 2 chains with 100 samples each (50 warmup, 50 post-warmup) but the output from print(fit_me)
says that the sampling was done with 4 chains and 500 post-warmup samples each.
More seriously, the traceplots show very poor mixing, but print(fit_me)
shows Rhat <= 1.01, which is inconsistent with the traceplots.
So I cloned it from github and ran it, and indeed, I see lots of divergences in the pairs plot and most of the Rhat numbers for fit_me are >> 1 (many > 10).
If I increase adapt_delta to 0.95, decrease stepsize to 0.002, and allow 1000 warmup iterations before the 200 sampling iterations, the Rhat numbers reduce significantly (<= 1.4 for the first model and <= 1.1 for the second) but there are still over 30 divergent transitions in both models, and the traceplots don't look like they're mixing very well.
The pairs plot for the first model looks like there is some serious funnel action happening, most obviously between k1 and alpha21, but also between other pairs of parameters.
Maybe this is well known to you. I understand that the example-models repository is a work in progress, so please don't think I'm complaining. Just wanting to let you know in case you hadn't realized this.
> sessionInfo()
R version 3.2.4 (2016-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] SoilR_1.1-23 RUnit_0.4.31 deSolve_1.13 rstan_2.9.0-3 ggplot2_2.1.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.4 grid_3.2.4 plyr_1.8.3 gtable_0.2.0 stats4_3.2.4
[6] scales_0.4.0 tools_3.2.4 munsell_0.4.3 rsconnect_0.4.2.1 inline_0.3.14
[11] colorspace_1.2-6 gridExtra_2.2.1
@wds15 mentioned in an email on stan-dev that
https://github.com/stan-dev/example-models/blob/master/misc/eight_schools/eight_schools.stan
uses centered parameterization and gets lots of divergences.
We should fix the parameterization so that it works!
Provide a working PK model library which supports
This is work in progress, not sure if we are going to add this to the example models eventually. The Torston library provides an alternative approach.
Missing pieces:
integrate_ode_rk45
Hello, I'm trying to implement the stan code for CJS models explained in the example above (title). But I have some doubts about the parameters estimated and the code itself. In my case, all the individuals are marked as chicks, it means, I know the exact age in each time t. When you run the model and get the parameters, you obtain p[1], but, as I have chicks, I know for sure that p[1] is always 1, because it is the ringing time. Besides, I don't understand why do we estimate p[1] because the recapture probability begins in t+1, and then, we only will have (in my case with 11 encounter occasions), 10 phi's and 10 p's, but we obtain 11 p. And I don't understand why.
In the other hand, I also don't understand why we put in the code:
transformed data {
int<lower=0,upper=K+1> first[I]; // first[i]: ind i first capture
int<lower=0,upper=K+1> last[I]; // last[i]: ind i last capture
Because, the last ringing year (K) don't compute for the calculation of the probabilities of interest, and then I guess the first capture should be from 1 to K-1, and the last only until K.
And the last question is also related with the block of transformed parameters:
first <- rep_array(K+1,I);
last <- rep_array(0,I);
Here, I don't understand the meaning of K+1 again, and the zeros for the last recapture. Just for concluding, I have also problems with NAs in the capture-recapture matrix with STAN. They are marking when the individual IS NOT in the study, but it doesn't work with STAN as I have the code (the same of the example).
I'm beginning with STAN and maybe some questions are trivial, I would appreciate some help.
Stan 2.17.1+ returns warnings:
Warning (non-fatal): Comments beginning with # are deprecated. Please use // in place of # for line comments.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): increment_log_prob(...); is deprecated and will be removed in the future.
Warning (non-fatal): the if_else() function is deprecated. Use the conditional operator '?:' instead.
Warning: Deprecated function 'cauchy_log'; please replace suffix '_log' with '_lpdf' for density functions or '_lpmf' for mass functions
....
#
to //
<-
to =
increment_log_prob(
to target +=
if_else()
to ?:
|
instead of comma after initial argAdditional best practice: use compound declare/define whenever possible
I have found that Rate_4_Stan.R does not plot. I have run through RStudio and native R. A copy of the console result is below. Hardware details are:
OS X Yosemite 10.10.1
Hardware Overview:
Model Name: MacBook Air
Model Identifier: MacBookAir4,2
Processor Name: Intel Core i5
Processor Speed: 1.7 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 3 MB
Memory: 4 GB
Boot ROM Version: MBA41.0077.B11
SMC Version (system): 1.73f66
Serial Number (system): C02GCAQPDJWT
Hardware UUID: 8B76916C-7101-57E6-B26E-EED12F76D2B
Thank you for your help,
Drew Yallop
R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Workspace loaded from ~/STAN Projects/BCM 4 Prior and Posterior Prediction/.RData]
clears workspace:
rm(list=ls())
library(rstan)
Loading required package: Rcpp
Loading required package: inline
Attaching package: ‘inline’
The following object is masked from ‘package:Rcpp’:
registerPlugin
rstan (Version 2.5.0, packaged: 2014-10-22 14:19:22 UTC, GitRev: e52c66f42e81)
to be passed on to Stan
data <- read_rdump("Rate_4a.data.R")
Uncomment for Trompetter Data
data <- read_rdump("Rate_4b.data.R")
k <- 1
n <- 15
Uncomment for Trompetter Data
k <- 24
n <- 121
myinits <- list(
- list(theta=.5, thetaprior=.5))
parameters to be monitored:
parameters <- c("theta", "thetaprior", "postpredk", "priorpredk")
The following command calls Stan with specific options.
For a detailed description type "?stan".
samples <- stan(file="Rate_4_model.stan",
data=data,
init=myinits, # If not specified, gives random inits
pars=parameters,
iter=20000,
chains=1,
thin=1,
# warmup = 100, # Stands for burn-in; Default = iter/2
# seed = 123 # Setting seed; Default is random seed
- )
TRANSLATING MODEL 'Rate_4_model' FROM Stan CODE TO C++ CODE NOW.
COMPILING THE C++ CODE FOR MODEL 'Rate_4_model' NOW.
In file included from file13b85652cbf5.cpp:413:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/rstaninc.hpp:3:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/stan_fit.hpp:732:15: warning: unused variable 'return_code' [-Wunused-variable]
int return_code = stan::common::do_bfgs_optimize(model, lbfgs, base_rng,
^
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/stan_fit.hpp:796:15: warning: unused variable 'return_code' [-Wunused-variable]
int return_code = stan::common::do_bfgs_optimize(model, bfgs, base_rng,
^
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include/rstan/stan_fit.hpp:616:14: warning: unused variable 'init_log_prob' [-Wunused-variable]
double init_log_prob;
^
In file included from file13b85652cbf5.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/model/model_header.hpp:16:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/agrad/rev.hpp:5:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/agrad/rev/chainable.hpp:87:17: warning: 'static' function 'set_zero_all_adjoints' declared in header file should be declared 'static inline' [-Wunneeded-internal-declaration]
static void set_zero_all_adjoints() {
^
In file included from file13b85652cbf5.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/model/model_header.hpp:23:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/io/dump.hpp:26:14: warning: function 'product' is not needed and will not be emitted [-Wunneeded-internal-declaration]
size_t product(std::vector<size_t> dims) {
^
5 warnings generated.
SAMPLING FOR MODEL 'Rate_4_model' NOW (CHAIN 1).
Iteration: 1 / 20000 0%
Iteration: 2000 / 20000 10%
Iteration: 4000 / 20000 20%
Iteration: 6000 / 20000 30%
Iteration: 8000 / 20000 40%
Iteration: 10000 / 20000 50%
Iteration: 10001 / 20000 50%
Iteration: 12000 / 20000 60%
Iteration: 14000 / 20000 70%
Iteration: 16000 / 20000 80%
Iteration: 18000 / 20000 90%
Iteration: 20000 / 20000 100%
#0.218085 seconds (Sampling)
#0.420275 seconds (Total)
Now the values for the monitored parameters are in the "samples" object,
ready for inspection.
print(samples)
Inference for Stan model: Rate_4_model.
1 chains, each with iter=20000; warmup=10000; thin=1;
post-warmup draws per chain=10000, total post-warmup draws=10000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
theta 0.12 0.00 0.07 0.02 0.06 0.10 0.16 0.30 5346 1
thetaprior 0.49 0.00 0.29 0.02 0.24 0.49 0.75 0.97 6044 1
postpredk 1.76 0.02 1.64 0.00 0.00 1.00 3.00 6.00 6880 1
priorpredk 7.40 0.06 4.60 0.00 3.00 7.00 11.00 15.00 6492 1
lp__ -8.67 0.02 1.08 -11.51 -9.11 -8.35 -7.88 -7.58 2525 1
Samples were drawn using NUTS(diag_e) at Fri Dec 19 17:44:52 2014.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).
################Plots
theta <- extract(samples)$theta
thetaprior <- extract(samples)$thetaprior
priorpredk <- extract(samples)$priorpredk
postpredk <- extract(samples)$postpredklayout(matrix(c(1,2),2,1))
layout.show(2)Prior and posterior of theta
plot(density(theta, from=0, to=1), zero.line=F, axes=F, main="", xlab="",
axis(1, at=c(0,0.2,0.4,0.6,0.8,1), lab=c("0","0.2","0.4","0.6","0.8","1"),ylab="", xlim=c(0,1), ylim=c(0,6))
mtext("Rate", side=1, line=2.25, cex=1.2)cex.axis=0.8)
axis(2, at=c(0,2,4,6),cex.axis=0.8)
mtext("Density", side=2, line=2.25, cex=1.2)
lines(density(thetaprior, from=0, to=1), lty=3, col="gray")
legend(0.6,5.75, c("Prior", "Posterior"), lty=c(3,1), col=c ("grey", "black"))Prior and posterior predictive
mybreaks <- seq(from=-.5,to=n+1,by=1)
Error in seq.default(from = -0.5, to = n + 1, by = 1) :
object 'n' not found
my.at <- seq(from=0,to=n,by=1)
Error in seq.default(from = 0, to = n, by = 1) : object 'n' not found
hist(postpredk,breaks=mybreaks,freq=F, right=F, ylab="", xlab="", ylim=c(0,0.3),
Error in hist.default(postpredk, breaks = mybreaks, freq = F, right = F, :main="", axes=F )
object 'mybreaks' not found
axis(1, at=my.at,lab=my.at,cex.axis=0.8)
Error in axis(1, at = my.at, lab = my.at, cex.axis = 0.8) :
object 'my.at' not found
mtext("Success Count", side=1, line=2.25, cex=1.2)
axis(2,at=c(0,0.1,0.2,0.3),lab=c("0","0.1","0.2","0.3"),cex.axis=0.8)
mtext("Mass", side=2, line=2.25, cex=1.2)
hist(priorpredk, breaks=mybreaks,freq=F,right=F,add=T, lty=3,border="grey")
Error in hist.default(priorpredk, breaks = mybreaks, freq = F, right = F, :
object 'mybreaks' not found
legend(8,0.3, c("Prior", "Posterior"), lty=c(3,1),col=c("grey", "black"))
ARM/Ch.21/finite_populations.stan
ARM/Ch.21/multiple_comparison.stan
ARM/Ch.21/r_sqr.stan
ARM/Ch.23/educational_subsidy.stan
bugs_examples/vol2/pines/fit.stan
bugs_examples/vol2/pines/pines-3.stan
bugs_examples/vol3/fire/fire.stan
In the generated quantities, the initial state likelihood is only set for the state with the highest index. I believe is this is a typo wherein a lower case k should have been used on the left side of line 51. Lines 50-51 in hmm-semisup.stan:
for (k in 1:K)
best_logp[1,K] <- log(phi[k,u[1]]);
The same typo appears in the manual.
most ARM model files have corresponding data.R files, but not all -
e.g. model ARM/Ch.3/kidiq_validation.stan
has 2 data files:
kids_before1987.data.R
kids_after1987.data.R
these should be renamed so that initial name matches model name, e.g.
kidiq_validation_pre1987.data.R
, etc.
Hello,
I asked over on the Pystan group about submitting juypter notebook example models using Pystan. I was directed to over here. I am in the process of moving our models into Pystan so this is a learning process for me.
I created a jupyter notebook here:
https://github.com/bioinfonm/bioinfonm.github.io/blob/master/_posts/pystan_musings_part1_img/pystan_three_centirues_english_grain_data.ipynb
The notebook, raw data, and images are all:
https://github.com/bioinfonm/bioinfonm.github.io/tree/master/_posts/pystan_musings_part1_img
I was wondering what was the best way to get this vetted and then hosted here as an example for PyStan.
Thank you for the time and consideration,
Ara
There are 5 case studies in the book "Bayesian Statistics and Marketing" by Peter Rossi and Robert McCulloch. However, the accompanied R code uses "bayesm" package and it is difficult to understand and interpret. Stan code for these models will be really helpful.
(I'm a Stan newbie, but I have played with occupancy models a lot, including the WinBUGS and JAGS code in BPA.)
These are occupancy studies, and occ_fs
is the number of sites in the sample which are occupied. As such, it cannot be less than the number of sites observed to be occupied (occ_obs
) or greater than the total number of sites in the sample (R
). The example code allows these constraints to be violated; it's just (bad!) luck that this doesn't happen with the example data from BPA.
A key idea is the conditional probability of occupancy, psi_con
, conditional on the data. So if the species is not detected, we know the site is in the yellow area in the diagram below.
site_occ.stan
The simple model has one value for psi
and one for p
, so all sites with no detections have the same value for psi_con
, called psi_nd
in the code below:
generated quantities {
int<lower=occ_obs, upper=R> occ_fs;
real psi_nd; // prob present | not detected
psi_nd = (psi * (1 - p)^T) / (psi * (1 - p)^T + (1 - psi));
occ_fs = occ_obs + binomial_rng(R - occ_obs, psi_nd);
}
site_occ_cov.stan
Here each site has a different psi
and p
, and a good strategy is to calculate psi_con
for each, which is in any case a result you may want to monitor.
generated quantities {
int occ_fs; // Number of occupied sites
real psi_con[R]; // prob occupied | data
int z[R]; // occupancy indicator, 0/1
for (i in 1:R) {
if (sum_y[i] == 0) { // species not detected
real psi = inv_logit(logit_psi[i]);
vector[T] q = inv_logit(-logit_p[i])'; // q = 1 - p
real qT = prod(q[]);
psi_con[i] = (psi * qT) / (psi * qT + (1 - psi));
z[i] = bernoulli_rng(psi_con[i]);
} else { // species detected at least once
psi_con[i] = 1;
z[i] = 1;
}
}
occ_fs = sum(z);
}
bluebug.stan
The same strategy can be applied here, this time allowing for the variable number of visits to each site. So we replace
vector[T] q = inv_logit(-logit_p[i])';
with
vector[last[i]] q = inv_logit(-logit_p[i, 1:last[i]])';
I haven't looked closely at the dynamic models, but it appears that the reported z
and n_occ
values are also based on unconditional psi
values. Of more concern are the closed-capture models in Ch.6, where the analogous value, the population size N
, appears to have the same problem; this has a smaller numerical impact, as the denominator of the conditional inclusion is always large (ie, close to 1), but should be fixed.
Enforce a common structure to the examples.
Use travis-ci to run models with a ___
in each directory.
___
is to be defined.
We need some PK/PD examples:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.