cor(use="pairwise")
时间: 2024-05-30 07:10:02 浏览: 177
`cor(use="pairwise")` 是用于计算数据中变量之间的相关性的函数。它指定在计算相关性时应该考虑每个配对变量之间的可用观测值的数量。在 `cor` 函数中,`use="pairwise"` 意味着计算每个变量之间的相关性时,仅考虑同时具有非缺失值的观测值对。这与 `use="complete.obs"` 参数不同,后者将仅考虑同时具有非缺失值的所有变量的观测值对。
相关问题
panel.cor <- function(x, y,digits=2, prefix="", use="pairwise.complete.obs", method=cormeth, cex.cor, ...) { usr <- par("usr"); on.exit(par(usr)) par(usr = c(0, 1, 0, 1)) r <- cor(x, y, use=use, method=method) # MG: remove abs here txt <- format(c(r, 0.123456789), digits=digits)[1] txt <- paste(prefix, txt, sep="") if(missing(cex.cor)) cex <- 0.8/strwidth(txt) test <- cor.test(as.numeric(x),as.numeric(y), method=method) # borrowed from printCoefmat Signif <- symnum(test$p.value, corr = FALSE, na = FALSE,cutpoints = c(0, 0.001, 0.005,0.01, 0.05,0.1,0.5, 1)) # MG: add abs here and also include a 30% buffer for small numbers corr_text <- round(cor(x, y), 2) symbol_size <- abs(Signif) * 10 color_scale <- colorRampPalette(c("#F09576","white","#175A9E")) # 颜色比例尺 symbol_color <- color_scale(5 + corr_text * 5) # 根据相关系数设置颜色 symbols(1, 1, circles = symbol_size, inches = FALSE, bg = symbol_color,fg = NA, add = TRUE) #symbols(mean(x), mean(y), circles = symbol_size,inches = FALSE,bg = symbol_color, fg = NA, add = TRUE) # 绘制符号 #text(0.5, 0.5,paste(corr_text), cex = 2 *( abs(corr_text) + .3) / 1.2) #text(0.5, 0.5, paste(corr_text), cex = )mean(x), max(y) * 0.9 text(0.5, 0.5, txt, cex = cex * (abs(r) + .3) / 1.3)怎么修改,使根据不同颜色圆圈表示相关性大小,且同时用不同大小圆圈表示显著性
可以尝试修改如下代码:
```
panel.cor <- function(x, y,digits=2, prefix="", use="pairwise.complete.obs", method=cormeth, cex.cor, ...) {
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- cor(x, y, use=use, method=method)
txt <- format(c(r, 0.123456789), digits=digits)[1]
txt <- paste(prefix, txt, sep="")
if(missing(cex.cor)) cex <- 0.8/strwidth(txt)
test <- cor.test(as.numeric(x),as.numeric(y), method=method)
Signif <- symnum(test$p.value, corr = FALSE, na = FALSE,cutpoints = c(0, 0.001, 0.005,0.01, 0.05,0.1,0.5, 1))
# 根据相关系数设置颜色和大小
corr_text <- round(cor(x, y), 2)
symbol_size <- abs(Signif) * 10 * (abs(corr_text) + 0.3) # 增加相关系数的权重
color_scale <- colorRampPalette(c("#F09576","white","#175A9E"))
symbol_color <- color_scale(5 + corr_text * 5)
# 绘制符号
symbols(1, 1, circles = symbol_size, inches = FALSE, bg = symbol_color, fg = NA, add = TRUE)
# 显示相关系数
text(0.5, 0.5, txt, cex = cex * (abs(r) + .3) / 1.3)
}
```
这个修改后的函数会根据相关系数的大小和显著性同时显示不同颜色和大小的圆圈,来表示相关性大小和显著性。其中,增加了相关系数的权重,以使圆圈大小更加准确地反映相关性大小。
panel.cor <- function(x, y, digits=2, prefix="", use="pairwise.complete.obs", method=cormeth, cex.cor, ...) { usr <- par("usr"); on.exit(par(usr)) # 背景颜色: 要求,相关系数大于0为蓝色渐变,小于0为红色渐变 names(usr) <- c("xleft", "xright", "ybottom", "ytop") col_index <- if (cor(x,y) > 0) {(1 - cor(x,y))} else {(1 + cor(x,y))} bg_col <- if (cor(x,y) > 0) { rgb( red = col_index, green = col_index, blue = 1)} else { rgb(red = 1, green = col_index, blue = col_index)} corr_text <- round(cor(x, y), 2) symbol_size <- abs(corr_text) * 5 #color_scale <- colorRampPalette(c("#F09576","white","#175A9E")) # 颜色比例尺 #color_scale<- ifelse(corr_text >= 0, "#175A9E","#F09576") #symbol_color <- color_scale(5 + corr_text * 10) # 根据相关系数设置颜色 symbols(mean(x), mean(y) , circles = symbol_size, inches = FALSE, bg =bg_col ,fg = NA, add = TRUE)这段代码该怎么修改可以使增加的圆圈在右三角每个格子中心位置,求代码,谢谢
可以尝试修改最后一行代码为:
```
symbols(x = seq(mean(x)+0.5, usr[2]-0.5, by = 1), y = seq(mean(y)+0.5, usr[4]-0.5, by = 1), circles = symbol_size, inches = FALSE, bg = bg_col, fg = NA, add = TRUE)
```
这样可以让增加的圆圈在右三角每个格子中心位置。其中`seq(mean(x)+0.5, usr[2]-0.5, by = 1)`表示在x轴上从中心位置向右每隔一个单位位置取一个坐标值,`seq(mean(y)+0.5, usr[4]-0.5, by = 1)`同理表示在y轴上从中心位置向上每隔一个单位位置取一个坐标值。
阅读全文