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)
println(result)
end
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], …