Skip to content

Commit 346b846

Browse files
committed
updated 17.1-17.3 models with R file
1 parent 5db0986 commit 346b846

13 files changed

+272
-83
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
library(rstan)
2+
3+
srrs2 <- read.table ("srrs2.dat", header=T, sep=",")
4+
mn <- srrs2$state=="MN"
5+
radon <- srrs2$activity[mn]
6+
log.radon <- log (ifelse (radon==0, .1, radon))
7+
floor <- srrs2$floor[mn] # 0 for basement, 1 for first floor
8+
n <- length(radon)
9+
y <- log.radon
10+
x <- floor
11+
12+
# get county index variable
13+
county.name <- as.vector(srrs2$county[mn])
14+
uniq <- unique(county.name)
15+
J <- length(uniq)
16+
county <- rep (NA, J)
17+
for (i in 1:J){
18+
county[county.name==uniq[i]] <- i
19+
}
20+
21+
# no predictors
22+
ybarbar = mean(y)
23+
24+
sample.size <- as.vector (table (county))
25+
sample.size.jittered <- sample.size*exp (runif (J, -.1, .1))
26+
cty.mns = tapply(y,county,mean)
27+
cty.vars = tapply(y,county,var)
28+
cty.sds = mean(sqrt(cty.vars[!is.na(cty.vars)]))/sqrt(sample.size)
29+
cty.sds.sep = sqrt(tapply(y,county,var)/sample.size)
30+
31+
# radon varying intercept and slope model
32+
dataList.1 <- list(N=length(y), y=y, county=county, J=J, x=x)
33+
radon_vary_inter_slope.sf1 <- stan(file='17.1_radon_vary_inter_slope.stan', data=dataList.1,
34+
iter=1000, chains=4)
35+
print(radon_vary_inter_slope.sf1)
36+
37+
# radon correlation model
38+
radon_correlation.sf1 <- stan(file='17.1_radon_correlation.stan', data=dataList.1,
39+
iter=1000, chains=4)
40+
print(radon_correlation.sf1)
41+
42+
# radon multiple varying coefficients model
43+
X <- cbind(1,x)
44+
W <- diag(2)
45+
dataList.1 <- list(N=length(y), y=y, county=county, J=J, X=X, K=2, W=W)
46+
radon_multi_varying_coef.sf1 <- stan(file='17.1_radon_multi_varying_coef.stan', data=dataList.1,
47+
iter=1000, chains=4)
48+
print(radon_multi_varying_coef.sf1)
49+
50+
W <- diag (2)
51+
dataList.2 <- list(N=length(y), y=y, county=county, J=J, x=x, W=W)
52+
53+
# radon Scaled inverse-Wishart model
54+
radon_wishart.sf1 <- stan(file='17.1_radon_wishart.stan', data=dataList.2,
55+
iter=1000, chains=4)
56+
print(radon_wishart.sf1)
57+
58+
# radon Scaled inverse-Wishart model 2
59+
radon_wishart2.sf1 <- stan(file='17.1_radon_wishart2.stan', data=dataList.2,
60+
iter=1000, chains=4)
61+
print(radon_wishart2.sf1)

ARM/Ch.17/17.1_radon_correlation.stan

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ parameters {
1212
real mu_a;
1313
real mu_b;
1414
real<lower=-1,upper=1> rho;
15-
matrix[J,2] B;
15+
vector[2] B_temp;
1616
}
1717
model {
1818
vector[N] y_hat;
1919
vector[J] a;
2020
vector[J] b;
21-
matrix[J,2] B_hat;
21+
matrix[2,J] B_hat;
2222
matrix[2,2] Sigma_b;
23+
vector[2] B_hat_temp;
24+
matrix[2,J] B;
2325

2426
mu_a ~ normal(0, 100);
2527
mu_b ~ normal(0, 100);
@@ -31,18 +33,22 @@ model {
3133
Sigma_b[2,1] <- Sigma_b[1,2];
3234

3335
for (j in 1:J) {
34-
B_hat[j,1] <- mu_a;
35-
B_hat[j,2] <- mu_b;
36-
B[j,1:2] ~ multi_normal(B_hat[j,],Sigma_b);
36+
B_hat[1,j] <- mu_a;
37+
B_hat[2,j] <- mu_b;
38+
B_hat_temp[1] <- mu_a;
39+
B_hat_temp[2] <- mu_b;
40+
B_temp ~ multi_normal(B_hat_temp, Sigma_b);
41+
B[1,j] <- B_temp[1];
42+
B[2,j] <- B_temp[2];
3743
}
3844

3945
for (j in 1:J) {
40-
a[j] <- B[j,1];
41-
b[j] <- B[j,2];
46+
a[j] <- B[1,j];
47+
b[j] <- B[2,j];
4248
}
4349

4450
for (i in 1:N)
45-
y_hat[i] <- a[county[i]] + b[county[i]] * x[i]
51+
y_hat[i] <- a[county[i]] + b[county[i]] * x[i];
4652

4753
y ~ normal(y_hat, sigma);
4854
}
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,53 @@
11
data {
22
int<lower=0> N;
33
int<lower=0> J;
4+
int<lower=0> K;
45
vector[N] y;
5-
int<lower=0,upper=1> x[N];
6+
matrix[N,K] X;
67
int county[N];
8+
matrix[K,K] W;
79
}
810
parameters {
911
real<lower=0> sigma;
10-
vector<lower=0>[K] sigma_b;
1112
vector[K] mu_raw;
1213
vector[K] xi;
13-
matrix[J,K] B_raw;
14-
matrix[K,K] W;
15-
matrix[K,K] Tau_b_raw;
14+
corr_matrix[K] Tau_b_raw;
15+
vector[K] B_raw_temp;
1616
}
1717
model {
1818
vector[N] y_hat;
1919
matrix[J,K] B;
2020
matrix[K,K] rho_b;
2121
matrix[K,K] Sigma_b_raw;
22-
vector[K] mu_raw;
22+
vector[K] mu;
23+
matrix[J,K] B_raw;
24+
vector[K] sigma_b;
2325

2426
mu_raw ~ normal(0, 100);
2527
xi ~ uniform(0, 100);
2628

2729
mu <- xi .* mu_raw;
2830

29-
Tau_b_raw ~ wishart(W, K+1);
31+
Tau_b_raw ~ wishart(K+1, W);
3032
Sigma_b_raw <- inverse(Tau_b_raw);
3133

3234
for (j in 1:J) {
33-
for (k in 1:K)
35+
B_raw_temp ~ multi_normal(mu_raw, Sigma_b_raw);
36+
for (k in 1:K) {
37+
B_raw[j,k] <- B_raw_temp[k];
3438
B[j,k] <- xi[k] * B_raw[j,k];
35-
B_raw[j,] ~ multi_normal(mu_raw, Sigma_b_raw);
39+
}
3640
}
3741

3842
for (k in 1:K) {
3943
for (k_prime in 1:K)
4044
rho_b[k,k_prime] <- Sigma_b_raw[k,k_prime]
4145
/ sqrt(Sigma_b_raw[k,k] * Sigma_b_raw[k_prime,k_prime]);
42-
sigma_b[k] <- abs(xi[k]) * sqrt(Sigma_b_raw[k,k]);
46+
sigma_b[k] <- fabs(xi[k]) * sqrt(Sigma_b_raw[k,k]);
4347
}
4448

4549
for (i in 1:N)
46-
y_hat[i] <- dot_product(B[county[i],],X[i,]);
50+
y_hat[i] <- dot_product(row(B,county[i]),row(X,i));
4751

4852
y ~ normal(y_hat, sigma);
4953
}

ARM/Ch.17/17.1_radon_vary_inter_slope.stan

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ transformed parameters {
1818
vector[N] y_hat;
1919

2020
for (i in 1:N)
21-
y_hat[i] <- a[county[i]] + b[county[i]] * x[i]
21+
y_hat[i] <- a[county[i]] + b[county[i]] * x[i];
2222
}
2323
model {
2424
mu_a ~ normal(0, 100);

ARM/Ch.17/17.1_radon_wishart.stan

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,57 @@ data {
44
vector[N] y;
55
int<lower=0,upper=1> x[N];
66
int county[N];
7+
matrix[2,2] W;
78
}
89
parameters {
910
real<lower=0> sigma;
10-
real<lower=0> sigma_a;
11-
real<lower=0> sigma_b;
1211
real mu_a_raw;
1312
real mu_b_raw;
14-
real xi_a;
15-
real xi_b;
16-
real<lower=-1,upper=1> rho;
17-
matrix[J,2] B_raw;
18-
matrix[2,2] W;
19-
matrix[2,2] Tau_b_raw;
13+
real<lower=0> xi_a;
14+
real<lower=0> xi_b;
15+
vector[2] B_raw_temp;
16+
corr_matrix[2] Tau_b_raw;
2017
}
2118
model {
19+
real sigma_a;
20+
real sigma_b;
2221
vector[N] y_hat;
2322
vector[J] a;
2423
vector[J] b;
2524
matrix[J,2] B_raw_hat;
26-
matrix[J,2] B_raw;
2725
matrix[2,2] Sigma_b_raw;
26+
matrix[J,2] B_raw;
2827
real mu_a;
2928
real mu_b;
29+
real rho;
3030

3131
mu_a_raw ~ normal(0, 100);
3232
mu_b_raw ~ normal(0, 100);
3333
xi_a ~ uniform(0, 100);
3434
xi_b ~ uniform(0, 100);
35-
rho ~ uniform(-1, 1);
3635

3736
mu_a <- xi_a * mu_a_raw;
3837
mu_b <- xi_b * mu_b_raw;
3938

40-
Tau_b_raw ~ wishart(W, 3);
39+
Tau_b_raw ~ wishart(3, W);
4140
Sigma_b_raw <- inverse(Tau_b_raw);
4241

4342
sigma_a <- xi_a * sqrt(Sigma_b_raw[1,1]);
44-
sigma_b <- xi_b * sqrt(Sigma_b_raw[2,2]):
43+
sigma_b <- xi_b * sqrt(Sigma_b_raw[2,2]);
4544
rho <- Sigma_b_raw[1,2] / sqrt(Sigma_b_raw[1,1] * Sigma_b_raw[2,2]);
4645

4746
for (j in 1:J) {
4847
B_raw_hat[j,1] <- mu_a_raw;
4948
B_raw_hat[j,2] <- mu_b_raw;
50-
B_raw[j,1:2] ~ multi_normal(B_raw_hat[j,], Sigma_b_raw);
49+
B_raw_temp ~ multi_normal(transpose(row(B_raw_hat,j)), Sigma_b_raw);
50+
B_raw[j,1] <- B_raw_temp[1];
51+
B_raw[j,2] <- B_raw_temp[2];
5152
a[j] <- xi_a * B_raw[j,1];
5253
b[j] <- xi_b * B_raw[j,2];
5354
}
5455

5556
for (i in 1:N)
56-
y_hat[i] <- a[county[i]] + b[county[i]] * x[i]
57+
y_hat[i] <- a[county[i]] + b[county[i]] * x[i];
5758

5859
y ~ normal(y_hat, sigma);
5960
}

ARM/Ch.17/17.1_radon_wishart2.stan

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ data {
44
vector[N] y;
55
int<lower=0,upper=1> x[N];
66
int county[N];
7+
matrix[2,2] W;
78
}
89
parameters {
910
real<lower=0> sigma;
10-
real<lower=0> sigma_a;
11-
real<lower=0> sigma_b;
1211
real mu_a_raw;
1312
real mu_b_raw;
14-
real xi_a;
15-
real xi_b;
16-
real<lower=-1,upper=1> rho;
17-
matrix[J,2] B_raw;
18-
matrix[2,2] W;
19-
matrix[2,2] Tau_b_raw;
20-
vector[K] b_0;
21-
matrix[N,K] X_0;
13+
real<lower=0> xi_a;
14+
real<lower=0> xi_b;
15+
vector[2] B_raw_temp;
16+
corr_matrix[2] Tau_b_raw;
17+
real b_0;
2218
}
2319
model {
2420
vector[N] y_hat;
@@ -29,34 +25,38 @@ model {
2925
matrix[2,2] Sigma_b_raw;
3026
real mu_a;
3127
real mu_b;
28+
real sigma_a;
29+
real sigma_b;
30+
real rho;
3231

3332
mu_a_raw ~ normal(0, 100);
3433
mu_b_raw ~ normal(0, 100);
3534
xi_a ~ uniform(0, 100);
3635
xi_b ~ uniform(0, 100);
37-
rho ~ uniform(-1, 1);
3836
b_0 ~ normal(0, 100);
3937

4038
mu_a <- xi_a * mu_a_raw;
4139
mu_b <- xi_b * mu_b_raw;
4240

43-
Tau_b_raw ~ wishart(W, 3);
41+
Tau_b_raw ~ wishart(3, W);
4442
Sigma_b_raw <- inverse(Tau_b_raw);
4543

4644
sigma_a <- xi_a * sqrt(Sigma_b_raw[1,1]);
47-
sigma_b <- xi_b * sqrt(Sigma_b_raw[2,2]):
45+
sigma_b <- xi_b * sqrt(Sigma_b_raw[2,2]);
4846
rho <- Sigma_b_raw[1,2] / sqrt(Sigma_b_raw[1,1] * Sigma_b_raw[2,2]);
4947

5048
for (j in 1:J) {
5149
B_raw_hat[j,1] <- mu_a_raw;
5250
B_raw_hat[j,2] <- mu_b_raw;
53-
B_raw[j,1:2] ~ multi_normal(B_raw_hat[j,], Sigma_b_raw);
51+
B_raw_temp ~ multi_normal(transpose(row(B_raw_hat,j)), Sigma_b_raw);
52+
B_raw[j,1] <- B_raw_temp[1];
53+
B_raw[j,2] <- B_raw_temp[2];
5454
a[j] <- xi_a * B_raw[j,1];
5555
b[j] <- xi_b * B_raw[j,2];
5656
}
5757

5858
for (i in 1:N)
59-
y_hat[i] <- dot_product(b_0,X_0[i,]) + a[county[i]] + b[county[i]] * x[i]
59+
y_hat[i] <- b_0 + a[county[i]] + b[county[i]] * x[i];
6060

6161
y ~ normal(y_hat, sigma);
6262
}

ARM/Ch.17/17.2_radon_correlation.stan

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ data {
44
vector[N] y;
55
int<lower=0,upper=1> x[N];
66
int county[N];
7+
vector[N] u;
78
}
89
parameters {
910
real<lower=0> sigma;
@@ -14,14 +15,15 @@ parameters {
1415
real g_b_0;
1516
real g_b_1;
1617
real<lower=-1,upper=1> rho;
17-
matrix[J,2] B;
18+
vector[2] B_temp;
1819
}
1920
model {
2021
vector[N] y_hat;
2122
vector[J] a;
2223
vector[J] b;
2324
matrix[J,2] B_hat;
2425
matrix[2,2] Sigma_b;
26+
matrix[J,2] B;
2527

2628
g_a_0 ~ normal(0, 100);
2729
g_a_1 ~ normal(0, 100);
@@ -37,16 +39,15 @@ model {
3739
for (j in 1:J) {
3840
B_hat[j,1] <- g_a_0 + g_a_1 * u[j];
3941
B_hat[j,2] <- g_b_0 + g_b_1 * u[j];
40-
B[j,1:2] ~ multi_normal(B_hat[j,],Sigma_b);
41-
}
42-
43-
for (j in 1:J) {
42+
B_temp ~ multi_normal(transpose(row(B_hat,j)),Sigma_b);
43+
B[j,1] <- B_temp[1];
44+
B[j,2] <- B_temp[2];
4445
a[j] <- B[j,1];
4546
b[j] <- B[j,2];
4647
}
4748

4849
for (i in 1:N)
49-
y_hat[i] <- a[county[i]] + b[county[i]] * x[i]
50+
y_hat[i] <- a[county[i]] + b[county[i]] * x[i];
5051

5152
y ~ normal(y_hat, sigma);
5253
}

0 commit comments

Comments
 (0)