I am struggling to find the most straightforward way (using as little additional packages as possible) to save a Chains MCMC chain
object (obtained from sampling from a Turing model) on the disk.
I would like to be able, in another script, to load some models and carry on with some post processing and further analysis.
Thanks!
I think JLD2.jl is pretty good at stuff like this.
Also easy to covert a chain to a DataFrame
and save it as a CSV file:
using Turing, DataFrames, CSV
@model function demo(x)
m ~ Normal()
s ~ Exponential()
x .~ Normal(m, s)
end
mod = demo(randn(100))
chn = sample(mod, NUTS(), 1000)
df = DataFrame(chn)
CSV.write("demo_chain.csv", df)
Also easy to covert a chain to a DataFrame
Unfortunately from a dataframe I don’t think I’m able to then load it and re-use the fitted model to make for e.g., predictions etc.
However, it worked using JLD2
using JLD2
jldsave("models/mymodel.jld2"; mymodel)
mymodel= jldopen("models/mymodel.jld2", "r+")["mymodel"]
1 Like
JLD2 is fine for going between steps of a workflow (IIRC you need the environment in which you are saving to be very similar to the one in which you are loading). For more long-term storage (e.g. deposition for a paper), formats like NetCDF are nice, since they also use HDF5 and because they are language- and environment-agnostic. If you use ArviZ.jl for downstream analysis, you can do
using ArviZ
idata = from_mcmcchains(chns)
to_netcdf(idata, "models/mymodel.nc")
1 Like