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 | 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)

关于julia - 如何在 Julia 表达式中使用混合模型的 "|"给定符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47401643/

相关文章:

julia - 如何检查字符串是否为空?

julia - Julia : copy (pasteable and "human-readable") value to clipboard

parallel-processing - Julia:远程工作人员的 SharedArray 变成一个 0 元素数组

arrays - 删除长度为 1 的 Julia 数组维度

printing - 在 Julia 上同时写入多个文件

dataframe - 如何从 Julia 的数据框中获取多个直方图

matplotlib - Julia PyPlot : plot 3D surface with as face colors the norm of surface gradient

julia - 稀疏矩阵转置的缓慢乘法

julia - 根据是否存在可选关键字参数设置函数输出类型

julia - Pluto.jl 中的打印语句