Skip to content

Instantly share code, notes, and snippets.

@nstjhp
Forked from rasmusab/rstan_test_script.R
Created March 2, 2021 18:43
Show Gist options
  • Save nstjhp/7b8c65feda21f5000e6822c6d83df393 to your computer and use it in GitHub Desktop.
Save nstjhp/7b8c65feda21f5000e6822c6d83df393 to your computer and use it in GitHub Desktop.
A short script you can use to test if rstan is installed and working correctly.
# Prior to the tutorial make sure that the script below runs without error on your R installation.
# What you need is a working installation of Stan: http://mc-stan.org/ .
# For installation instructions, see here:
# https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
# After installation you should be able to run this script which should output
# some summary statistics and some pretty plots, :)
# Generating some fake data
set.seed(123)
y <- rbinom(30, size = 1, prob = 0.2016)
# Fitting a simple binomial model using Stan
library(rstan)
model_string <- "
data {
int n;
int y[n];
}
parameters {
real<lower=0, upper=1> theta;
}
model {
y ~ bernoulli(theta);
}"
stan_samples <- stan(model_code = model_string, data = list(y = y, n = length(y)) )
stan_samples
traceplot(stan_samples)
plot(stan_samples)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment