如何使用|&引用;julia表达式中混合模型的给定符号

如何使用|&引用;julia表达式中混合模型的给定符号,julia,Julia,我想以编程方式迭代一些因变量,并使用MixedModels包创建一些公式,例如: form = @formula(y ~ 1 + x1 + x2 + (1 + x1 + x2 | stuff)) 理想情况下,我会为因变量设置一个符号数组,并对其进行迭代,创建不同类型的公式: depvars = [:height, :weight] for var in depvars my_formula = @formula(var ~ otherstuff + (1 + otherstuff |

我想以编程方式迭代一些因变量,并使用MixedModels包创建一些公式,例如:

form = @formula(y ~ 1 + x1 + x2 + (1 + x1 + x2 | stuff))
理想情况下,我会为因变量设置一个符号数组,并对其进行迭代,创建不同类型的公式:

depvars = [:height, :weight]

for var in depvars
    my_formula = @formula(var ~ otherstuff + (1 + otherstuff | thing))
    lm_out = fit!(lmm(my_formula, data))
end
其中
otherstuff
thing
将发生变化。如果我没有混合模型,那么我可能会这样做:

depvars = [:height, :weight]
f1 = Expr(:call, :+, [:x1, :x2])
f2 = Expr(:call, :+, [:x3, :x4])

for var in depvars
    my_formula1 = Formula(var, f1)
    my_formula2 = Formula(var, f2)
    lm_out = lm(my_formula1, data)
    # and so on...
end
但是我不确定我是否可以用表达式来表达
(1+x1+x2 | stuff)


这可能吗

而不是使用
@formula
直接使用
数据帧。formula
和引用表达式中的插值:

using MixedModels, DataFrames

depvars = [:height, :weight]
things = [:thing1, :thing2]
otherstuffs = [:other1, :other2]

for thing in things, otherstuff in otherstuffs
    for var in depvars
        my_formula = Formula(var, :( $otherstuff + (1 +$otherstuff | $thing)))
        @show my_formula
    end
end
给出:

my_formula = Formula: height ~ other1 + ((1 + other1) | thing1)
my_formula = Formula: weight ~ other1 + ((1 + other1) | thing1)
my_formula = Formula: height ~ other2 + ((1 + other2) | thing1)
my_formula = Formula: weight ~ other2 + ((1 + other2) | thing1)
my_formula = Formula: height ~ other1 + ((1 + other1) | thing2)
my_formula = Formula: weight ~ other1 + ((1 + other1) | thing2)
my_formula = Formula: height ~ other2 + ((1 + other2) | thing2)
my_formula = Formula: weight ~ other2 + ((1 + other2) | thing2)