# Iterating on regressions using GLM

**URL:** <https://discourse.julialang.org/t/iterating-on-regressions-using-glm/6686>\
**Category:** General Usage\
**Tags:** question, glm\
**Created:** [October 25, 2017, 7:52pm UTC](https://discourse.julialang.org/t/iterating-on-regressions-using-glm/6686 "2017-10-25T19:52:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathtd](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@mathtd](https://discourse.julialang.org/u/mathtd)\
**Post date:** [October 25, 2017, 7:52pm UTC](https://discourse.julialang.org/t/iterating-on-regressions-using-glm/6686/1 "2017-10-25T19:52:19Z")

</div>

I have a large number of OLS regressions to run. I’m hoping to do this using the GLM package but I need to find a way to iterate over regressions easily. I’m hoping to do something like the following:

```julia
for i=1:n
    reg_temp = glm(@formula(Z$i ~ X$i + Y$i), data, Normal(), IdentityLink())
end 

```

where $i would operate like with strings. Is there anyway to do this easily? Maybe using a dictionary? Many thanks!

---

<div class="post-metadata">

**Author:** ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)\
**Post date:** [October 25, 2017, 8:01pm UTC](https://discourse.julialang.org/t/iterating-on-regressions-using-glm/6686/2 "2017-10-25T20:01:19Z")

</div>

Take a look at the `@eval` macro

```julia
julia> x = 1
1

julia> @eval $x + 4
5

```

---

<div class="post-metadata">

**Author:** ![mathtd](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@mathtd](https://discourse.julialang.org/u/mathtd)\
**Post date:** [October 25, 2017, 9:52pm UTC](https://discourse.julialang.org/t/iterating-on-regressions-using-glm/6686/3 "2017-10-25T21:52:32Z")

</div>

Ok this was helpful but I’m very confused by the whole thing. If I run

```julia
data = DataFrame(x=[2,3],y=[3,6])
r = :x
@eval temp=glm(@formula(y ~ $r), data, Normal(), IdentityLink())

```

everything works, no problem. However, the following throws an error

```julia
function foo()
       data2 = DataFrame(x=[2,3],y=[3,6])
       r2 = :x
       @eval temp=glm(@formula(y ~ $r2), data2, Normal(), IdentityLink())
end
foo()

```

The outcome is

```julia
ERROR: UndefVarError: data2 not defined

```

What’s going on?

---

<div class="post-metadata">

**Author:** ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)\
**Post date:** [October 25, 2017, 10:17pm UTC](https://discourse.julialang.org/t/iterating-on-regressions-using-glm/6686/4 "2017-10-25T22:17:14Z")

</div>

`eval` and `@eval` only evaluate in global scope.

You could also try working with the `Formula` type directly rather than `@formula`.

```julia
julia> i = 1
1

julia> Formula(:y, Symbol("x$i"))
Formula: y ~ x1

```

---

<div class="post-metadata">

**Author:** ![mathtd](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@mathtd](https://discourse.julialang.org/u/mathtd)\
**Post date:** [October 26, 2017, 12:29am UTC](https://discourse.julialang.org/t/iterating-on-regressions-using-glm/6686/5 "2017-10-26T00:29:24Z")

</div>

> [@joshday](#):
>
> Formula(:y, Symbol(“x$i”))

Thanks, that worked!
