How to get a GLM where formula is programmatically generated

Cameron is exactly right. These two forms are exactly equivalent:

julia> using StatsModels

julia> (Term(:y) ~ Term(:x1) + Term(:x2)) == @formula(y ~ x1 + x2)
true

As @nilshg pointed out in this post, this is actually the expression that’s generated by the formula macro:

julia> @macroexpand @formula(y ~ x1 + x2)
:(StatsModels.Term(:y) ~ StatsModels.Term(:x1) + StatsModels.Term(:x2))
2 Likes