在 gratia::draw(parametric_effects()) 中获取参数项的正确顺序。

huangapple go评论114阅读模式
英文:

Getting the correct order of factors for parametric terms in gratia::draw(parametric_effects())

问题

我有一个数据集:

df <- structure(list(Depth = c(6.84, 8.56, 4.65, 8.84, 6.56, 8.65, 12.21, 11.82, 5.41, 11.63, 9.41, 11.26, 8.96, 10.81, 10.68, 12.74, 14.06, 8.16, 12.31, 10.76, 10.74, 1, 9.38, 5, 4, 12, 6.71, 8.56, 14.65, 16.71, 12.56, 18.65, 20.21, 11.82, 13.41, 13.63, 13.41, 13.26, 22.96, 14.81, 20.74, 30.06, 30.16, 32.31, 32.21, 14.76, 14.74, 4.66, 10, 4, 15, 8.71, 32.65, 26.21, 29.82, 29.41, 5.63, 23.41, 29.26, 2.96, 2.81, 2.68, 2.74, 2.06, 2.16, 2.31, 4.21, 8.76, 2.74, 18.66, 3, 4, 20, 6.84, 1, 6.65, 6.21, 1.82, 1.41, 3.63, 3.41, 5.26, 2.96, 2.81, 1, 2.74, 4.06, 4.16, 4.31, 4.21, 2.76, 2.74, 1, 5, 3, 4.71, 2.56, 2.65, 10.21, 7.82), NEAR_DIST = c(18.78, 18.3, 61.36, 179.28, 10.43, 17.92, 46.21, 31.99, 10.43, 26.72, 24.48, 33.09, 27.27, 43.42, 46.07, 50.12, 47.4, 64.44, 49.67, 12.13, 15.13, 25.02, 10.46, 13.46, 16.89, 13.52, 32.51, 38.24, 38.55, 82.06, 49.57, 90.64, 83.62, 49.74, 397.27, 53.49, 68.02, 59.66, 130.75, 67.27, 111.3, 347.36, 220.52, 268.56, 194.92, 84.49, 57.13, 24.36, 54.84, 18.74, 66.64, 203.71, 829.38, 309.42, 395.5, 326.77, 35.65, 264.24, 307.03, 23.02, 26.37, 22.93, 25.28, 15.5, 14.6, 13.37, 11.95, 152.75, 11.3, 74.37, 3.77, 6.83, 79.4, 38.85, 3.85, 34.87, 38.03, 21.06, 20.76, 37.61, 25.72, 41.95, 26.17, 26.17, 16.49, 29.13, 29.13, 25.21, 27.63, 25.21, 21.06, 18.6, 11.09, 19.93, 16.25, 18.53, 5.58, 10.12, 56.3, 36.03), Season2 = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), levels = c("Winter", "Spring", "Summer", "Fall"), class = c("ordered", "factor"))), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"))

并且正在使用数据运行一个 gam 模型:

```R
library(mgcv)
library(gratia)
gam <- gam(Depth ~ s(NEAR_DIST) + Season2, data = df)

尽管 Season2 变量是有序的:

unique(df$Season2)
[1] Summer Fall   Winter Spring
Levels: Winter < Spring < Summer < Fall

但当我调用以下命令时:

draw(parametric_effects(gam))

x轴的顺序是按字母顺序的。如何才能让x轴的顺序与我的因子一致呢?旧版本的 gratia 以前可以做到这一点。我使用的版本

英文:

I have a data set:

   df&lt;- structure(list(Depth = c(6.83999999999997, 8.56, 4.64999999999998, 
8.83999999999997, 6.56, 8.64999999999998, 12.21, 11.82, 5.41000000000003, 
11.63, 9.41000000000003, 11.26, 8.95999999999998, 10.81, 10.68, 
12.74, 14.06, 8.16000000000003, 12.31, 10.76, 10.74, 1, 9.38, 
5, 4, 12, 6.70999999999998, 8.56, 14.65, 16.71, 12.56, 18.65, 
20.21, 11.82, 13.41, 13.63, 13.41, 13.26, 22.96, 14.81, 20.74, 
30.06, 30.16, 32.31, 32.21, 14.76, 14.74, 4.66000000000003, 10, 
4, 15, 8.70999999999998, 32.65, 26.21, 29.82, 29.41, 5.63, 23.41, 
29.26, 2.95999999999998, 2.81, 2.68000000000001, 2.74000000000001, 
2.06, 2.16000000000003, 2.31, 4.20999999999998, 8.75999999999999, 
2.74000000000001, 18.66, 3, 4, 20, 6.83999999999997, 1, 6.64999999999998, 
6.20999999999998, 1.81999999999999, 1.41000000000003, 3.63, 3.41000000000003, 
5.25999999999999, 2.95999999999998, 2.81, 1, 2.74000000000001, 
4.06, 4.16000000000003, 4.31, 4.20999999999998, 2.75999999999999, 
2.74000000000001, 1, 5, 3, 4.70999999999998, 2.56, 2.64999999999998, 
10.21, 7.81999999999999), NEAR_DIST = c(18.77925552, 18.30180262, 
61.36019078, 179.2770495, 10.43166516, 17.9171804, 46.20571245, 
31.99340507, 10.43166516, 26.7170903, 24.47782541, 33.08965222, 
27.27138524, 43.4212158, 46.0670014, 50.11661352, 47.39692573, 
64.4374351, 49.66872737, 12.12884673, 15.13068812, 25.02246826, 
10.46189005, 13.46373164, 16.89230952, 13.51981867, 32.50661183, 
38.24201162, 38.5502434, 82.06185032, 49.57486607, 90.64395203, 
83.61730031, 49.74483449, 397.2686612, 53.49338859, 68.02475678, 
59.6583949, 130.7528811, 67.27058895, 111.2988217, 347.3593823, 
220.5169227, 268.5649787, 194.9220113, 84.48739079, 57.1344938, 
24.35529161, 54.84148996, 18.74063124, 66.63864028, 203.7119682, 
829.3788162, 309.4190672, 395.4959263, 326.7671063, 35.65309711, 
264.2374189, 307.025746, 23.02085763, 26.3683775, 22.93486062, 
25.28307029, 15.49632807, 14.59667995, 13.36925569, 11.9476145, 
152.7517309, 11.30381957, 74.36911773, 3.773174432, 6.825998674, 
79.40020637, 38.8451901, 3.853365482, 34.8719427, 38.02805106, 
21.06138328, 20.76016614, 37.60511548, 25.71672169, 41.9543577, 
26.1675823, 26.1675823, 16.49388675, 29.12695505, 29.12695505, 
25.21064884, 27.6250245, 25.21064884, 21.06138328, 18.59893184, 
11.08799823, 19.92747995, 16.25210115, 18.52964249, 5.582718512, 
10.11944373, 56.29794875, 36.03064946), Season2 = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L), levels = c(&quot;Winter&quot;, &quot;Spring&quot;, &quot;Summer&quot;, &quot;Fall&quot;), class = c(&quot;ordered&quot;, 
&quot;factor&quot;))), row.names = c(NA, -100L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, 
&quot;data.frame&quot;))

and am running a gam with the data:

library(mgcv)
library(gratia)
gam&lt;-gam(Depth~s(NEAR_DIST)+Season2,data=df)

even though the Season2 variable is ordered:

unique(df$Season2)
[1] Summer Fall   Winter Spring
Levels: Winter &lt; Spring &lt; Summer &lt; Fall

when I call:

draw(parametric_effects(gam))

The order of the x-axis is alphabetical.
在 gratia::draw(parametric_effects()) 中获取参数项的正确顺序。

How can I get the x-axis to match the order of my factor here? The old version of gratia used to do this. I have: version 0.8.1.34

答案1

得分: 2

作为解决方法或修复问题的一种方法,您可以使用 scale_x_discretelimits 参数来设置顺序:

library(mgcv)
#&gt; Loading required package: nlme
#&gt; This is mgcv 1.8-42. For overview type &#39;help(&quot;mgcv-package&quot;)&#39;.
library(gratia)
library(ggplot2)

packageVersion(&quot;gratia&quot;)
#&gt; [1] &#39;0.8.1.34&#39;

gam &lt;- gam(Depth ~ s(NEAR_DIST) + Season2, data = df)

draw(parametric_effects(gam)) +
  ggplot2::scale_x_discrete(limits = levels(df$Season2))

在 gratia::draw(parametric_effects()) 中获取参数项的正确顺序。<!-- -->

英文:

As a workaround or fix for your issue you could set the order using the limits argument of scale_x_discrete:

library(mgcv)
#&gt; Loading required package: nlme
#&gt; This is mgcv 1.8-42. For overview type &#39;help(&quot;mgcv-package&quot;)&#39;.
library(gratia)
library(ggplot2)

packageVersion(&quot;gratia&quot;)
#&gt; [1] &#39;0.8.1.34&#39;

gam &lt;- gam(Depth ~ s(NEAR_DIST) + Season2, data = df)

draw(parametric_effects(gam)) +
  ggplot2::scale_x_discrete(limits = levels(df$Season2))

在 gratia::draw(parametric_effects()) 中获取参数项的正确顺序。<!-- -->

huangapple
  • 本文由 发表于 2023年6月1日 22:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定