# Iteration over 10 columns to calculate LinearRegression

**URL:** <https://discourse.julialang.org/t/iteration-over-10-columns-to-calculate-linearregression/58036>\
**Category:** Machine Learning\
**Created:** [March 26, 2021, 6:14pm UTC](https://discourse.julialang.org/t/iteration-over-10-columns-to-calculate-linearregression/58036 "2021-03-26T18:14:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)\
**Post date:** [March 26, 2021, 6:14pm UTC](https://discourse.julialang.org/t/iteration-over-10-columns-to-calculate-linearregression/58036/1 "2021-03-26T18:14:03Z")

</div>

I have a data frame with 10 columns(features) `COL0 to COL9` and a column `RESP`. How do I calculate a LinearRegression Model for each pair COL0 to COL9 ~ RESP?  
I am expecting to get 10 graphs showing the Model and also a table with the coefficients of my model for each column.

What I tried do far:

```julia
x = df.Col0
y = df.RESP

data = DataFrame(X=x,Y=y)

model = lm(@formula(Y~X),data)

```

And I get what i want for this first pair COl0 ~Resp. But still i need to plot it.

Now i need to do the same steps more 9 times. I want to optimize this as i can have over 100 columns.

I am new to Julia and I really dont have a clue how to get this done. Any help?

Thanks

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 26, 2021, 6:25pm UTC](https://discourse.julialang.org/t/iteration-over-10-columns-to-calculate-linearregression/58036/2 "2021-03-26T18:25:01Z")

</div>

See [Constructing a formula programmatically](https://juliastats.org/StatsModels.jl/stable/formula/#Constructing-a-formula-programmatically) in the StatsModels docs.

---

<div class="post-metadata">

**Author:** ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)\
**Post date:** [March 26, 2021, 7:09pm UTC](https://discourse.julialang.org/t/iteration-over-10-columns-to-calculate-linearregression/58036/3 "2021-03-26T19:09:29Z")

</div>

I got this:

```julia
model2 = fit(LinearModel, @formula(RESP ~EXPL_0 + EXPL_1 + EXPL_2 + EXPL_3 + EXPL_4 + EXPL_5 + EXPL_6 + EXPL_7 + EXPL_8 + EXPL_9 + EXPL_10), df)

```

But i have EXPL\_0 to EXPL\_1000 for example, how can i avoid to type all the EXPL\_0 to EXPL\_1000?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 26, 2021, 8:14pm UTC](https://discourse.julialang.org/t/iteration-over-10-columns-to-calculate-linearregression/58036/4 "2021-03-26T20:14:52Z")

</div>

Did you read the link I sent you? It shows you how to use `term` to construct a term.

You want

```julia
julia> using DataFrames, GLM;

julia> df = DataFrame();

julia> df.y = rand(N);

julia> for i in 0:100
           df[!, "EXPL_$i"] = rand(N)
       end;

julia> lhs = term(:y);

julia> rhs = sum([term(Symbol(:EXPL_, i)) for i in 0:100]);

julia> lm(lhs ~ rhs, df)

```

---

<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:** [March 27, 2021, 6:54am UTC](https://discourse.julialang.org/t/iteration-over-10-columns-to-calculate-linearregression/58036/5 "2021-03-27T06:54:29Z")

</div>

Note that `term` these days accepts strings (I PR’ed this after DataFrames moved to returning strings from `names(df)`), so slightly more concise:

```julia
sum([term("EXPL_$i") for i in 0:100])

```
