【问题标题】:Adding R^2 and P-value in ggpairs (GGally) composed of LM and GAM scatterplot在由LM和GAM散点图组成的ggpairs(GGally)中添加R^2和P值
【发布时间】:2021-12-15 14:29:27
【问题描述】:

我是 R 和 stackoverflow 的新手

我一直在寻找在配对图中呈现 R^2 和 P 值的代码,特别是对于 gam(上)和 lm(下)。但是,我坚持使用此代码:

library(tidyverse) # plotting and manipulation
library(grid) # combining plots
library(gridExtra) # combining plots
library(ggpubr) # combining plots
library(patchwork) # combining plots
library(ggfortify) # nice extension for ggplot
library(mgcv) #fitting gam models
library(GGally) # displaying pairs panel

sel1<-select(rekap,c('Total_Ni', 'Total_Mg', 'Total_Fe', 'CEC', 'pH','SWC'))## selecting column

str(sel1)
tibble [36 x 6] (S3: tbl_df/tbl/data.frame)
 $ Total_Ni: num [1:36] 1750 1565 1249 853 959 ...
 $ Total_Mg: num [1:36] 1468 1558 1164 813 915 ...
 $ Total_Fe: num [1:36] 381 300 172 173 144 ...
 $ CEC     : num [1:36] 105 132 117 118 141 ...
 $ pH      : num [1:36] 4.21 4.22 4.49 4.43 4.05 4.09 5.21 5.27 4.32 4.29 ...
 $ SWC     : num [1:36] 435 511 497 517 621 ...

## Build function for upper and lower plots
my_fn1 <- function(data, mapping, method="gam", ...){
      p <- ggplot(data = sel1, mapping = mapping) + 
      geom_point() + 
      geom_smooth(method=method,colour="blue", ...)
      p
    }       
my_fn2 <- function(data, mapping, method="lm", ...){
      p2 <- ggplot(data = sel1, mapping = mapping) + 
      geom_point() + 
      geom_smooth(method=method,colour="orangered2", ...)
      p2
    }   

##Pairing with ggpairs
p1 <- ggpairs(sel1, columnLabels = c("Total Ni", "Total Mg", "Total Fe", "CEC", "pH","SWC"),
            upper=list(continuous =my_fn1),
            lower=list(continuous =my_fn2))+ 
            theme_bw() + theme(axis.text.x=(element_text(size=rel(0.7), angle=0)),
            axis.text.y=(element_text(size=rel(0.7), angle=0)), panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(), panel.border = element_rect(fill = NA,colour = "grey35"))   

这里的结果, Rplot

我一直在尝试构造更合适的代码,但没有成功。

【问题讨论】:

    标签: r p-value gam ggally ggpairs


    【解决方案1】:

    更新: 这是一个起点,而不是完整的解决方案。主要问题是要访问 gam 模型中的 R² 和 p.value。 不幸的是,在我的解决方案中,lm 模型中的 R² 和 p 应用于 gam 模型。

    经过一番研究,我发现了这个Getting adjusted r-squared value for each line in a geom_smooth gam

    stat_fit_glance and generalized additive models (GAM) error.

    我将 sel1 替换为模拟数据: 我们正在使用来自ggpubrstat_cor

    library(tidyverse) # plotting and manipulation
    library(grid) # combining plots
    library(gridExtra) # combining plots
    library(ggpubr) # combining plots
    library(patchwork) # combining plots
    library(ggfortify) # nice extension for ggplot
    library(mgcv) #fitting gam models
    library(GGally) # displaying pairs panel
    
    #sel1<-select(rekap,c('Total_Ni', 'Total_Mg', 'Total_Fe', 'CEC', 'pH','SWC'))## selecting column
    mtcars1 <- mtcars %>% select(1:6)
    
    mtcars1
    ## Build function for upper and lower plots
    my_fn1 <- function(data, mapping, method="gam", ...){
      p <- ggplot(data = mtcars1, mapping = mapping) + 
        geom_point() + 
        geom_smooth(method=method,colour="blue", ...)+
        stat_cor(aes(label=paste("Pearson",..rr.label.., ..p.label.., sep = "~`,`~")), 
                 method="pearson", label.y = 0)
      p
    }  
    
    my_fn2 <- function(data, mapping, method="lm", ...){
      p2 <- ggplot(data = mtcars1, mapping = mapping) + 
        geom_point() + 
        geom_smooth(method=method,colour="orangered2", ...)+
        stat_cor(aes(label=paste("Pearson",..rr.label.., ..p.label.., sep = "~`,`~")), 
                 method="pearson", label.y = 0)
      p2
    }   
    
    p1 <- ggpairs(mtcars1, columnLabels = c("Total Ni", "Total Mg", "Total Fe", "CEC", "pH","SWC"),
                  upper=list(continuous =my_fn1),
                  lower=list(continuous =my_fn2))+ 
      theme_bw() + theme(axis.text.x=(element_text(size=rel(0.7), angle=0)),
                         axis.text.y=(element_text(size=rel(0.7), angle=0)), panel.grid.major = element_blank(),
                         panel.grid.minor = element_blank(), panel.border = element_rect(fill = NA,colour = "grey35")) 
    p1
    

    【讨论】:

    • 感谢您的快速回复@TarJae。如果我为每个特定的子图寻找确定系数(R^2),考虑到它们在我的数据集建模方面的差异,我错了吗?可能与 mgcv 链接,例如:my_fn2
    • 我明白你的意思了!不幸的是,在我的解决方案中,来自 lm 模型的 R² 和 p 应用于 gam 模型。经过一番研究,我发现了这个 stackoverflow.com/questions/48627287/…> 和 stackoverflow.com/questions/58943575/…>。 :-( 我会留下这个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多