0

我正在尝试将 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
4

1 回答 1

1

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

stat_fit_glance()broom:glance()是适合模型并将模型适合对象传递给的包装器broom:glance()。在 的情况下gam()broom:glance()不返回 R2 的估计值,因此也stat_fit_glance()无法返回它。

要查看可用的计算值,可以geom_debug()从包“gginnards”中使用。

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)

在此处输入图像描述

stat_fit_glance()上面显示的是图中前两个面板返回的值。

注意:对于 R-square 是否对 GAM 有意义,似乎没有一致意见。但是,该summary()方法gam确实将调整后的 R 平方估计值返回为 member r.sq

于 2019-11-20T15:53:41.990 回答