# Using GLM programmatically

**URL:** https://discourse.julialang.org/t/using-glm-programmatically/30828
**Category:** General Usage
**Tags:** question, metaprogramming, glm
**Created:** [November 7, 2019, 1:19pm UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828 "2019-11-07T13:19:35Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [November 7, 2019, 1:19pm UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828/1 "2019-11-07T13:19:35Z")

</div>

I would like to perform linear regression in a loop with variables. For example,

```julia
using DataFrames, GLM

data = DataFrame(X=[1,2,3], Y=[2,4,7])
var1 = :Y 
var2 = :X 
ols = lm(@formula($var1 ~ $var2), data)

```

I tried various approaches including the one proposed [here](https://discourse.julialang.org/t/iterating-on-regressions-using-glm/6686/3), which no longer works.

---

<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: [November 7, 2019, 1:26pm UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828/2 "2019-11-07T13:26:18Z")

</div>

Just add `@eval` in front

```julia
ols = lm(@eval(@formula($var1 ~ $var2)), data)

```

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [November 7, 2019, 1:29pm UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828/3 "2019-11-07T13:29:13Z")

</div>

> [@xiaodai](#):
>
> ols = lm(@eval(@formula($var1 ~ $var2)), data)

Thank you! I tried something similar:

`ols = lm(@eval @formula($var1 ~ $var2) , data)`

I didn’t realize the parentheses were necessary.

---

<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: [November 7, 2019, 1:30pm UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828/4 "2019-11-07T13:30:24Z")

</div>

Ah, if you don’t add parenthesis then `@eval @formula($var1 ~ $var2) , data` doesn’t make sense for `@eval`, cos of the `,` which is captured by `@eval`.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [November 7, 2019, 1:31pm UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828/5 "2019-11-07T13:31:55Z")

</div>

Thanks for the explanation. At some point I should really learn how macros work.

---

<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: [November 7, 2019, 1:39pm UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828/6 "2019-11-07T13:39:02Z")

</div>

Just to add that the recommended approach to programmatically generating formula terms is by constructing the Term objects directly, as referenced in the StatsModels API here: [Modeling tabular data · StatsModels.jl](https://juliastats.org/StatsModels.jl/stable/formula/#Constructing-a-formula-programmatically-1)

---

<div class="post-metadata">

### Author: ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)
#### Post date: [October 8, 2024, 7:39am UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828/7 "2024-10-08T07:39:42Z")

</div>

I have a question: I need to have a squared term in the formula. I got the thing with `log` in the example [here](https://juliastats.org/StatsModels.jl/stable/formula/#Constructing-a-FunctionTerm-programmatically):

```julia
julia> log_term(t::AbstractTerm) = FunctionTerm(log, [t], :(log($(t))))

julia> lt = log_term(term(:a))
(a)->log(a)

julia> lt.f(9)
2.1972245773362196

```

but how do I use this to make a squared term?

```julia
julia> square_term(t::AbstractTerm) = FunctionTerm(^, [t], :(^($t,2)))
square_term (generic function with 2 methods)

julia> st = square_term(term(:b))
(b)->b ^ 2

julia> st.f(2)
ERROR: MethodError: no method matching ^(::Int64)

Closest candidates are:
  ^(::Integer, ::BigInt)
   @ Base gmp.jl:655
  ^(::Integer, ::Bool)
   @ Base bool.jl:170
  ^(::T, ::T) where T<:Integer
   @ Base intfuncs.jl:310
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[135]:1

julia> st.f(2,3)
8

```

I don’t get 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 8, 2024, 7:59am UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828/8 "2024-10-08T07:59:16Z")

</div>

I can have a quick look later to see if I can figure it out, but isn’t that the example in the docs?

[https://juliastats.org/StatsModels.jl/stable/internals/#An-example-of-custom-syntax:-poly](https://juliastats.org/StatsModels.jl/stable/internals/#An-example-of-custom-syntax:-poly)

---

<div class="post-metadata">

### Author: ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)
#### Post date: [October 8, 2024, 8:13am UTC](https://discourse.julialang.org/t/using-glm-programmatically/30828/9 "2024-10-08T08:13:07Z")

</div>

yes that looks exactly like what I need. I think we should add the sentence to this example with the log function - I had no incentive to look any further, given that example.

> this is going to work only in cases with non-ambigous dispatch, for example, the log example below. For functions with multiple arguments you will need to extent the formula syntax as explained [there](https://juliastats.org/StatsModels.jl/stable/internals/#extending)
