Skip to content

Commit 7c6abab

Browse files
committed
peel out stan code
1 parent 7c9b5dd commit 7c6abab

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

regression_model_example.stan

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
data { // Data block
2+
int<lower=1> N; // Sample size
3+
int<lower=1> K; // Dimension of model matrix
4+
matrix[N, K] X; // Model Matrix
5+
vector[N] y; // Target variable
6+
}
7+
8+
/*
9+
transformed data { // Transformed data block. Not used presently.
10+
}
11+
*/
12+
13+
parameters { // Parameters block
14+
vector[K] beta; // Coefficient vector
15+
real<lower=0> sigma; // Error scale
16+
}
17+
18+
model { // Model block
19+
vector[N] mu;
20+
mu = X * beta; // Creation of linear predictor
21+
22+
// priors
23+
beta ~ normal(0, 10);
24+
sigma ~ cauchy(0, 5); // With sigma bounded at 0, this is half-cauchy
25+
26+
// likelihood
27+
y ~ normal(mu, sigma);
28+
}
29+
30+
/*
31+
generated quantities { // Generated quantities block. Not used presently.
32+
}
33+
*/

regression_model_example_2.stan

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
data { // Data block
2+
int<lower=1> N; // Sample size
3+
int<lower=1> K; // Dimension of model matrix
4+
matrix [N, K] X; // Model Matrix
5+
vector[N] y; // Target variable
6+
}
7+
8+
/*
9+
transformed data { // Transformed data block. Not used presently.
10+
}
11+
*/
12+
13+
parameters { // Parameters block; declarations only
14+
vector[K] beta; // Coefficient vector
15+
real<lower=0> sigma; // Error scale
16+
}
17+
18+
model { // Model block
19+
vector[N] mu;
20+
mu <- X * beta; // Creation of linear predictor
21+
22+
// priors
23+
beta ~ normal(0, 10);
24+
sigma ~ cauchy(0, 5); // With sigma bounded at 0, this is half-cauchy
25+
26+
// likelihood
27+
y ~ normal(mu, sigma);
28+
}
29+
30+
generated quantities {
31+
real rss;
32+
real totalss;
33+
real<lower=0, upper=1> R2;
34+
vector[N] mu;
35+
36+
mu <- X * beta;
37+
rss <- dot_self(y-mu);
38+
totalss <- dot_self(y-mean(y));
39+
R2 <- 1 - rss/totalss;
40+
}

0 commit comments

Comments
 (0)