|
| 1 | +using StatisticalRethinking |
| 2 | +using JSON |
| 3 | +using StanSample |
| 4 | +using PSIS |
| 5 | +#using Statistics |
| 6 | +using Printf |
| 7 | +#using StatsPlots |
| 8 | + |
| 9 | +ProjDir = @__DIR__ |
| 10 | + |
| 11 | +include(joinpath(ProjDir, "cvit.jl")) |
| 12 | + |
| 13 | +# Data |
| 14 | +data = JSON.parsefile(joinpath(ProjDir, "wells.data.json")) |
| 15 | +y = Float64.(data["switched"]) |
| 16 | +x = Float64[data["arsenic"] data["dist"]] |
| 17 | +n, m = size(x) |
| 18 | + |
| 19 | +# Model |
| 20 | +model_str = read(open(joinpath(ProjDir, "arsenic_logistic.stan")), String) |
| 21 | +tmpdir = joinpath(ProjDir, "tmp") |
| 22 | +sm1 = SampleModel("arsenic_logistic", model_str) |
| 23 | + |
| 24 | +data1 = (p = m, N = n, y = Int.(y), x = x) |
| 25 | +# Fit the model in Stan |
| 26 | +rc1 = stan_sample(sm1; data=data1) |
| 27 | +if success(rc1) |
| 28 | + nt1 = read_samples(sm1) |
| 29 | + |
| 30 | + # Compute LOO and standard error |
| 31 | + log_lik = nt1.log_lik' |
| 32 | + loo, loos, pk = psisloo(log_lik) |
| 33 | + elpd_loo = sum(loos) |
| 34 | + se_elpd_loo = std(loos) * sqrt(n) |
| 35 | + @printf(">> elpd_loo = %.1f, SE(elpd_loo) = %.1f\n", elpd_loo, se_elpd_loo) |
| 36 | + |
| 37 | + # Check the shape parameter k of the generalized Pareto distribution |
| 38 | + if all(pk .< 0.5) |
| 39 | + println("All Pareto k estimates OK (k < 0.5)") |
| 40 | + else |
| 41 | + pkn1 = sum((pk .>= 0.5) & (pk .< 1)) |
| 42 | + pkn2 = sum(pk .>= 1) |
| 43 | + @printf(">> %d (%.0f%%) PSIS Pareto k estimates between 0.5 and 1\n", pkn1, pkn1/n*100) |
| 44 | + @printf(">> %d (%.0f%%) PSIS Pareto k estimates greater than 1\n", pkn2, pkn2/n*100) |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +# Fit a second model, using log(arsenic) instead of arsenic |
| 49 | +x2 = Float64[log.(data["arsenic"]) data["dist"]] |
| 50 | + |
| 51 | +# Model |
| 52 | +data2 = (p = m, N = n, y = Int.(y), x = x2) |
| 53 | +# Fit the model in Stan |
| 54 | +rc2 = stan_sample(sm1; data=data2) |
| 55 | + |
| 56 | +if success(rc2) |
| 57 | + nt2 = read_samples(sm1) |
| 58 | + # Compute LOO and standard error |
| 59 | + log_lik = nt2.log_lik' |
| 60 | + loo2, loos2, pk2 = psisloo(log_lik) |
| 61 | + elpd_loo = sum(loos2) |
| 62 | + se_elpd_loo = std(loos2) * sqrt(n) |
| 63 | + @printf(">> elpd_loo = %.1f, SE(elpd_loo) = %.1f\n", elpd_loo, se_elpd_loo) |
| 64 | + |
| 65 | + # Check the shape parameter k of the generalized Pareto distribution |
| 66 | + if all(pk .< 0.5) |
| 67 | + println("All Pareto k estimates OK (k < 0.5)") |
| 68 | + else |
| 69 | + pkn1 = sum((pk .>= 0.5) & (pk .< 1)) |
| 70 | + pkn2 = sum(pk .>= 1) |
| 71 | + @printf(">> %d (%.0f%%) PSIS Pareto k estimates between 0.5 and 1\n", pkn1, pkn1/n*100) |
| 72 | + @printf(">> %d (%.0f%%) PSIS Pareto k estimates greater than 1\n", pkn2, pkn2/n*100) |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +if success(rc1) && success(rc2) |
| 77 | + ## Compare the models |
| 78 | + loodiff = loos - loos2 |
| 79 | + @printf("elpd_diff = %.1f, SE(elpd_diff) = %.1f\n",sum(loodiff), std(loodiff) * sqrt(n)) |
| 80 | +end |
| 81 | + |
| 82 | +## k-fold-CV |
| 83 | +# k-fold-CV should be used if several khats>0.5 |
| 84 | +# in this case it is not needed, but provided as an example |
| 85 | + |
| 86 | +model_str = read(open(joinpath(ProjDir, "arsenic_logistic_t.stan")), String) |
| 87 | +sm3 = SampleModel("arsenic_logistic_t", model_str); |
| 88 | + |
| 89 | +cvitr, cvitst = cvit(n, 10, true) |
| 90 | +kfcvs = similar(loos) |
| 91 | +for cvi in 1:3 |
| 92 | + @printf("%d\n", cvi) |
| 93 | + |
| 94 | + standatacv = (p = m, N = length(cvitr[cvi]), Nt = length(cvitst[cvi]), |
| 95 | + x = x[cvitr[cvi],:], y = Int.(y[cvitr[cvi]]), |
| 96 | + xt = x[cvitst[cvi],:], yt = Int.(y[cvitst[cvi]])) |
| 97 | + |
| 98 | + # Fit the model in Stan |
| 99 | + rc3 = stan_sample(sm3; data=standatacv) |
| 100 | + if success(rc3) |
| 101 | + nt3 = read_samples(sm3) |
| 102 | + # Compute LOO and standard error |
| 103 | + log_likt = nt3.log_likt' |
| 104 | + kfcvs[cvitst[cvi]] = PSIS.logsumexp(log_likt) .- log(size(log_likt, 1)) |
| 105 | + end |
| 106 | +end |
0 commit comments