# UndefVarError: @formula not defined

**URL:** <https://discourse.julialang.org/t/undefvarerror-formula-not-defined/97243>\
**Category:** New to Julia\
**Tags:** question, dataframes\
**Created:** [April 8, 2023, 5:35am UTC](https://discourse.julialang.org/t/undefvarerror-formula-not-defined/97243 "2023-04-08T05:35:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nirong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nirong/32/48859_2.png) [@nirong](https://discourse.julialang.org/u/nirong)\
**Post date:** [April 8, 2023, 5:35am UTC](https://discourse.julialang.org/t/undefvarerror-formula-not-defined/97243/1 "2023-04-08T05:35:01Z")

</div>

I am using Julia version1.8.5. There was an error " LoadError: UndefVarError: @formula not defined" when I ran "fmla = @formula(outcome ~ GP(X1, X2) | region + covarA + covarB + categ)” in julia. The code and error is showed in the following picture. What should I do to deal with it ? I’ll really appreciated it if anyone can help me out.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/0/707b309048f7199301b6e1baadea329ba8b5d133.png)

---

<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:** [April 8, 2023, 8:53am UTC](https://discourse.julialang.org/t/undefvarerror-formula-not-defined/97243/2 "2023-04-08T08:53:40Z")

</div>

You need to actually load a package which defines a `@formula` macro - judging from your regression specification you might be looking for `MixedModels`

---

<div class="post-metadata">

**Author:** ![nirong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nirong/32/48859_2.png) [@nirong](https://discourse.julialang.org/u/nirong)\
**Post date:** [April 8, 2023, 9:52am UTC](https://discourse.julialang.org/t/undefvarerror-formula-not-defined/97243/3 "2023-04-08T09:52:56Z")

</div>

Thanks for your answer. But there are still few problems that I am confused. First, I don’t know how to find the package which @formula belongs to. Second, besides MixedModels, I also find running “using StatsModels” before using @formula also works, but I don’t know the dfference between MixedModels and StatsModels or other packages including @formula. Which one should I choose. I will appreciated it if you can help to explain 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:** [April 8, 2023, 10:35am UTC](https://discourse.julialang.org/t/undefvarerror-formula-not-defined/97243/4 "2023-04-08T10:35:41Z")

</div>

How did you know how to write `@formula`, and how did you know to specify your regression the way you did (ie using the `|` random effects operator)?

It is true that the are multiple packages which use the `@formula` macro, `StatsModels` is the base package defining the `@formula` DSL but many other packages use and extend it. The question of which one to use can’t be answered on generally, it’s up to you to decide based on the problem at hand, eg got a plain vanilla OLS linear model or probit you might use `GLM`, if you have high dimensional fixed effects you might use `FixedEffectModels`, if you want to fit a mixed model then you’re probably interested in `MixedModels` etc

---

<div class="post-metadata">

**Author:** ![nirong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nirong/32/48859_2.png) [@nirong](https://discourse.julialang.org/u/nirong)\
**Post date:** [April 8, 2023, 10:46am UTC](https://discourse.julialang.org/t/undefvarerror-formula-not-defined/97243/5 "2023-04-08T10:46:11Z")

</div>

Thanks for your detailed explanation. It really helps me.
