# I find R's DSL for defining regression a bit mind blowing. GLM.jl and other have tried to "adopt" them. What would an ideal DSL for defining regression look like in Julia?

**URL:** <https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786>\
**Category:** Modelling & Simulations\
**Tags:** regression\
**Created:** [October 1, 2024, 2:50pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786 "2024-10-01T14:50:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 1, 2024, 2:50pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/1 "2024-10-01T14:50:01Z")

</div>

In R `glm(x~-1+y+z)` fits a GLM model without an intercept and using `y` and `z` as data, so there will be two coefficients from the model fit.

I think GLM.jl has adopted the `~` DSL. But I’ve always found R’s DSL on regression to be whacky. More examples `y~x^2` is looking for interactions effects of `x` and not fitting a coefficient for `x` squared. To do that you need `y~I(x^2)`.

I don’t know the origin of R’s regression DSL but I find it strange that Julia’s main package has adopted it since it feels so unintuitive.

Are there modern alternatives? I don’t imagine there is a lot of research on the DSL.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 1, 2024, 2:53pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/2 "2024-10-01T14:53:31Z")

</div>

I don’t have any answer from the Julia side, but I, and all other economists know use the excellent [`fixest`](https://lrberge.github.io/fixest/) package, which has a _much_ more robust DSL and also allows programmatic formula construction. Ideally Julia would look to that better package for a DSL when designing an API.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 1, 2024, 3:16pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/3 "2024-10-01T15:16:44Z")

</div>

A lot of people use and know R’s syntax so I’m guessing that’s why it was used in the GLM / @formula macro.

I find the formula macro to be fine for basic models, fitting a line and such, but if I want anything that’s even slightly beyond that, I immediately turn to Turing.jl

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [October 1, 2024, 4:13pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/4 "2024-10-01T16:13:42Z")

</div>

I know that @rimhajal and @lrnv 's work on [GitHub - JuliaSurv/NetSurvival.jl: A pure-Julia take on standard net survival routines](https://github.com/JuliaSurv/NetSurvival.jl) uses similar syntax – just another data point to add to the discussion here.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [October 1, 2024, 4:32pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/5 "2024-10-01T16:32:38Z")

</div>

Thanks for the ping @TheCedarPrince. Basically, the implementation of `@formula` in Julia is far superior to the old R’s DSL, while allowing the same syntax to be kept and extended. Yes, specificities of the syntax might feel odd to a new user, and no I do not know where it originated 🤣

But do read its docs [there](https://juliastats.org/StatsModels.jl/stable/) they are well written and highlight certain features such as programmatic constructions of formulas (mentioned in this thread for the competition too), and extensibility.

In `JuliaSurv` org, we use it to model survival analysis regression such as hazard regressions, matching the old R syntax once more. Allowing people to switch easily was one of the strength of this decision.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [October 1, 2024, 4:50pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/6 "2024-10-01T16:50:22Z")

</div>

This notation is called Wilkinson notation and isn’t only used by R

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 1, 2024, 5:27pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/7 "2024-10-01T17:27:58Z")

</div>

Thank you! I was googling like a madman trying to find the name, even asking llms but couldn’t find it!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 1, 2024, 5:30pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/8 "2024-10-01T17:30:19Z")

</div>

Peter knows his way around the regression ecosystem in Julia and R (and I assume Stata as well), and without putting words into his mouth what he might have been referring to is that programatically constructing things like fixed effects and instrumental variables is easier in fixest than it is in FixedEffectModels

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 4, 2024, 2:35am UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/9 "2024-10-04T02:35:45Z")

</div>

> [@pdeffebach](#):
>
> I, and all other economists know use the excellent [`fixest`](https://lrberge.github.io/fixest/) package, which has a _much_ more robust DSL and also allows

it seems to be just an extension of the existing syntax

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 4, 2024, 2:31pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/10 "2024-10-04T14:31:10Z")

</div>

Yeah, basically. It just has more features and great documentation. I like it’s syntax for interaction terms and variable slopes.

I don’t actually construct things programmatically in a fancy way though… I just put a lot of effort into string interpolation.

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [October 4, 2024, 5:44pm UTC](https://discourse.julialang.org/t/i-find-rs-dsl-for-defining-regression-a-bit-mind-blowing-glm-jl-and-other-have-tried-to-adopt-them-what-would-an-ideal-dsl-for-defining-regression-look-like-in-julia/120786/11 "2024-10-04T17:44:45Z")

</div>

> [@xiaodai](#):
>
> More examples `y~x^2` is looking for interactions effects of `x` and not fitting a coefficient for `x` squared. To do that you need `y~I(x^2)`.
> 
> I don’t know the origin of R’s regression DSL but I find it strange that Julia’s main package has adopted it since it feels so unintuitive

we actually intentionally do _not_ include this in StatsModels.jl basic formula DSL. that really is just `~`, `+`, `&`, and (much to many folks’ chagrin), `*`.

the design of StatsModels.jl is such that you don’t _need_ to add a bit of syntax to the base DSL in order to able to use it in a specific package. that is, you have to opt-in to any additional syntax by loading a package that defines it. see for instance [RegressionFormulae.jl](https://github.com/kleinschmidt/RegressionFormulae.jl), a tiny package that adds the `/` syntax for “nested interactions” and the `^n` for "all interactions up to the nth degree (the example you called out as wacky).

the syntax extensions can also be _scoped_ by the kind of model that’s been fit, so for instance MixedModels.jl can define `|` to mean one thing when you’re fitting a mixed model without “squatting” on that syntax for users of other packages, _even when they’re loaded simultaneously_.

also, like others have said, the notation is pretty universal (there’s support for it via libraries like [patsy in python](https://patsy.readthedocs.io/en/latest/) and uh some rust crate that I dug up recently but can’t remember now, although that one was pretty primitive, just main effects).

last thing I’d add is that StatsModels.jl via the “programmatic interface” provides very basic building blocks that anyone can build a better DSL on top of if they wish! the DSL is really just a convenience layer, not something that is in any way required or fundamental to the table-to-matrix transformations that are necessary to fit regression and other models.
