[READ-ONLY] Mirror of https://github.com/just-cameron/BayesModels. Easy Bayesian models.
BayesModels README.md
4.6 kB · 102 lines
Markdown

# BayesModels.jl BayesModels.jl is a package that uses [Turing](https://github.com/TuringLang/Turing.jl) for Bayesian inference and [StatsModels](https://github.com/JuliaStats/StatsModels.jl) for its easy-to-use formula API. Currently only Bayesian linear regression is supported, but I plan at some point to extend it to additional model types. # How to You can run a Bayesian linear regression using `blm(formula, dataframe)`. ```julia # Import packages. using BayesModels using RDatasets using DataFrames # Load the wages dataframe. # I'm thinning out the dataframe to make it quicker, # Bayesian methods don't scale as well with bigger data. df = RDatasets.dataset("Ecdat", "Wages")[1:10:end, :] # Estimate the model; # Defaults to 1000 MCMC samples using NUTS chain = blm(@formula(LWage ~ 1 + Married + Ed + Married*Ed), df) # Display the results. display(chain) ``` Custom priors for the intercept `α` and coefficients `β`, as well as any likelihood specific parameters, can be optionally passed as keyword arguments. Priors can be specified as either a `Distribution` or a function of the data `(X,y) -> Distribution`: ```julia # use higher prior variance on intercept and coefficients α_prior = Normal(0,2) β_prior = (X,y) -> MvNormal(zeros(size(X,2)),2) # use truncated Normal for observation variance σ_prior = truncated(Normal(0,5),0,Inf) chain = blm(@formula(LWage ~ 1 + Married + Ed + Married*Ed), df; α_prior=α_prior, β_prior=β_prior, σ_prior=σ_prior) ``` You can customize inference by explicitly setting the `InferenceStrategy`. The `MCMC` strategy takes a sampler algorithm as its first argument and passes all other arguments directly to `sample`: ```julia df = RDatasets.dataset("Ecdat", "Wages")[1:10:end, :] # Draw 500 samples from 4 chains in parallel using HMC instead of NUTS. chain = blm(@formula(LWage ~ 1 + Married + Ed + Married*Ed), df, MCMC(HMC(0.05,10),MCMCThreads(),500,n_chains=4)) ``` `blm` also supports `Bernoulli`, `Exponential`, and `Poisson` likelihoods for non-Normal response variables. By …