简体   繁体   English

将 p 值的星添加到相关矩阵 R

[英]add star of p value to correlation matrix R

I am using codes from this website to plot scatter and correlation matrix: http://www.sthda.com/english/wiki/scatter-plot-matrices-r-base-graphs我正在使用本网站的代码到 plot 散点和相关矩阵: http://www.sthda.com/english/wiki/scatter-plot-matrices-r-base-graphs

panel.cor <- function(x, y){
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r <- round(cor(x, y), digits=2)
    txt <- paste0("R = ", r)
    cex.cor <- 0.8/strwidth(txt)
    text(0.5, 0.5, txt, cex = cex.cor * r)
}
# Customize upper panel
upper.panel<-function(x, y){
  points(x,y, pch = 19, col = my_cols[iris$Species])
}
# Create the plots
pairs(iris[,1:4], 
      lower.panel = panel.cor,
      upper.panel = upper.panel)

How to calculate p value and add stars next to the correlation coefficient?如何计算p值并在相关系数旁边加星? I find "PerformanceAnalytics" package can do this but I want to use smoothScatter function in the upper panel scatterplot.我发现“PerformanceAnalytics”package 可以做到这一点,但我想在上面板散点图中使用 smoothScatter function。 I don't know how to use smoothScatter function in PerformanceAnalytics.我不知道如何在 PerformanceAnalytics 中使用 smoothScatter function。 Currently my upper panel function looks like this.目前我的上面板 function 看起来像这样。 It is copied from another website它是从另一个网站复制的

pairs(df, lower.panel = panel.cor,
        upper.panel = function(...) smoothScatter(..., nrpoints = 0, add = TRUE), gap = 0.2)
 r2 <- cor.test(x, y)$p.value
 r3 <- symnum(r2, cutpoints = c(0, 0.001, 0.01, 0.05, 1), 
                   symbols = c("***","**","*",""))

can be used to obtain p value.可以用来获取p值。

so the whole panel.cor function looks like this:所以整个 panel.cor function 看起来像这样:

panel.cor <- function(x, y){
  usr <- par("usr"); on.exit(par(usr))
  par(usr = c(0, 1, 0, 1))
  r <- round(cor(x, y), digits=2)
  r2 <- cor.test(x, y)$p.value
  r3 <- symnum(r2, cutpoints = c(0, 0.001, 0.01, 0.05, 1), 
                   symbols = c("***","**","*",""))
  txt <- paste(r, r3, sep = " ")
  cex.cor <- 0.8/strwidth(txt)
  text(0.5, 0.5, txt, cex = cex.cor*r)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM