stat_fit_glance 和广义加性模型 (GAM) 错误

stat_fit_glance and generalized additive models (GAM) error

我正在尝试将 mgcv::gam 结果中的 p 值和 R2 添加到带有小平面的 ggplot 中。示例数据框和代码如下。有没有办法成功地将 p 值和 R2 粘贴到 ggplots 上?

DF <- data.frame(Site = rep(LETTERS[20:24], each = 4),
                 Region = rep(LETTERS[14:18], each = 4),
                 time = rep(LETTERS[1:10], each = 10),
                 group = rep(LETTERS[1:4], each = 10),
                 value1 = runif(n = 1000, min = 10, max = 15),
                 value2 = runif(n = 1000, min = 100, max = 150))
DF$time <- as.numeric(DF$time)


GAMFORMULA <- y ~ s(x,bs="cr",k=3)

plot1 <- ggplot(data=DF, 
                aes(x=time, y=value2)) +
  geom_point(col="gray", alpha=0.8,
             name="") +
  geom_line(col="gray", alpha=0.8,
            name="",aes(group=group)) +
  geom_smooth(se=T, col="darkorange", alpha=0.8,
              name="", fill="orange",
              method="gam",formula=GAMFORMULA) +
  theme_bw() + 
  theme(strip.text.x = element_text(size=10),
        strip.text.y = element_text(size=10, face="bold", angle=0),
        strip.background = element_rect(colour="black", fill="gray90"),
        axis.text.x = element_text(size=10),  # remove x-axis text
        axis.text.y = element_text(size=10), # remove y-axis text
        axis.ticks = element_blank(),  # remove axis ticks
        axis.title.x = element_text(size=18), # remove x-axis labels
        axis.title.y = element_text(size=25), # remove y-axis labels
        panel.background = element_blank(), 
        panel.grid.major = element_blank(),  #remove major-grid labels
        panel.grid.minor = element_blank(),  #remove minor-grid labels
        plot.background = element_blank()) + 
  labs(y="Value", x="Time", title = "") +
  stat_fit_glance(method = "gam",
                  method.args = list(formula = GAMFORMULA),
                  aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
                                      stat(..r.squared..),stat(..p.value..))),
                  parse = TRUE)


plot1 + facet_wrap(Site~group, scales="free_y", ncol=3)

Error in sprintf("R^2~\"=\"~%.3f~~italic(p)~\"=\"~%.2f", r.squared, p.value) : 
  object 'r.squared' not found

我的回答解释了为什么 stat_fit_glance() 不能用于将 r.sq 添加到情节中,但恐怕没有提供替代方法。

stat_fit_glance()broom:glance() 上的包装器,用于拟合模型并将模型拟合对象传递给 broom:glance()。在 gam() 的情况下,broom:glance() 不会 return 对 R2 的估计,因此 stat_fit_glance() 也无法 return 它。

要查看可用的计算值,可以使用包 'gginnards'.

中的 geom_debug()
library(ggpmisc)
library(gginnards)
library(mgcv)

DF <- data.frame(Site = rep(LETTERS[20:24], each = 4),
                 Region = rep(LETTERS[14:18], each = 4),
                 time = rep(LETTERS[1:10], each = 10),
                 group = rep(LETTERS[1:4], each = 10),
                 value1 = runif(n = 1000, min = 10, max = 15),
                 value2 = runif(n = 1000, min = 100, max = 150))
DF$time <- as.numeric(DF$time)


GAMFORMULA <- y ~ s(x,bs="cr",k=3)

plot1 <- ggplot(data=DF, 
                aes(x=time, y=value2)) +
  geom_point(col="gray", alpha=0.8,
             name="") +
  geom_line(col="gray", alpha=0.8,
            name="",aes(group=group)) +
  geom_smooth(se=T, col="darkorange", alpha=0.8,
              name="", fill="orange",
              method="gam",formula=GAMFORMULA) +
  theme_bw() + 
  theme(strip.text.x = element_text(size=10),
        strip.text.y = element_text(size=10, face="bold", angle=0),
        strip.background = element_rect(colour="black", fill="gray90"),
        axis.text.x = element_text(size=10),  # remove x-axis text
        axis.text.y = element_text(size=10), # remove y-axis text
        axis.ticks = element_blank(),  # remove axis ticks
        axis.title.x = element_text(size=18), # remove x-axis labels
        axis.title.y = element_text(size=25), # remove y-axis labels
        panel.background = element_blank(), 
        panel.grid.major = element_blank(),  #remove major-grid labels
        panel.grid.minor = element_blank(),  #remove minor-grid labels
        plot.background = element_blank()) + 
  labs(y="Value", x="Time", title = "") +
  stat_fit_glance(method = "gam",
                  method.args = list(formula = GAMFORMULA),
                  # aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
                  #                     stat(..r.squared..),stat(..p.value..))),
                  # parse = TRUE)
                  geom = "debug")


plot1 + facet_wrap(Site~group, scales="free_y", ncol=3)

上面显示的是图中前两个面板的 return 由 stat_fit_glance() 编辑的值。

注:关于R-square对GAM是否有意义,似乎没有达成一致意见。但是,gamsummary() 方法会 return 作为成员 r.sq.

调整后的 R 方估计