# 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:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Juan\_Mac\_Donagh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan_mac_donagh/32/31798_2.png) [@Juan\_Mac\_Donagh](https://discourse.julialang.org/u/Juan_Mac_Donagh)
#### Post date: [June 7, 2022, 9:48pm UTC](https://discourse.julialang.org/t/how-to-use-lm-inside-a-for-loop-for-different-variables/82391/1 "2022-06-07T21:48:40Z")

</div>

Hi all, I’ve been trying to work around this problem, and I need a little bit of help.

I have a datarme that looks something like this:

```julia
|Y|X1|X2|X3|X4|X5|
|----------------|
|1|22|23|25|98|23|
|8|52|23|15|28|63|

```

And so on, and I want to evaluate all the linear models between Y and the combinations of 2 of the other columns, for that I am doing this:

```julia
list_comb = collect(combinations(names(list_cols, 2)) #where list_cols contains all the names expect Y

```

But then I really don’t know how to continue.  
I thought about using a loop like this:

```julia
	for i in lista_comb
		lm(@formula(y~ i), df)
	end

```

But this gives the following error: `ArgumentError: There isn't a variable called 'i' in your data; the nearest names appear to be:` (this is also inside a **let** block).

I later want to evaluate the r2 of every combination, so I can use the best one, and I know this is easily done with a list and an append!, but I need some help with this first part.

Thanks a lot!

---

<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))

---

<div class="post-metadata">

### Author: ![Juan\_Mac\_Donagh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan_mac_donagh/32/31798_2.png) [@Juan\_Mac\_Donagh](https://discourse.julialang.org/u/Juan_Mac_Donagh)
#### Post date: [June 8, 2022, 1:34pm UTC](https://discourse.julialang.org/t/how-to-use-lm-inside-a-for-loop-for-different-variables/82391/3 "2022-06-08T13:34:51Z")

</div>

Thanks a lot! I wasn’t familiar with term. After some tinkering, it works great.
