|
| 1 | +using StatisticalRethinking |
1 | 2 | using JSON |
2 | | -using Colors |
3 | | -using Gadfly |
4 | | -using DataFrames |
5 | | - |
6 | | -using Mamba |
7 | | -using Stan |
8 | | - |
| 3 | +using StanSample |
9 | 4 | using PSIS |
| 5 | +using Statistics |
10 | 6 |
|
11 | | -include("cvit.jl") |
| 7 | +ProjDir = @__DIR__ |
12 | 8 |
|
| 9 | +include(joinpath(ProjDir, "cvit.jl")) |
13 | 10 |
|
14 | 11 | # Data |
15 | | -data = JSON.parsefile("wells.data.json") |
| 12 | +data = JSON.parsefile(joinpath(ProjDir, "wells.data.json")) |
16 | 13 | y = Float64.(data["switched"]) |
17 | 14 | x = Float64[data["arsenic"] data["dist"]] |
18 | 15 | n, m = size(x) |
19 | 16 |
|
20 | 17 | # Model |
21 | | -model_str = readstring(open("arsenic_logistic.stan")) |
22 | | -stanmodel = Stanmodel(name="arsenic_logistic", adapt=500, update=500, model=model_str) |
23 | | - |
24 | | -if isfile("sim.jls") |
25 | | - sim = read("sim.jls", Chains) |
26 | | -else |
27 | | - standata = [Dict("p" => m, "N" => n, "y" => y, "x" => x)] |
28 | | - # Fit the model in Stan |
29 | | - sim = stan(stanmodel, standata, '.', CmdStanDir=CMDSTAN_HOME, summary=false) |
30 | | - write("sim.jls", sim) |
31 | | -end |
| 18 | +model_str = read(open(joinpath(ProjDir, "arsenic_logistic.stan")), String) |
| 19 | +tmpdir = joinpath(ProjDir, "tmp") |
| 20 | +sm = SampleModel("arsenic_logistic", model_str; tmpdir) |
32 | 21 |
|
33 | | -names_sim = filter(x->startswith(x,"log_lik"), sim.names) |
| 22 | +data = (p = m, N = n, y = Int.(y), x = x) |
| 23 | +# Fit the model in Stan |
| 24 | +rc = stan_sample(sm; data) |
| 25 | +nt = read_samples(sm) |
34 | 26 |
|
35 | 27 | # Compute LOO and standard error |
36 | | -log_lik = sim[:, names_sim, :] |
| 28 | +log_lik = nt.log_lik' |
37 | 29 | loo, loos, pk = psisloo(log_lik) |
38 | 30 | elpd_loo = sum(loos) |
39 | 31 | se_elpd_loo = std(loos) * sqrt(n) |
|
0 commit comments