Implementing r's stan_glm() in Julia

I am working through “Regression and Other Stories” for a class. All of the examples are done in R and I have been trying to recreate them in Julia for practice. For the most part it has been no problem, however, I am having a difficult time incorporating Stan as smoothly as they do.

The authors frequently use rstan’s stan_glm( ) call to get coefficients for a linear model. The function call seems to behave identically to julias GLM.lm() function. However, I know whats happening “under the hood” is different. I have StanSample and MCMCChains installed and working, however there doesn’t seem to be a simple call to stan_glm() like in “r”. How do I accomplish something similar in the Julia ecosystem?

For example in r you could fit a simple Bayesian regression just with:

fit<-stan_glm(y ~ x , data = mydata)

If you’re just looking for Bayesian GLMs with a formula interface, check out: GitHub - TuringLang/TuringGLM.jl: Bayesian Generalized Linear models using `@formula` syntax.

It doesn’t use Stan as a backend, but it uses many of the same algorithms (HMC/NUTS, etc.).

GLM.jl’s functions are more comparable to R’s builtin glm and lm function than the functions from rstanarm or brms. If you don’t understand how [g]lm and and stan_glm differ, then I would suggest focusing on the conceptual underpinnings there before trying to find exact software equivalents.

There is also a Julia port McElreath’s Statisitcal Rethinking: GitHub - StatisticalRethinkingJulia/StatisticalRethinking.jl: Julia package with selected functions in the R package `rethinking`. Used in the SR2... projects.

1 Like

I understand the theoretical differences in how they work, which is why I am not satisfied recreating the text examples using the GLM package. I didn’t want to focus on that because my main question is just trying to reliably recreate examples from the text, so what would be the easiest close approximation in Julia. Does the julia ecosystem not support such a quick and casual use of STAN?

No, that is only rstanarm not even the Python Stan’s packages do that. We have something similar that is bambi but is an interface to PyMC3 (another probabilistic programming language, PPL, in Python).

TuringGLM.jl would be the closest a quick and casual use of PPL with a nice formula interface.

If you want to use Stan in Julia checkout the StanJulia GitHub organization.

As a long contributor to Stan and a current member of the Stan Governing Body, I can attest that unfortunately Julia is not high in the priorities list of Stan. It is much focused in R and Python. But the developers and academics around Stan they obviously see what different PPLs are doing and highly praise Turing.jl as a good example of a nice PPL.

1 Like

Thank you for the insights, this exactly what I was looking for!

1 Like

Thanks, that Turing package seems promising!

1 Like