# How to use lm inside a for loop for different variables

**URL:** <https://discourse.julialang.org/t/how-to-use-lm-inside-a-for-loop-for-different-variables/82391>\
**Category:** Statistics\
**Created:** [June 7, 2022, 9:48pm UTC](https://discourse.julialang.org/t/how-to-use-lm-inside-a-for-loop-for-different-variables/82391 "2022-06-07T21:48:39Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [June 8, 2022, 4:45am UTC](https://discourse.julialang.org/t/how-to-use-lm-inside-a-for-loop-for-different-variables/82391/2 "2022-06-08T04:45:03Z")

</div>

> [@How to get a GLM where formula is programmatically generated](https://discourse.julialang.org/t/how-to-get-a-glm-where-formula-is-programmatically-generated/43519/3):
>
> Cameron is exactly right. These two forms are exactly equivalent: julia\> using StatsModels julia\> (Term(:y) ~ Term(:x1) + Term(:x2)) == @formula(y ~ x1 + x2) true As @nilshg pointed out in [this post](https://discourse.julialang.org/t/using-all-independent-variables-with-formula-in-a-multiple-linear-model/43691/5), this is actually the expression that’s generated by the formula macro: julia\> @macroexpand @formula(y ~ x1 + x2) :(StatsModels.Term(:y) ~ StatsModels.Term(:x1) + StatsModels.Term(:x2))

---

_[View the full topic](https://discourse.julialang.org/t/how-to-use-lm-inside-a-for-loop-for-different-variables/82391)._
