使用 gratia::draw() 函数在 R 中创建一个可编辑的部分效应图,该函数也有一个 rugplot

Creating an editable partial effect plot in R with the gratia::draw() function that also has a rugplot

以下post回答了我的问题:。当我提到 mydraw.gam 函数时,它来自 post 中的代码。我想要做的是使用 mydraw.gam 函数和一个看起来像 gratia::draw() 函数的 rugplot。

这是我的数据:

dput(LMB.stack)
structure(list(X1 = c(0.0541887294548451, 0.0721473880136936, 
0.0175421164050594, 0.0215182766921787, 0.0440735967747106, 0.046669040060852, 
0.0526230550013067, 0.112833597945919, 0.063812034754301, 0.0940158338572872, 
0.0506721208894938, 0.0127474420783362, 0.0657879523145501, 0.0541887294548451, 
0.0721473880136936, 0.0175421164050594, 0.0215182766921787, 0.0440735967747106, 
0.046669040060852, 0.0526230550013067, 0.112833597945919, 0.063812034754301, 
0.0940158338572872, 0.0506721208894938, 0.0127474420783362, 0.0382272328188603, 
0.0541887294548451, 0.0721473880136936, 0.0175421164050594, 0.0215182766921787, 
0.0440735967747106, 0.046669040060852, 0.0526230550013067, 0.112833597945919, 
0.063812034754301, 0.0940158338572872, 0.0506721208894938, 0.0127474420783362, 
0.0657879523145501, 0.0382272328188603, 0.0541887294548451, 0.0721473880136936, 
0.0175421164050594, 0.0215182766921787, 0.0440735967747106, 0.046669040060852, 
0.0526230550013067, 0.0056727211129064, 0.063812034754301, 0.0940158338572872, 
0.106570293080958, 0.116604915677637, 0.0422424508991219, 0.109071218434758, 
0.0666150693773212, 0.108073813949563, 0.0394885672397296, 0.0688845434754768, 
0.0530021292114909, 0.106570293080958, 0.116604915677637, 0.0422424508991219, 
0.109071218434758, 0.0666150693773212, 0.108073813949563, 0.0411444155997384, 
0.0394885672397296, 0.0688845434754768, 0.0530021292114909, 0.106570293080958, 
0.116604915677637, 0.0422424508991219, 0.109071218434758, 0.0666150693773212, 
0.108073813949563, 0.0411444155997384, 0.0394885672397296, 0.0688845434754768, 
0.0530021292114909, 0.0578017962016202, 0.106570293080958, 0.116604915677637, 
0.0422424508991219, 0.109071218434758, 0.0666150693773212, 0.174633119183298, 
0.0645268299068541, 0.0709485215243274, 0.0682173756351461, 0.0643514854635756, 
0.014808611175444, 0.163637352944664, 0.0599393459014399, 0.134349635442672, 
0.214544784680364, 0.0460287439577173, 0.0692001626120574, 0.0682173756351461, 
0.0643514854635756, 0.014808611175444), X2 = c(0.64, 0.47, 0.598, 
0.52, 0.41, 1.38, 0.53, 0.73, 0.367, 0.58, 0.75, 0.38, 0.227, 
0.39, 0.36, 0.35, 0.41, 0.84, 0.53, 0.55, 0.33, 0.33, 0.356, 
0.58, 0.33, 0.52, 0.43, 0.53, 0.45, 0.37, 0.54, 0.98, 0.789, 
0.44, 0.23, 0.21, 0.67144, 0.37, 0.38, 0.18, 0.24, 0.36, 0.37, 
0.16, 0.58, 0.44, 0.41, 0.16, 0.13, 0.55, 0.99, 2.31, 1.264, 
1.005, 1.345, 1.24, 1.665, 1.545, 0.799, 0.736, 1.237, 0.776, 
0.742, 1.0259, 0.66, 1.17, 0.864, 1.191, 0.631, 0.745, 0.866, 
0.917, 1.105, 1.04, 0.517, 1.236, 1.066, 1.35, 0.947, 0.74, 0.62, 
1.572, 0.56, 1.189, 0.645, 0.9, 0.74, 0.568, 1.14, 1.159, 1.325, 
1.217, 1.37, 1.147, 1.89, 1.19, 1.3, 0.73, 0.693, 1.06)), row.names = c(NA, 
100L), class = "data.frame")

这是我的 gam 的样子(使用 mgcv):

LMB.gam<-gam(X2~s(X1), data = LMB.stack)

当我在gratia包中使用draw(LMB.gam)命令时,部分效果图是这样的:

当我使用 mydraw.gam 命令(参见前面的 post)尝试添加地毯图(参见下面的代码)时,我的图如下所示:

p<-mydraw.gam(LMB.gam)
p[[1]] +  geom_rug(position = "jitter",sides="b")

我需要一些帮助来弄清楚如何正确地将地毯图添加到与实际数据相对应的可编辑 gratia::draw ggplot 部分效果图中。

谢谢!

我只会使用 smooth_estimates() 及其 draw() 方法从模型中绘制单个平滑图。然后您可以使用标准 ggplot2 功能添加到它...

# using your data in `df`
m <- gam(X2 ~ s(X1), data = df)
sm <- smooth_estimates(m, smooth = "s(X1)")

draw(sm) +
  labs(title = "My title", y = "foo") +
  geom_rug(data = df,
           mapping = aes(x = X1),
           sides = "b",
           inherit.aes = FALSE)

产生