我想在某些因变量上编程迭代并使用混合模式软件包创建一些公式,因此诸如:
之类的东西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
使用 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)