# Programmatically create formula

**URL:** <https://discourse.julialang.org/t/programmatically-create-formula/136170>\
**Category:** New to Julia\
**Tags:** mixed-models\
**Created:** [March 12, 2026, 6:57pm UTC](https://discourse.julialang.org/t/programmatically-create-formula/136170 "2026-03-12T18:57:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahmah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mahmah/32/214326_2.png) [@mahmah](https://discourse.julialang.org/u/mahmah)\
**Post date:** [March 12, 2026, 6:57pm UTC](https://discourse.julialang.org/t/programmatically-create-formula/136170/1 "2026-03-12T18:57:13Z")

</div>

Hi,

I’m wanting to use this formula programatically:

```julia
f = @formula(var ~ A * B + (1 | id))

```

Here’s what the AI recommends, but it doesn’t seem to work:

```julia
varname = :y # or Symbol("y")
f = @formula($(varname) ~ A * B + (1 | id))

```

Following previous topics here in discourse I also tried `Term(:fname)` but this doesn’t work either, I’m getting this error:

```julia
ERROR: MethodError: no method matching |(::Int64, ::StatsModels.Term)
The function `|` exists, but no method is defined for this combination of argument types.

```

Any suggestions will be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [March 12, 2026, 7:10pm UTC](https://discourse.julialang.org/t/programmatically-create-formula/136170/2 "2026-03-12T19:10:06Z")

</div>

You may need to run

`using MixedModels`

before trying this. If I recall correctly the code to parse expressions like `(1 | id)` is in the MixedModels package, not the `StatsModels` package

And the `MixedModels` package does re-export the `@formula` macro. Thus you do not need to explicitly import the `StatsModels` namespace.

---

<div class="post-metadata">

**Author:** ![mahmah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mahmah/32/214326_2.png) [@mahmah](https://discourse.julialang.org/u/mahmah)\
**Post date:** [March 12, 2026, 7:17pm UTC](https://discourse.julialang.org/t/programmatically-create-formula/136170/3 "2026-03-12T19:17:54Z")

</div>

Thanks, but that doesn’t seem to be the issue.

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [March 12, 2026, 7:25pm UTC](https://discourse.julialang.org/t/programmatically-create-formula/136170/4 "2026-03-12T19:25:20Z")

</div>

Works for me (although you must use `@eval @formula` for substitution of a symbol).

```julia-auto
julia> using MixedModels

julia> varname = :y
:y

julia> form = @eval @formula($(varname) ~ 1 + (1|id))
FormulaTerm
Response:
  y(unknown)
Predictors:
  1
  (id)->1 | id

```

---

<div class="post-metadata">

**Author:** ![mahmah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mahmah/32/214326_2.png) [@mahmah](https://discourse.julialang.org/u/mahmah)\
**Post date:** [March 12, 2026, 7:27pm UTC](https://discourse.julialang.org/t/programmatically-create-formula/136170/5 "2026-03-12T19:27:52Z")

</div>

Thank you so much for this (and for your amazing contributions to academia)!
