Skip to content

Commit 8a7eb50

Browse files
committed
checkpointing case study updates
1 parent f25f280 commit 8a7eb50

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

knitr/car-iar-poisson/icar_stan.Rmd

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ These are declared in the data block of a Stan program as follows:
266266
data {
267267
int<lower=0> N;
268268
int<lower=0> N_edges;
269-
int<lower=1, upper=N> node1[N_edges];
270-
int<lower=1, upper=N> node2[N_edges];
269+
array[N_edges] int<lower=1, upper=N> node1; // node1[i] adjacent to node2[i]
270+
array[N_edges] int<lower=1, upper=N> node2; // and node1[i] < node2[i]
271271
```
272272
Stan's multiple indexing feature allows multiple indexes to be provided for containers
273273
(i.e., arrays, vectors, and matrices) in a single index position on that container,
@@ -303,8 +303,10 @@ parameters {
303303
}
304304
model {
305305
target += -0.5 * dot_self(phi[node1] - phi[node2]);
306-
// soft sum-to-zero constraint on phi)
307-
sum(phi) ~ normal(0, 0.001 * N); // equivalent to mean(phi) ~ normal(0,0.001)
306+
307+
// soft sum-to-zero constraint on phi,
308+
// equivalent to mean(phi) ~ normal(0,0.01)
309+
sum(phi) ~ normal(0, 0.01 * N);
308310
}
309311
```
310312

@@ -519,8 +521,11 @@ The R script
519521
fits the model to the data.
520522

521523
```{r fit-scotland }
522-
library(rstan)
523-
options(mc.cores = parallel::detectCores())
524+
library(devtools)
525+
if(!require(cmdstanr)){
526+
devtools::install_github("stan-dev/cmdstanr", dependencies=c("Depends", "Imports"))
527+
}
528+
library(cmdstanr)
524529
525530
source("mungeCARdata4stan.R")
526531
source("scotland_data.R")
@@ -534,8 +539,21 @@ node1 = nbs$node1;
534539
node2 = nbs$node2;
535540
N_edges = nbs$N_edges;
536541
537-
bym_scot_stanfit = stan("bym_predictor_plus_offset.stan", data=list(N,N_edges,node1,node2,y,x,E), control=list(adapt_delta = 0.97, stepsize = 0.1), chains=3, warmup=9000, iter=10000, save_warmup=FALSE);
538-
print(bym_scot_stanfit, pars=c("beta0", "beta1", "sigma_phi", "tau_phi", "sigma_theta", "tau_theta", "mu[5]", "phi[5]", "theta[5]"), probs=c(0.025, 0.5, 0.975));
542+
data = list(N=N,N_edges=N_edges,node1=node1,node2=node2,y=y,x=x,E=E)
543+
544+
bym_model = cmdstan_model("bym_predictor_plus_offset.stan");
545+
scot_stanfit = bym_model$sample(
546+
data = data,
547+
parallel_chains = 4,
548+
iter_warmup = 5000,
549+
iter_sampling = 5000);
550+
551+
options(digits=3)
552+
scot_stanfit$summary(variables = c("lp__", "beta0", "beta1",
553+
"sigma_phi", "tau_phi",
554+
"sigma_theta", "tau_theta",
555+
"mu[5]","phi[5]","theta[5]"),
556+
~quantile(.x, probs = c(0.025, 0.5, 0.975)))
539557
```
540558

541559
The priors on all parameters match the priors on the corresponding WinBUGS model in the file

0 commit comments

Comments
 (0)