# \[ANN\] TuringGLM.jl: A simple @formula macro for specifying Bayesian models in Turing.jl

**URL:** <https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978>\
**Category:** Package Announcements\
**Created:** [February 7, 2022, 10:39pm UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978 "2022-02-07T22:39:10Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [February 7, 2022, 10:39pm UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978/1 "2022-02-07T22:39:10Z")

</div>

I am excited to announce [`TuringGLM.jl`](https://github.com/TuringLang/TuringGLM.jl)!

It uses the well-known `@formula` macro from `JuliaStats` ecosystem (`StatsModels.jl`, `GLM.jl` and `MixedModels.jl`) to specify a Bayesian model and returns a ready to be sampled/run `Turing.jl` model.

It is still a proof-of-concept package and many functionalities are still in the roadmap.  
It is also heavily inspired by python’s bambino (PyMC) and R’s brms (Stan).

* * *

### [`@formula`](https://turinglang.github.io/TuringGLM.jl/dev/#@formula)

The syntax is done by using the `@formula` macro and then specifying the dependent variable followed by a tilde `~` then the independent variables separated by a plus sign `+` .

Example:

```julia-auto
@formula(y ~ x1 + x2 + x3)

```

Moderations/interactions can be specified with the asterisk sign `*` , e.g. `x1 * x2` . This will be expanded to `x1 + x2 + x1:x2` , which, following the principle of hierarchy, the main effects must also be added along with the interaction effects. Here `x1:x2` means that the values of `x1` will be multiplied (interacted) with the values of `x2` .

Random-effects (a.k.a. group-level effects) can be specified with the `(term | group)` inside the `@formula` , where `term` is the independent variable and `group` is the **categorical** representation (i.e., either a column of `String` s or a `CategoricalArray` in `data` ). You can specify a random-intercept with `(1 | group)` .

Example:

```julia-auto
@formula(y ~ (1 | group) + x1)

```

### [Data](https://turinglang.github.io/TuringGLM.jl/dev/#Data)

TuringGLM supports any `Tables.jl` -compatible data interface. The most popular ones are `DataFrame` s and `NamedTuple` s.

### [Supported Models](https://turinglang.github.io/TuringGLM.jl/dev/#Supported-Models)

TuringGLM supports non-hiearchical and hierarchical models. For hierarchical models, only single random-intercept hierarchical models are supported.

For likelihoods, `TuringGLM.jl` supports:

- `Gaussian()` (the default if not specified): linear regression
- `Student()` : robust linear regression
- `Logistic()` : logistic regression
- `Pois()` : Poisson count data regression
- `NegBin()` : negative binomial robust count data regression

* * *

## Tutorials

We have several tutorials in the documentation at [Home · TuringGLM.jl](https://turinglang.github.io/TuringGLM.jl/dev/).

## Acknowledgements

I would like to thank the whole `TuringGLM.jl` dev team, specially @yebai for the support and trust. Also, @rikh was fundamental in the whole development and I probably could not have done without him. Thanks!

---

<div class="post-metadata">

**Author:** ![eliascarv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliascarv/32/22152_2.png) [@eliascarv](https://discourse.julialang.org/u/eliascarv)\
**Post date:** [February 7, 2022, 11:47pm UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978/2 "2022-02-07T23:47:00Z")

</div>

Congratulations! The package is amazing!

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [February 8, 2022, 12:01am UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978/3 "2022-02-08T00:01:30Z")

</div>

Is there a way to extract the underlying Turing model produced by this?

---

<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:** [February 8, 2022, 9:12am UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978/4 "2022-02-08T09:12:57Z")

</div>

Not yet. But in the roadmap.

---

<div class="post-metadata">

**Author:** ![RickandMortyforever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rickandmortyforever/32/33304_2.png) [@RickandMortyforever](https://discourse.julialang.org/u/RickandMortyforever)\
**Post date:** [February 8, 2022, 9:21am UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978/5 "2022-02-08T09:21:33Z")

</div>

It looks like R. Can I set prior distribution on parameters?  
And I enjoyed watching your video on youtube!

---

<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:** [February 8, 2022, 9:49am UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978/6 "2022-02-08T09:49:49Z")

</div>

> [@RickandMortyforever](#):
>
> It looks like R

Yes, it is a gateway drug to R users to get into Julia and Turing.jl

> [@RickandMortyforever](#):
>
> Can I set prior distribution on parameters?

Yes, check [Custom Priors · TuringGLM.jl](https://turinglang.github.io/TuringGLM.jl/dev/tutorials/custom_priors/) and the docstring [API reference · TuringGLM.jl](https://turinglang.github.io/TuringGLM.jl/dev/api/#TuringGLM.CustomPrior).

---

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [August 18, 2022, 9:36pm UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978/7 "2022-08-18T21:36:01Z")

</div>

How can I tell which parameters correspond to the `@formula`? E.g. if we consider the [cheese example](https://beta.turing.ml/TuringGLM.jl/stable/tutorials/hierarchical_models/), how could I tell which of the z\_i[n] corresponds to the type of cheese?

After figuring it out, I’d be happy to PR an addition to the docs to help users like myself.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 19, 2022, 4:20am UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978/8 "2022-08-19T04:20:05Z")

</div>

Is there plan for less macro based construction?

---

<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:** [August 19, 2022, 9:56am UTC](https://discourse.julialang.org/t/ann-turingglm-jl-a-simple-formula-macro-for-specifying-bayesian-models-in-turing-jl/75978/9 "2022-08-19T09:56:12Z")

</div>

@Alec_Loudenback,

Under the hood TuringGLM converts the strings to integers using this [specific function](https://github.com/TuringLang/TuringGLM.jl/blob/main/src/utils.jl#L92) which is called [here](https://github.com/TuringLang/TuringGLM.jl/blob/main/src/data_constructors.jl#L191) and used in the [`turing_model` function here](https://github.com/TuringLang/TuringGLM.jl/blob/main/src/turing_model.jl#L129).

Unfortunately, the ordering is the original ordering of first appearance in the grouping variable.

A PR ideia would be to do a `unique` then `sort` before so that the `z[n]` follows an alphabetical (for strings) or ascending (for ints) order.

> [@jling](#):
>
> Is there plan for less macro based construction?

We used the `@formula` from the Julia’s Stats ecosystem so we don’t have to reinvent the wheel.  
But we would glad to take ideas (and PRs) regarding diffferent ways to build models apart from a `@formula`.
