簡體   English   中英

將 p 值的星添加到相關矩陣 R

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

我正在使用本網站的代碼到 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)

如何計算p值並在相關系數旁邊加星? 我發現“PerformanceAnalytics”package 可以做到這一點,但我想在上面板散點圖中使用 smoothScatter function。 我不知道如何在 PerformanceAnalytics 中使用 smoothScatter function。 目前我的上面板 function 看起來像這樣。 它是從另一個網站復制的

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("***","**","*",""))

可以用來獲取p值。

所以整個 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