# Iterating on regressions using GLM

#1

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:

``````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!

#2

Take a look at the `@eval` macro

``````julia> x = 1
1

julia> @eval \$x + 4
5
``````

#3

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

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

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

``````ERROR: UndefVarError: data2 not defined
``````

What’s going on?

#4

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

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

``````julia> i = 1
1

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

#5

Thanks, that worked!