- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Effects 包在 Julia 中运行平均边际效应。我的目标是了解不同年龄段男性和女性的体重变化情况。正如您在下面的输出中看到的,它计算了男性和女性每个年龄段的平均边际效应。但是,我想采用一系列年龄变量,而不是单独采用每年。例如,我希望年龄范围为 0:5、5:10、10:15 等等。这必须在运行回归模型之后而不是事先完成。我尝试自己完成它,但我对 Julia 不够流利。
因此唯一需要纠正的行如下:
d1 = Dict(:sex => ["male","female"],:age => [0:5; 6:20])
代码如下:
using DataFrames, Effects, GLM, StatsModels, StableRNGs
rng = StableRNG(42)
growthdata = DataFrame(; age=[13:20; 13:20],
sex=repeat(["male", "female"], inner=8),
weight=[range(100, 155; length=8); range(100, 125; length=8)] .+ randn(rng, 16))
mod_uncentered = lm(@formula(weight ~ 1 + sex * age), growthdata)
d1 = Dict(:sex => ["male","female"],:age => [0:5; 6:20])
ave = effects(d1, mod_uncentered)
输出
sex age weight err lower upper
String Int64 Float64 Float64 Float64 Float64
1 male 0 0.287822 2.88762 -2.5998 3.17545
2 female 0 56.4387 2.88762 53.5511 59.3263
3 male 1 8.00869 2.71603 5.29266 10.7247
4 female 1 59.8481 2.71603 57.1321 62.5641
5 male 2 15.7296 2.54468 13.1849 18.2742
6 female 2 63.2575 2.54468 60.7128 65.8022
7 male 3 23.4504 2.37361 21.0768 25.824
8 female 3 66.6669 2.37361 64.2933 69.0405
9 male 4 31.1713 2.2029 28.9684 33.3742
10 female 4 70.0763 2.2029 67.8734 72.2792
11 male 5 38.8922 2.03264 36.8595 40.9248
12 female 5 73.4857 2.03264 71.4531 75.5184
13 male 6 46.613 1.86295 44.7501 48.476
14 female 6 76.8951 1.86295 75.0322 78.7581
15 male 7 54.3339 1.69399 52.6399 56.0279
16 female 7 80.3046 1.69399 78.6106 81.9985
17 male 8 62.0548 1.52602 60.5288 63.5808
18 female 8 83.714 1.52602 82.1879 85.24
19 male 9 69.7756 1.3594 68.4162 71.135
20 female 9 87.1234 1.3594 85.764 88.4828
21 male 10 77.4965 1.19469 76.3018 78.6912
22 female 10 90.5328 1.19469 89.3381 91.7275
23 male 11 85.2174 1.03282 84.1846 86.2502
24 female 11 93.9422 1.03282 92.9094 94.975
25 male 12 92.9383 0.875345 92.0629 93.8136
26 female 12 97.3516 0.875345 96.4762 98.2269
27 male 13 100.659 0.72515 99.934 101.384
28 female 13 100.761 0.72515 100.036 101.486
29 male 14 108.38 0.587838 107.792 108.968
30 female 14 104.17 0.587838 103.583 1
最佳答案
对于那些熟悉 R 的人来说,Effects.jl 相当于 effects
包,不是 emmeans
包。虽然 effects
和 emmeans
有一定程度的重叠,但 effects
“仅”对预测变量的特定值进行预测,而 emmeans
能够计算预测变量的多个值(例如,范围)的边际平均值。
Effects.jl 本质上是做一些事情的包装器:
vcov
) 的方差-协方差矩阵计算预测和相关的标准误差。请注意,对于混合模型,这意味着只有固定效应起作用。 (这同样适用于在适用于 lme4 的模型中使用 R 中的效果包。)换句话说,Effects.jl 不理解范围,它只理解一组值。它不知道如何预测 0:5
,但它知道如何预测 0
、1
等.
由于您对某个范围内的平均预测感兴趣,因此您可以只计算预测的平均值:
julia> using Statistics
julia> transform!(ave, :age => ByRow(x -> x <= 5 ? "0:5" : "6:20") => :age_bin)
42×7 DataFrame
Row │ sex age weight err lower upper age_bin
│ String Int64 Float64 Float64 Float64 Float64 String
─────┼────────────────────────────────────────────────────────────────────
1 │ male 0 0.287822 2.88762 -2.5998 3.17545 0:5
2 │ female 0 56.4387 2.88762 53.5511 59.3263 0:5
3 │ male 1 8.00869 2.71603 5.29266 10.7247 0:5
4 │ female 1 59.8481 2.71603 57.1321 62.5641 0:5
5 │ male 2 15.7296 2.54468 13.1849 18.2742 0:5
6 │ female 2 63.2575 2.54468 60.7128 65.8022 0:5
7 │ male 3 23.4504 2.37361 21.0768 25.824 0:5
8 │ female 3 66.6669 2.37361 64.2933 69.0405 0:5
9 │ male 4 31.1713 2.2029 28.9684 33.3742 0:5
10 │ female 4 70.0763 2.2029 67.8734 72.2792 0:5
11 │ male 5 38.8922 2.03264 36.8595 40.9248 0:5
12 │ female 5 73.4857 2.03264 71.4531 75.5184 0:5
13 │ male 6 46.613 1.86295 44.7501 48.476 6:20
14 │ female 6 76.8951 1.86295 75.0322 78.7581 6:20
....
julia> rms(x) = sqrt(mean(abs2, x))
rms (generic function with 1 method)
julia> combine(groupby(ave, [:sex, :age_bin]), :weight => mean, :err => rms; renamecols=false)
4×4 DataFrame
Row │ sex age_bin weight err
│ String String Float64 Float64
─────┼────────────────────────────────────
1 │ male 0:5 19.59 2.47686
2 │ female 0:5 64.9622 2.47686
3 │ male 6:20 100.659 1.04247
4 │ female 6:20 100.761 1.04247
对于误差,我使用了均方根 (RMS):换句话说,取相关方差的平均值,然后转换回标准差尺度。 (标准误差是检验统计量的抽样分布的标准偏差。)
对于这个特定的模型(平衡良好的数据,没有讨厌的协变量,没有响应的非线性变换),这与您通过取预测变量的平均值然后计算单个预测得到的预测相同:
julia> d2 = Dict(:sex => ["male","female"],:age => [ mean(0:5); mean(6:20)])
Dict{Symbol, Vector} with 2 entries:
:sex => ["male", "female"]
:age => [2.5, 13.0]
julia> effects(d2, mod_uncentered)
4×6 DataFrame
Row │ sex age weight err lower upper
│ String Float64 Float64 Float64 Float64 Float64
─────┼────────────────────────────────────────────────────────
1 │ male 2.5 19.59 2.4591 17.1309 22.0491
2 │ female 2.5 64.9622 2.4591 62.5031 67.4213
3 │ male 13.0 100.659 0.72515 99.934 101.384
4 │ female 13.0 100.761 0.72515 100.036 101.486
误差稍微小一些,因为这里的误差反射(reflect)了与单个预测相关的不确定性,而上面的误差反射(reflect)了多个预测的不确定性。
关于julia - 如何在 Julia 中创建连续变量的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72201683/
使用 julia 控制台时,您输入如下内容: [10,20]*[1:100,1:100]' 你会得到这样的输出: 2x200 Array{Int64,2}: 10 20 30 40 50
Julia Computing 提供的 Julia 和 Julia Pro 有什么区别? Julia Pro 是否有任何在 Julia 中不可用的企业库? 最佳答案 正如您在 project desc
我最近将我的一个模拟移植到 Julia 中,我仅在运行时发现了几个类型错误。我希望静态分析我的 Julia 代码。 MATLAB 也有类似的问题,只在运行时发现很多错误。 我发现的唯一工具 ( Typ
是否有一种简单的方法来监控 julia 和所有 julia 包的提交和开发?我知道 https://github.com/JuliaLang/julia/commits/master 最佳答案 如果您
我正在从 R 迁移,我使用 head() function很多。我在 Julia 中找不到类似的方法,所以我为 Julia Arrays 写了一个。我还将其他几个 R 函数移植到 Julia。 我需要
在某些语言(如 Python)中,有函数装饰器,它们看起来像宏,位于函数定义之上。装饰器为函数本身提供了一些额外的功能。 Julia 是否以任何方式支持函数装饰器的想法?是否可以使用宏来实现相同的目标
我用Julia中的pmap()函数写了一段并行代码。 然后我在集群上保护了四个核心并运行了一个脚本: julia -p 12 my_parallel_program.jl 我现在应该取消我的工作吗?现
谁能帮我理解接下来的事情: 1)为什么我们需要在制作链表的同时制作一个 future 结构的新抽象类? 2) 为什么有参数 T? 3)这个操作符是干什么的 struct BrokenList
我在 Julia 中有一个数组 Z,它表示二维高斯函数的图像。 IE。 Z[i,j] 是像素 i,j 处的高斯高度。我想确定高斯的参数(均值和协方差),大概是通过某种曲线拟合。 我研究了各种拟合 Z
假设,我们有如下数据结构 struct MyStruct{T} t :: Union{Nothing, T} end 并且我们希望允许用户在不添加任何数据的情况下初始化结构,例如 MyStru
我有一个包含相同类型字段的结构,我无法在创建时分配该字段。 Julia 似乎不喜欢以下内容。 (它吐出一个循环引用投诉。)我打算将问题归结为它的本质 mutable struct Test t
我正在尝试使用最大似然估计 Julia 中的正态线性模型。根据 Optim 文档中关于不更改的值,我使用以下代码通过拦截和匿名函数来模拟该过程: using Optim nobs = 500 nvar
有没有办法从命令行更新 Julia?我浏览了 documentation ,但我找不到任何东西。 最佳答案 我建议尝试 asdf如果您使用的是 MacOS、Linux 或 Linux 的 Window
我想对维度为 n 乘以 n 的矩阵 A 中的所有元素求和。该矩阵是对称的并且对角线上有 0。我发现最快的方法就是求和(A)。然而,这似乎很浪费,因为它没有使用我只需要计算矩阵的下三角这一事实。但是,s
假设你有一个向量元组 $a$,我想在 julia 中定义一个函数 p(x)=x^a。 例如,如果 a=(1,2,3),则结果函数将为 x^1 *y^2 * z^3。 我想为任何元组提供一个通用方法,但
例如,我希望能够按照以下方式做一些事情: abstract Tree abstract SupervisedModel type DecisionTree <: Tree, SupervisedMod
在 Julia 中构建复杂表达式时,是否可以使用列表推导式之类的东西? 例如,假设我有一些符号和类型,并想从它们构建一个类型。现在,我必须做类似的事情。 syms = [:a, :b, :c] typ
在 MATLAB 中,[N,edges,bin] = histcounts (___) 可以获得相应元素的 bin 索引。 Julia 有什么等价的功能吗?谢谢! 我已经尝试过 StatsBase.j
我有一个 Julia 脚本,它反复调用 C++ 程序来执行优化。 C++ 程序写入一个文本文件,然后我让 Julia 读取结果并决定下一步做什么。问题是偶尔(可能是 1000 多次)C++ 程序卡住(
我使用了一些需要特定版本的 Julia 包(即 ≥ v0.3 和 0.4 ≤)。我找不到编译 Julia 的方法来自特定版本的源代码(我正在使用 Linux )。有没有办法做到这一点,我不知道? Gi
我是一名优秀的程序员,十分优秀!