GithubHelp home page GithubHelp logo

stan-dev / example-models Goto Github PK

View Code? Open in Web Editor NEW
762.0 99.0 478.0 239.11 MB

Example models for Stan

Home Page: http://mc-stan.org/

R 27.17% Makefile 0.05% Stan 1.40% TeX 0.96% CSS 0.01% HTML 70.02% Shell 0.01% Jupyter Notebook 0.37% Rez 0.02%

example-models's Introduction

Example Models

This repository holds open source Stan models, data simulators, and real data. There are models translating those found in books, most of the BUGS examples, and some basic examples used in the manual.

Books

BUGS examples

Basics

  • Basic Distributions
  • Basic Estimators

Organization

Each example model should be organized to include the following

  1. Stan program(s) implementing the model (and variants),
  2. program to simulate data (in R or Python),
  3. simulated data file itself (for now in .data.R dump format). For each Stan file(s), such as foo.stan and bar.stan, there must be a foo.data.R file and a bar.data.R file in the same subdirectory as the .stan file (even if foo.data.R is the same as bar.data.R).
  4. summary of output fit for simulated data file (text), (a) check diagnostics for post-warmup iterations: n_divergent is zero for all post-warmup iterations, tree_depth < max_depth for all post-warmup iterations, (b) check for all parameters (and unnormalized density lp__) that R_hat < 1.1 and n_eff > 20, and (c) if diagnostics fail improve model with a note explaining the fix.
  5. any real data sets with summary of fit (in original and Stan-readable format, with munging program from original to Stan-readable), (a) check diagnostics (as above), (b) check convergence (as above), and (c) if fit is poor then improve model with a note explaining the fix.
  6. citations to source for model and/or data,
  7. documentation needed to understand the model (LaTeX, text, or HTML), including discussion of parameterizations and/or optimizations and their relations to performance (and to each other if there are multiple models),
  8. keywords or other tags to help organize by category (e.g., from manual, from BUGS volume, from book, involving logistic regression, application area such as population model, model type such as IRT or mark-recapture, etc.)
  9. author/copyright-holder info and open-source license info if not new BSD.

The idea is to encourage people to go through all of these steps for their models, particularly 3 and 4, which often get overlooked. And if the example cannot be executed via rstan::stan_demo(), then something is wrong.

Licensing

All of the example models are copyrighted by their author(s) or assignees under the new BSD license unless another open-source license is explicitly stipulated in the directory containing the model.

example-models's People

Contributors

alecksphillips avatar andrewgelman avatar avehtari avatar bbbales2 avatar bgoodri avatar bob-carpenter avatar charlesm93 avatar danielcfurr avatar dmenne avatar dpsimpson avatar education-stan avatar ericnovik avatar ito4303 avatar jgabry avatar joonho112 avatar jrnold avatar martin-smira avatar matthewdhoffman avatar maverickg avatar mitzimorris avatar peterli2016 avatar philclemson avatar raerickson avatar randommm avatar rok-cesnovar avatar rtrangucci avatar seantalts avatar singmann avatar syclik avatar wardbrian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

example-models's Issues

bugs_examples vol1 dogs doesn't run.

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.

https://github.com/stan-dev/example-models/blob/master/Bayesian_Cognitive_Modeling/ParameterEstimation/Binomial/Rate_5_Stan.R

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%

Elapsed Time: 0.088028 seconds (Warm-up)

#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)$postpredk2

Two-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,

  • font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
    
    Nbreaks <- 80
    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",

  •  ylab="Success Count 2", xlim=c(-1, n1+1), ylim=c(-1,n2+1))  
    
    Error in plot(k1, k2, type = "p", pch = 4, cex = 2, lwd = 2, xlab = "Success Count 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

Fix exercise 5 in repeated binary trials

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

CJS models with STAN: example-models/misc/ecology/mark-recapture/cjs-K.stan

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.

Whitelist of example models

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.:

  • Stan code that matches our current best-practices standards for efficiency, clarity, and priors
  • Has a dataset adhering to the naming convention referenced by @bgoodri in #45
  • Runs quickly (less than 2 minutes?)
  • All Rhats below 1.1
  • No n_divergent draws
  • Represent models we'd like Stan to be known for (H(G)LM, IRT, etc.)

Some of these models don't compile with Stan 2.17.1+

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

Switch to the new assignment operator

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.

update model files to remove deprecated syntax

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
....
  1. Change comments # to //
  2. Change assignment operator <- to =
  3. Change increment_log_prob( to target +=
  4. Change if_else() to ?:
  5. change function names, syntax for all functions _log() - name change, use | instead of comma after initial arg

Additional best practice: use compound declare/define whenever possible

standardize format for models to auto-generate doc

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,

  • create a few examples to illustrate the format
  • doc the format lightly
  • write code (which language?) to generate doc from the hierarchy
    • HTML
    • PDF of HTML
    • better PDF from LaTeX

Would it make sense to put this all in

  • Notebooks
    • iPython
    • equivalent on R side (knitr?)

nile.stan file missing

@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.

Create case studies example

I'm going to start with

  • simple linear regression
  • a Bradley-Terry-like model of on-base ability of batters and pitchers
  • soil-carbon ODE model
  • ecology occupation models
  • Horseshoe and horseshoe + prior vs. lasso example on diabetes data

Request for factorial ANOVA with generated means and sds

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.

Soil model case study: divergences, poor mixing Rhat >> 1.

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  

Update Gaussian Process models

Summary:

Update the Gaussian process model code in the example models repo to mirror the GP chapter in the manual.

Current Version:

v2.16.0

broken IRT model (chapter 19)

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.

  • rewrite model
  • write a data simulator for it with a fitting script
  • make sure simulated data fits

shrinkage uniform prior in Magnesium meta-analysis doesn't work

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!
image

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]

Stan programs that do not compile

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

Survey_Stan.R

Does not plot. Console ...

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("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%

Elapsed Time: 13.5233 seconds (Warm-up)

#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)$n

First calculate MLE:

cc <- -Inf
ind <- 0

for (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 found

    end 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 yet

par(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")
    

ARM custom functions

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

ARM model data file names should match model name

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.

pilot model misfiled

The pilot model is in example_models/ARM/Ch.14, but it's an example from Chapter 13.

Stan PKPD models with Nonmem data support

Provide a working PK model library which supports

  • convenient multiple dosing scenarios
  • regular dosing patterns (using Nonmem addl and tau coding)
  • user extensible to arbitrary models
  • seamless simulation in R of models

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:

  • formalized tests (functions were tested, but code for that has not been automated)
  • performance tuning
  • support for infusions (adds a constant term on the ODE RHS for a fixed time-period)
  • example using integrate_ode_rk45
  • more models (only 1-cmt type models, including metabolite modeling)

Rats example does not work, problem with dimension of x

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?

Stacks example code gives 404

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.

implement BUGS/vol2/asia model

(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.

possible bug in Gaussian_Stan.R (vector[n]<lower=0>)

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?

leuk example uses poisson_log but does not use log rate

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]);

Possible issue with Viterbi code for HMM example

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]]);
    
    for (t in 2:T_unsup) {
    for (k in 1:K) {
    best_logp[t,k] <- negative_infinity();

Stan Models on "Bayesian Statistics and Marketing" case studies

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.

Initial State in Semisupervised HMM Example

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.

deprecation warnings in LDA example code

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!

Pystan example model - comparing two groups

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

Rate_4_Stan.R example

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%

Elapsed Time: 0.20219 seconds (Warm-up)

#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)$postpredk

layout(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="",

  •  ylab="", xlim=c(0,1), ylim=c(0,6))
    
    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"),
  •  cex.axis=0.8)
    
    mtext("Rate", side=1, line=2.25, cex=1.2)
    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),

  •  main="", axes=F )
    
    Error in hist.default(postpredk, breaks = mybreaks, freq = F, right = 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"))

Gelman and Hill 14.1_State-LevelOpinionsFromNationalPolls.R plot error

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

BPA Ch.13: Incorrect `occ_fs`

(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.
venn_bpa_ch13

Model 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);
}

Model 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);
}

Model 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]])';

Other models

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.