# Implementing r's stan\_glm() in Julia

**URL:** <https://discourse.julialang.org/t/implementing-rs-stan-glm-in-julia/78916>\
**Category:** Statistics\
**Tags:** question, stan, glm\
**Created:** [April 2, 2022, 4:50pm UTC](https://discourse.julialang.org/t/implementing-rs-stan-glm-in-julia/78916 "2022-04-02T16:50:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [April 2, 2022, 4:50pm UTC](https://discourse.julialang.org/t/implementing-rs-stan-glm-in-julia/78916/1 "2022-04-02T16:50:56Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [April 2, 2022, 6:29pm UTC](https://discourse.julialang.org/t/implementing-rs-stan-glm-in-julia/78916/2 "2022-04-02T18:29:14Z")

</div>

If you’re just looking for Bayesian GLMs with a formula interface, check out: [https://github.com/TuringLang/TuringGLM.jl](https://github.com/TuringLang/TuringGLM.jl)

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_: [https://github.com/StatisticalRethinkingJulia/StatisticalRethinking.jl](https://github.com/StatisticalRethinkingJulia/StatisticalRethinking.jl)

---

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [April 2, 2022, 6:40pm UTC](https://discourse.julialang.org/t/implementing-rs-stan-glm-in-julia/78916/3 "2022-04-02T18:40:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)\
**Post date:** [April 2, 2022, 9:39pm UTC](https://discourse.julialang.org/t/implementing-rs-stan-glm-in-julia/78916/4 "2022-04-02T21:39:25Z")

</div>

> [@charperflow](#):
>
> 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](https://github.com/StanJulia).

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.

---

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [April 3, 2022, 7:56pm UTC](https://discourse.julialang.org/t/implementing-rs-stan-glm-in-julia/78916/5 "2022-04-03T19:56:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [April 3, 2022, 7:56pm UTC](https://discourse.julialang.org/t/implementing-rs-stan-glm-in-julia/78916/6 "2022-04-03T19:56:55Z")

</div>

Thanks, that Turing package seems promising!
