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

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

DF <- data.frame(Site = rep(LETTERS[20:24],each = 4),Region = rep(LETTERS[14:18],time = rep(LETTERS[1:10],each = 10),group = rep(LETTERS[1:4],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",name="",aes(group=group)) +
  geom_smooth(se=T,col="darkorange",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
tammyluxiuliang 回答:stat_fit_glance和广义加性模型(GAM)错误

我的回答解释了为什么无法使用stat_fit_glance()向情节中添加r.sq,但是恐怕它不能提供替代方法。

stat_fit_glance()broom:glance()的包装,适合模型,并将模型拟合对象传递给broom:glance()。对于gam()broom:glance()不会返回R2的估算值,因此stat_fit_glance()也无法返回它。

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

library(ggpmisc)
library(gginnards)
library(mgcv)

DF <- data.frame(Site = rep(LETTERS[20:24],each = 4),Region = rep(LETTERS[14:18],time = rep(LETTERS[1:10],each = 10),group = rep(LETTERS[1:4],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",name="",aes(group=group)) +
  geom_smooth(se=T,col="darkorange",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)

serialize_with

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

注意:关于R平方对GAM是否有意义,似乎尚未达成共识。但是,summary()的{​​{1}}方法确实以成员gam的形式返回调整后的R平方估计。

本文链接:https://www.f2er.com/3069988.html

大家都在问