Skip to content

Commit 0aa03e2

Browse files
committed
checkpoint work on bym2 et al
1 parent 2888017 commit 0aa03e2

File tree

8 files changed

+54
-68
lines changed

8 files changed

+54
-68
lines changed

knitr/car-iar-poisson/update_2021_02/BYM2 islands.ipynb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@
385385
},
386386
{
387387
"cell_type": "code",
388-
"execution_count": null,
388+
"execution_count": 2,
389389
"metadata": {},
390390
"outputs": [],
391391
"source": [
@@ -407,9 +407,21 @@
407407
},
408408
{
409409
"cell_type": "code",
410-
"execution_count": null,
411-
"metadata": {},
412-
"outputs": [],
410+
"execution_count": 3,
411+
"metadata": {},
412+
"outputs": [
413+
{
414+
"name": "stdout",
415+
"output_type": "stream",
416+
"text": [
417+
"num nodes: 56, num edges: 126\n",
418+
"num components: 4\n",
419+
"scaling factors: [0.4504, 1, 1, 1]\n",
420+
"nodes per component: [53, 1, 1, 1]\n",
421+
"node indices: [[1, 2, 3, 4, 5, 7, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 0, 0], [6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]\n"
422+
]
423+
}
424+
],
413425
"source": [
414426
"with open('scotland_islands.data.json') as fd:\n",
415427
" islands_data = json.load(fd)\n",

knitr/car-iar-poisson/update_2021_02/bym2.stan

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ parameters {
6767
}
6868
transformed parameters {
6969
// spatial effects (combine heterogeneous and spatially smoothed)
70-
vector[I] gamma = (sqrt(1 - rho) * theta + sqrt(rho / tau) * phi) * sigma;
70+
vector[I] gamma = sigma * (sqrt(1 - rho) * theta
71+
+ sqrt(rho / tau) * phi);
7172
}
7273
model {
73-
y ~ poisson_log(log_E + alpha + x * beta + gamma * sigma); // co-variates
74+
y ~ poisson_log(log_E + alpha + x * beta + gamma); // likelihood
7475

7576
alpha ~ normal(0, 1);
7677
beta ~ normal(0, 1);
@@ -82,20 +83,6 @@ model {
8283
phi ~ standard_icar(edges);
8384
}
8485
generated quantities {
85-
// posterior predictive checks
86-
vector[I] eta = log_E + alpha + x * beta + gamma * sigma;
87-
vector[I] y_prime = exp(eta);
88-
// int y_rep[I,10];
89-
// for (j in 1:10) {
90-
// if (max(eta) > 20) {
91-
// // avoid overflow in poisson_log_rng
92-
// print("max eta too big: ", max(eta));
93-
// for (i in 1:I)
94-
// y_rep[i,j] = -1;
95-
// } else {
96-
// for (i in 1:I)
97-
// y_rep[i,j] = poisson_log_rng(eta[i]);
98-
// }
99-
// }
100-
real logit_rho = log(rho / (1.0 - rho));
86+
vector[I] eta = log_E + alpha + x * beta + gamma;
87+
vector[I] y_est = exp(eta);
10188
}

knitr/car-iar-poisson/update_2021_02/bym2_islands.stan

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@ functions {
5252
" elements in phi and columns of node_idxs, and ",
5353
num_edges,
5454
" columns of edge_idxs.");
55-
5655
real total = 0;
5756
for (n in 1:num_comps) {
58-
if (node_cts[n] > 1)
59-
total += -0.5 * dot_self(phi[adjacency[1, edge_idxs[n, 1:edge_cts[n]]]] -
60-
phi[adjacency[2, edge_idxs[n, 1:edge_cts[n]]]])
61-
+ normal_lpdf(sum(phi[node_idxs[n, 1:node_cts[n]]]) | 0, 0.001 * node_cts[n]);
62-
else
63-
total += normal_lpdf(phi[node_idxs[n, 1]] | 0, 1);
57+
int nodes[node_cts[n]] = node_idxs[n, 1:node_cts[n]];
58+
if (node_cts[n] > 1) {
59+
int edges[edge_cts[n]] = edge_idxs[n, 1:edge_cts[n]];
60+
total += -0.5 * dot_self(phi[adjacency[1, edges]] - phi[adjacency[2, edges]])
61+
+ normal_lpdf(sum(phi[nodes]) | 0, 0.001 * node_cts[n]);
62+
} else {
63+
total += normal_lpdf(phi[nodes] | 0, 1);
64+
}
6465
}
6566
return total;
6667
}
@@ -77,36 +78,37 @@ data {
7778
int<lower=0, upper=I> K_node_idxs[K, I]; // rows contain per-component node indexes
7879
int<lower=0, upper=J> K_edge_idxs[K, J]; // rows contain per-component edge indexes
7980

80-
vector[K] tau; // scaling factor
81+
vector[K] tau; // per-component scaling factor
8182

8283
int<lower=0> y[I]; // count outcomes
8384
vector<lower=0>[I] E; // exposure
8485
vector[I] x; // predictor
8586
}
8687
transformed data {
8788
vector[I] log_E = log(E);
89+
vector[I] taus;
90+
for (k in 1:K) {
91+
int node_idxs[K_node_cts[k]] = K_node_idxs[k, 1:K_node_cts[k]];
92+
for (j in 1:size(node_idxs))
93+
taus[node_idxs[j]] = tau[k];
94+
}
8895
}
8996
parameters {
90-
real alpha; // intercept
91-
real beta; // covariates
97+
real alpha; // intercept
98+
real beta; // single covariate (Scotland dataset)
9299

93100
// spatial effects
94101
real<lower=0, upper=1> rho; // proportion unstructured vs. spatially structured variance
95-
real<lower = 0> sigma; // scale of spatial effects
102+
real<lower=0> sigma; // scale of spatial effects
96103
vector[I] theta; // standardized heterogeneous spatial effects
97104
vector[I] phi; // standardized spatially smoothed spatial effects
98105
}
99106
transformed parameters {
100-
vector[I] gamma;
101-
for (k in 1:K)
102-
gamma[K_node_idxs[k, 1:K_node_cts[k]]] =
103-
(sqrt(1 - rho) * theta[K_node_idxs[k, 1:K_node_cts[k]]]
104-
+
105-
sqrt(rho / tau[k]) * phi[K_node_idxs[k, 1:K_node_cts[k]]])
106-
* sigma;
107-
}
107+
vector[I] gamma = sigma * (sqrt(1 - rho) * theta
108+
+ sqrt(rho ./ taus) .* phi);
109+
}
108110
model {
109-
y ~ poisson_log(log_E + alpha + x * beta + gamma * sigma); // co-variates
111+
y ~ poisson_log(log_E + alpha + x * beta + gamma); // likelihood
110112

111113
alpha ~ normal(0, 1);
112114
beta ~ normal(0, 1);
@@ -118,20 +120,6 @@ model {
118120
phi ~ standard_icar_disconnected(edges, K_node_cts, K_edge_cts, K_node_idxs, K_edge_idxs);
119121
}
120122
generated quantities {
121-
// posterior predictive checks
122-
vector[I] eta = log_E + alpha + x * beta + gamma * sigma;
123-
vector[I] y_prime = exp(eta);
124-
// int y_rep[I,10];
125-
// for (j in 1:10) {
126-
// if (max(eta) > 20) {
127-
// // avoid overflow in poisson_log_rng
128-
// print("max eta too big: ", max(eta));
129-
// for (i in 1:I)
130-
// y_rep[i,j] = -1;
131-
// } else {
132-
// for (i in 1:I)
133-
// y_rep[i,j] = poisson_log_rng(eta[i]);
134-
// }
135-
// }
136-
real logit_rho = log(rho / (1.0 - rho));
123+
vector[I] eta = log_E + alpha + x * beta + gamma;
124+
vector[I] y_est = exp(eta);
137125
}
-41.5 KB
Binary file not shown.
-34.4 KB
Binary file not shown.
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
{
2-
"I": 56,
3-
"J": 132,
2+
"I": 56,
3+
"J": 132,
44

55
"edges": [
66
[ 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 7, 7, 7, 7, 9, 9, 9, 9, 9, 10, 10, 11, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 16, 17, 17, 18, 18, 18, 18, 18, 20, 21, 21, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 29, 29, 29, 30, 30, 30, 30, 30, 31, 31, 31, 31, 32, 33, 33, 34, 34, 34, 34, 34, 34, 34, 35, 35, 36, 36, 36, 37, 37, 38, 38, 38, 38, 38, 39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 44, 44, 45, 46, 46, 47, 47, 47, 48, 49, 49, 49, 51, 52, 55 ],
77
[ 5, 9, 11, 19, 7, 10, 6, 12, 18, 20, 28, 11, 12, 13, 19, 8, 10, 13, 16, 17, 11, 17, 19, 23, 29, 16, 22, 12, 17, 19, 31, 32, 35, 25, 29, 50, 17, 21, 22, 29, 19, 29, 20, 28, 33, 55, 56, 55, 29, 50, 29, 34, 36, 37, 39, 27, 30, 31, 44, 47, 48, 55, 56, 26, 29, 29, 42, 43, 31, 32, 55, 33, 45, 34, 43, 50, 38, 42, 44, 45, 56, 32, 35, 46, 47, 35, 45, 56, 39, 40, 42, 43, 51, 52, 54, 37, 46, 37, 39, 41, 41, 46, 42, 44, 49, 51, 54, 40, 41, 41, 49, 52, 46, 49, 53, 43, 51, 48, 49, 56, 47, 53, 48, 49, 53, 49, 52, 53, 54, 54, 54, 56 ]
88
],
99

10-
"tau": 0.4853,
10+
"tau": 0.4853,
1111

12-
"y": [ 9, 39, 11, 9, 15, 8, 26, 7, 6, 20, 13, 5, 3, 8, 17, 9, 2, 7, 9, 7, 16, 31, 11, 7, 19, 15, 7, 10, 16, 11, 5, 3, 7, 8, 11, 9, 11, 8, 6, 4, 10, 8, 2, 6, 19, 3, 2, 3, 28, 6, 1, 1, 1, 1, 0, 0 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
1715

16+
"x": [16, 16, 10, 24, 10, 24, 10, 7, 7, 16, 7, 16, 10, 24, 7, 16, 10, 7, 7, 10, 7, 16, 10, 7, 1, 1, 7, 7, 10, 10, 7, 24, 10, 7, 7, 0, 10, 1, 16, 0, 1, 16, 16, 0, 1, 7, 1, 1, 0, 1, 1, 0, 1, 1, 16, 10]
1817
}

knitr/car-iar-poisson/update_2021_02/scotland_connected_as_Kgraph.data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
xo{
22
"I": 56,
33
"J": 132,
44

knitr/car-iar-poisson/update_2021_02/scotland_islands.data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222

2323
"E": [ 1.4, 8.7, 3, 2.5, 4.3, 2.4, 8.1, 2.3, 2, 6.6, 4.4, 1.8, 1.1, 3.3, 7.8, 4.6, 1.1, 4.2, 5.5, 4.4, 10.5, 22.7, 8.8, 5.6, 15.5, 12.5, 6, 9, 14.4, 10.2, 4.8, 2.9, 7, 8.5, 12.3, 10.1, 12.7, 9.4, 7.2, 5.3, 18.8, 15.8, 4.3, 14.6, 50.7, 8.2, 5.6, 9.3, 88.7, 19.6, 3.4, 3.6, 5.7, 7, 4.2, 1.8 ],
2424

25-
"x": [1.6, 1.6, 1.0, 2.4, 1.0, 2.4, 1.0, 0.7, 0.7, 1.6, 0.7, 1.6, 1.0, 2.4, 0.7, 1.6, 1.0, 0.7, 0.7, 1.0, 0.7, 1.6, 1.0, 0.7, 0.1, 0.1, 0.7, 0.7, 1.0, 1.0, 0.7, 2.4, 1.0, 0.7, 0.7, 0, 1.0, 0.1, 1.6, 0, 0.1, 1.6, 1.6, 0, 0.1, 0.7, 0.1, 0.1, 0, 0.1, 0.1, 0, 0.1, 0.1, 1.6, 1.0]
25+
"x": [16, 16, 10, 24, 10, 24, 10, 7, 7, 16, 7, 16, 10, 24, 7, 16, 10, 7, 7, 10, 7, 16, 10, 7, 1, 1, 7, 7, 10, 10, 7, 24, 10, 7, 7, 0, 10, 1, 16, 0, 1, 16, 16, 0, 1, 7, 1, 1, 0, 1, 1, 0, 1, 1, 16, 10]
2626
}

0 commit comments

Comments
 (0)