为什么 Julia 中的 Effects 包在 MixedModels 输出后返回错误?
Why Effects package in Julia is returning an error following the output of MixedModels?
我想 运行 两个预测变量之间的平均边际效应:教育和失业率。虽然 Julia 中的包 Effects
可以做到这一点,但我遇到了一个错误,我将在下面描述。首先,我展示了运行良好的代码以及我从 运行 宁 MixedModels
包中获得的结果。其次,我展示了代码中不起作用的部分。
using Arrow, Effects, DataFrames, MixedModels, PooledArrays, FreqTables
dffnm = "df4_full"
df = DataFrame(Arrow.Table(dffnm))
df1 = DataFrame(
unemploy = disallowmissing(df.macro_unemployment),
workhours_imputed = df.workhours_imputed,
education = PooledArray(df.education),
id = PooledArray(string.(disallowmissing(df.id))),
age = Int8.(disallowmissing(df.age)) .- Int8(42), # center the age at 42
sex = PooledArray(df.sex),)
form1 = @formula workhours_imputed ~ 1 + sex + age + education + education * unemploy + sex * unemploy + (1|id)
contr = Dict(nm => Grouping() for nm in (:country, :country_year, :id))
contr = Dict(:country => Grouping(),
:country_year => Grouping(),
:id => Grouping(),
:sex => DummyCoding(base="Female"),
:education => DummyCoding(base="Tertiary"))
m1 = @time fit(MixedModel, form1, df1, contrasts=contr)
这是输出:
目前一切正常。目的是 运行 教育和失业对工作时间的平均边际效应。然而,我想我会从一个更简单的例子开始,它只是取教育的平均边际效应(我在这里遵循他们的例子:https://beacon-biosignals.github.io/Effects.jl/stable/#Interaction-Terms-in-Effects):
design = Dict(:education => unique(df1))
effects(design, m1)
错误:
MethodError: no method matching effects!(::Dict{Symbol, DataFrame}, ::LinearMixedModel{Float64})
Closest candidates are:
effects!(::DataFrame, ::StatsBase.RegressionModel; eff_col, err_col, typical) at /Users/jmoawad/.julia/packages/Effects/B3CEy/src/regressionmodel.jl:40
Stacktrace:
[1] top-level scope
@ In[28]:1
[2] eval
@ ./boot.jl:360 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1116
有人可以帮我解决这个问题吗?我也不确定如何 运行 教育和失业率之间相互作用的平均边际效应。
基于@sundar 和@Peter Deffebach(来自另一个线程)的建议。
解决方法代码如下:
你必须在 运行 模型之后过滤缺失的第一个教育:
filter!(!ismissing, df1.education)
然后您在两个变量中选择感兴趣的类别或感兴趣的范围:
design = Dict(:education => ["Tertiary","Below upper-secondary"],:unemploy => [5,10,15,20,25])
然后你使用effects
包:
effects(design, m1)
我想 运行 两个预测变量之间的平均边际效应:教育和失业率。虽然 Julia 中的包 Effects
可以做到这一点,但我遇到了一个错误,我将在下面描述。首先,我展示了运行良好的代码以及我从 运行 宁 MixedModels
包中获得的结果。其次,我展示了代码中不起作用的部分。
using Arrow, Effects, DataFrames, MixedModels, PooledArrays, FreqTables
dffnm = "df4_full"
df = DataFrame(Arrow.Table(dffnm))
df1 = DataFrame(
unemploy = disallowmissing(df.macro_unemployment),
workhours_imputed = df.workhours_imputed,
education = PooledArray(df.education),
id = PooledArray(string.(disallowmissing(df.id))),
age = Int8.(disallowmissing(df.age)) .- Int8(42), # center the age at 42
sex = PooledArray(df.sex),)
form1 = @formula workhours_imputed ~ 1 + sex + age + education + education * unemploy + sex * unemploy + (1|id)
contr = Dict(nm => Grouping() for nm in (:country, :country_year, :id))
contr = Dict(:country => Grouping(),
:country_year => Grouping(),
:id => Grouping(),
:sex => DummyCoding(base="Female"),
:education => DummyCoding(base="Tertiary"))
m1 = @time fit(MixedModel, form1, df1, contrasts=contr)
这是输出:
目前一切正常。目的是 运行 教育和失业对工作时间的平均边际效应。然而,我想我会从一个更简单的例子开始,它只是取教育的平均边际效应(我在这里遵循他们的例子:https://beacon-biosignals.github.io/Effects.jl/stable/#Interaction-Terms-in-Effects):
design = Dict(:education => unique(df1))
effects(design, m1)
错误:
MethodError: no method matching effects!(::Dict{Symbol, DataFrame}, ::LinearMixedModel{Float64})
Closest candidates are:
effects!(::DataFrame, ::StatsBase.RegressionModel; eff_col, err_col, typical) at /Users/jmoawad/.julia/packages/Effects/B3CEy/src/regressionmodel.jl:40
Stacktrace:
[1] top-level scope
@ In[28]:1
[2] eval
@ ./boot.jl:360 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1116
有人可以帮我解决这个问题吗?我也不确定如何 运行 教育和失业率之间相互作用的平均边际效应。
基于@sundar 和@Peter Deffebach(来自另一个线程)的建议。
解决方法代码如下:
你必须在 运行 模型之后过滤缺失的第一个教育:
filter!(!ismissing, df1.education)
然后您在两个变量中选择感兴趣的类别或感兴趣的范围:
design = Dict(:education => ["Tertiary","Below upper-secondary"],:unemploy => [5,10,15,20,25])
然后你使用effects
包:
effects(design, m1)