如何在 julia 表达式中将"|"给定符号用于混合模型



我想在某些因变量上编程迭代并使用混合模式软件包创建一些公式,因此诸如:

之类的东西
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

otherstuffthing会更改。如果我没有混合模型,那么我可能会做类似的事情:

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使用 DataFrames.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)

最新更新