Paul Viefers
June 25, 2015
\[ \begin{align} p(\theta \mid y) &= \frac{p(y,\theta)}{p(y)} = \frac{p(y \mid \theta) p(\theta)}{p(y)} \propto p(y \mid \theta) p(\theta) \end{align} \]
\( \theta \) an unknown sample proportion or probability of a binary event (see e.g. Markus' blog)
likelihood: \[ \begin{align} p(y \mid \theta) = \binom{n}{y} \theta^{y}(1- \theta)^{n-y} \end{align} \]
prior: \[ \begin{align} p(\theta) = \frac{\Gamma(a+b)}{\Gamma (a) \Gamma(b)}\theta^{a-1} (1-\theta)^{b-1} \end{align} \]
posterior: \[ \begin{align} p(\theta \mid y) \propto \theta^{y + a - 1}(1- \theta)^{n - y + b -1} \end{align} \]
\[ \begin{align} \min \left\{1, p(\theta_a \mid y)/p(\theta_1 \mid y) \right\} \end{align} \]
functions {
# OPTIONAL: user-defined functions
}
data {
# OPTIONAL: read in data ...
}
transformed data {
# OPTIONAL: Create new variables/auxiliary variables
}
parameters {
# Declare parameters that will be estimated
}
transformed parameters {
# OPTIONAL: Declare auxiliary parameters (e.g. to reparamterize, Stan Manual ch. 20)
}
model {
# Declare your probability model: priors, hyperpriors & likelihood
}
generated quantities {
# OPTIONAL: Declare any quantities other than simulated parameters to be generated
}
data {
int<lower=0> N;
int<lower=0> M;
matrix[N, M] x;
vector[N] y;
}
int<lower=a, upper=b> x; // a, b maybe a parameter
real<lower=a, upper=b> x;
vector[N] x;
row_vector[N] x;
matrix[N, K] X;
simplex[h] x; // sum(x) = 1
ordered[j] x; // x[1] < ... < x[j]
cov_matrix[N] Sigma;
corr_matrix[N] Rho;
transformed data {
vector[N] cons;
matrix[N, M+1] X;
cons <- rep_vector(1, N);
X <- append_col(cons, x);
}
parameters {
vector[M+1] beta;
real<lower=0> sigma;
}
model {
y ~ normal(X * beta, sigma);
}
data {
int<lower=0> N;
int<lower=0> M;
matrix[N, M] x;
vector[N] y;
}
transformed data {
vector[N] cons;
matrix[N, M+1] X;
cons <- rep_vector(1, N);
X <- append_col(cons, x);
}
parameters {
vector[K] beta;
real<lower=0> sigma;
}
model {
y ~ normal(x * beta, sigma);
}
# Load Stan from CRAN
install.packages('rstan')
library(rstan)
# In case you want to boost performance through parallelization...
source("http://mc-stan.org/rstan/stan.R")
# Read in data
...
# ... and convert it to a named list (careful with types here)
stan_data <- list(N=N, M=M, y=y, x=x)
# Assume you have Regression.stan in the current WD
fitted.model <- stan(file = 'Regression.stan', model_name='Multi_Reg', data=stan_data, iter=5000, chains=2)
data {
int<lower=1> K; // No. of predictors incl. constant
int<lower=1> G; // No. of individuals or units.
int<lower=0> N; // Total no. of observations
int<lower=1, upper = G> id[N]; // mapping obs. to inds.
int<lower=0, upper = 1> y[N]; // The LHS outcome variable
matrix[N, K] x;
}
parameters {
real mu[K]; // Means of top-level dist.
real<lower = 0> sigmamu[K]; // Top-level variances
matrix[G, K] beta; // Regression parameters incl. intercept
}
model {
vector[N] x_beta;
mu ~ normal(0, 3);
sigmamu ~ chi_square(2);
for(j in 1:K)
for(i in 1:G)
beta[i, j] ~ normal(mu[j], sigmamu[j]);
for(i in 1:N)
x_beta[i] <- dot_product(x[i], beta[id[i]]);
y ~ bernoulli_logit(x_beta);
}