How to create a loop for a regression model?

Hi everyone, I’m new to Julia and I’m trying to create a loop for a regression model.
For example,

list = [:A, :B, :C]
for i in list
    result = lm(@formula(i ~ X1 + X2), data)

Something like this. But I get an error.

type NamedTuple has no field i

Can someone teach me how to fix it?
Thank you.

I could solve it by not using the macro “@formula”. Instead,

list = [:A, :B, :C]
for i in list
result = lm(StatsModels.Term(i) ~ StatsModels.Term(:X1) + StatsModels.Term(:X2), data)

Please let me know if you have any other solution to this.

In the expression @formula(i ~ X1 + X2) you have to specify that i is not a column name in your data, but that it contains a column name… You can do it like this:

for i in list
    result = lm((@eval @formula($i ~ X1 + X2)), data)

Thank you! It works.

Do you also have any idea of how to save the results for each regression?
For example, result1, result2, …

You can collect them using array comprehension:

using DataFrames, GLM
data = DataFrame(X1=[1,2,3,4], X2=[2,4,7,8], A=[4,8,13,17], B=[10,11,12,13]);
list = [:A, :B];
results = [lm((@eval @formula($i ~ X1 + X2)), data) for i in list]

This gives you results in results[1], results[2], …

1 Like

The recommended way of constructing formulas programmatically is to use Terms (or the term function) like so:

[lm(Term(i) ~ Term(:X1) + Term(:X2), data) for i in list]

this is discussed in the docs here.


@nilshg Thank you! :slight_smile:

@sijo Thank you :slight_smile:

2 posts were split to a new topic: Regression with variables in array