Skip to content

Commit b3a64d0

Browse files
authored
Merge pull request #194 from andrjohns/feature/arm_ch4
Update ARM Ch.4 Example Models
2 parents 241de2c + 598cfbe commit b3a64d0

19 files changed

+140
-152
lines changed

ARM/Ch.4/earn_height.stan

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ data {
33
vector[N] earn;
44
vector[N] height;
55
}
6+
transformed data {
7+
matrix[N,1] x = [height']';
8+
}
69
parameters {
7-
vector[2] beta;
10+
real alpha;
11+
vector[1] beta;
812
real<lower=0> sigma;
913
}
1014
model {
11-
earn ~ normal(beta[1] + beta[2] * height, sigma);
15+
earn ~ normal_id_glm(x, alpha, beta, sigma);
1216
}

ARM/Ch.4/kidiq_interaction.stan

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ data {
55
vector[N] mom_iq;
66
}
77
transformed data { // interaction
8-
vector[N] inter;
9-
inter = mom_hs .* mom_iq;
8+
vector[N] inter = mom_hs .* mom_iq;
9+
matrix[N,3] x = [mom_hs', mom_iq', inter']';
1010
}
1111
parameters {
12-
vector[4] beta;
12+
real alpha;
13+
vector[3] beta;
1314
real<lower=0> sigma;
1415
}
1516
model {
16-
kid_score ~ normal(beta[1] + beta[2] * mom_hs + beta[3] * mom_iq
17-
+ beta[4] * inter, sigma);
17+
kid_score ~ normal_id_glm(x, alpha, beta, sigma);
1818
}

ARM/Ch.4/kidiq_interaction_c.stan

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ data {
55
vector[N] mom_iq;
66
}
77
transformed data { // centered predictors
8-
vector[N] c_mom_hs;
9-
vector[N] c_mom_iq;
10-
vector[N] inter;
11-
c_mom_hs = mom_hs - mean(mom_hs);
12-
c_mom_iq = mom_iq - mean(mom_iq);
13-
inter = c_mom_hs .* c_mom_iq;
8+
vector[N] c_mom_hs = mom_hs - mean(mom_hs);
9+
vector[N] c_mom_iq = mom_iq - mean(mom_iq);
10+
vector[N] inter = c_mom_hs .* c_mom_iq;
11+
matrix[N,3] x = [c_mom_hs', c_mom_iq', inter']';
1412
}
1513
parameters {
16-
vector[4] beta;
14+
real alpha;
15+
vector[3] beta;
1716
real<lower=0> sigma;
1817
}
1918
model {
20-
kid_score ~ normal(beta[1] + beta[2] * c_mom_hs + beta[3] * c_mom_iq
21-
+ beta[4] * inter, sigma);
19+
kid_score ~ normal_id_glm(x, alpha, beta, sigma);
2220
}

ARM/Ch.4/kidiq_interaction_c2.stan

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ data {
55
vector[N] mom_iq;
66
}
77
transformed data { // centering on reference points
8-
vector[N] c2_mom_hs;
9-
vector[N] c2_mom_iq;
10-
vector[N] inter;
11-
c2_mom_hs = mom_hs - 0.5;
12-
c2_mom_iq = mom_iq - 100;
13-
inter = c2_mom_hs .* c2_mom_iq;
8+
vector[N] c2_mom_hs = mom_hs - 0.5;
9+
vector[N] c2_mom_iq = mom_iq - 100;
10+
vector[N] inter = c2_mom_hs .* c2_mom_iq;
11+
matrix[N,3] x = [c2_mom_hs', c2_mom_iq', inter']';
1412
}
1513
parameters {
16-
vector[4] beta;
14+
real alpha;
15+
vector[3] beta;
1716
real<lower=0> sigma;
1817
}
1918
model {
20-
kid_score ~ normal(beta[1] + beta[2] * c2_mom_hs + beta[3] * c2_mom_iq
21-
+ beta[4] * inter, sigma);
19+
kid_score ~ normal_id_glm(x, alpha, beta, sigma);
2220
}

ARM/Ch.4/kidiq_interaction_z.stan

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ data {
55
vector[N] mom_iq;
66
}
77
transformed data { // standardizing
8-
vector[N] z_mom_hs;
9-
vector[N] z_mom_iq;
10-
vector[N] inter;
11-
z_mom_hs = (mom_hs - mean(mom_hs)) / (2 * sd(mom_hs));
12-
z_mom_iq = (mom_iq - mean(mom_iq)) / (2 * sd(mom_iq));
13-
inter = z_mom_hs .* z_mom_iq;
8+
vector[N] z_mom_hs = (mom_hs - mean(mom_hs)) / (2 * sd(mom_hs));
9+
vector[N] z_mom_iq = (mom_iq - mean(mom_iq)) / (2 * sd(mom_iq));
10+
vector[N] inter= z_mom_hs .* z_mom_iq;
11+
matrix[N,3] x = [z_mom_hs', z_mom_iq', inter']';
1412
}
1513
parameters {
16-
vector[4] beta;
14+
real alpha;
15+
vector[3] beta;
1716
real<lower=0> sigma;
1817
}
1918
model {
20-
kid_score ~ normal(beta[1] + beta[2] * z_mom_hs + beta[3] * z_mom_iq
21-
+ beta[4] * inter, sigma);
19+
kid_score ~ normal_id_glm(x, alpha, beta, sigma);
2220
}

ARM/Ch.4/kidscore_momwork.stan

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ transformed data {
77
vector[N] work2;
88
vector[N] work3;
99
vector[N] work4;
10+
matrix[N,3] x;
11+
1012
for (i in 1:N) {
1113
work2[i] = mom_work[i] == 2;
1214
work3[i] = mom_work[i] == 3;
1315
work4[i] = mom_work[i] == 4;
1416
}
17+
18+
x = [work2', work3', work4']';
1519
}
1620
parameters {
17-
vector[4] beta;
21+
real alpha;
22+
vector[3] beta;
1823
real<lower=0> sigma;
1924
}
2025
model {
21-
kid_score ~ normal(beta[1] + beta[2] * work2 + beta[3] * work3
22-
+ beta[4] * work4, sigma);
26+
kid_score ~ normal_id_glm(x, alpha, beta, sigma);
2327
}

ARM/Ch.4/log10earn_height.stan

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ data {
44
vector[N] height;
55
}
66
transformed data { // log 10 transformation
7-
vector[N] log10_earn;
8-
for (i in 1:N) {
9-
log10_earn[i] = log10(earn[i]);
10-
}
7+
vector[N] log10_earn = log10(earn);
8+
matrix[N,1] x = [height']';
119
}
1210
parameters {
13-
vector[2] beta;
11+
real alpha;
12+
vector[1] beta;
1413
real<lower=0> sigma;
1514
}
1615
model {
17-
log10_earn ~ normal(beta[1] + beta[2] * height, sigma);
16+
log10_earn ~ normal_id_glm(x, alpha, beta, sigma);
1817
}

ARM/Ch.4/logearn_height.stan

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ data {
44
vector[N] height;
55
}
66
transformed data { // log transformation
7-
vector[N] log_earn;
8-
log_earn = log(earn);
7+
vector[N] log_earn = log(earn);
8+
matrix[N,1] x = [height']';
99
}
1010
parameters {
11-
vector[2] beta;
11+
real alpha;
12+
vector[1] beta;
1213
real<lower=0> sigma;
1314
}
1415
model {
15-
log_earn ~ normal(beta[1] + beta[2] * height, sigma);
16+
log_earn ~ normal_id_glm(x, alpha, beta, sigma);
1617
}

ARM/Ch.4/logearn_height_male.stan

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ data {
55
vector[N] male;
66
}
77
transformed data { // log transformation
8-
vector[N] log_earn;
9-
log_earn = log(earn);
8+
vector[N] log_earn = log(earn);
9+
matrix[N,2] x = [height', male']';
1010
}
1111
parameters {
12-
vector[3] beta;
12+
real alpha;
13+
vector[2] beta;
1314
real<lower=0> sigma;
1415
}
1516
model {
16-
log_earn ~ normal(beta[1] + beta[2] * height + beta[3] * male, sigma);
17+
log_earn ~ normal_id_glm(x, alpha, beta, sigma);
1718
}

ARM/Ch.4/logearn_interaction.stan

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ data {
55
vector[N] male;
66
}
77
transformed data {
8-
vector[N] log_earn; // log transformation
9-
vector[N] inter; // interaction
10-
log_earn = log(earn);
11-
inter = height .* male;
8+
// log transformation
9+
vector[N] log_earn = log(earn);
10+
// interaction
11+
vector[N] inter = height .* male;
12+
matrix[N,3] x = [height', male', inter']';
1213
}
1314
parameters {
14-
vector[4] beta;
15+
real alpha;
16+
vector[3] beta;
1517
real<lower=0> sigma;
1618
}
1719
model {
18-
log_earn ~ normal(beta[1] + beta[2] * height + beta[3] * male
19-
+ beta[4] * inter, sigma);
20+
log_earn ~ normal_id_glm(x, alpha, beta, sigma);
2021
}

0 commit comments

Comments
 (0)