- 阅读权限
- 255
- 威望
- 2 级
- 论坛币
- 497178 个
- 通用积分
- 12679.5631
- 学术水平
- 517 点
- 热心指数
- 662 点
- 信用等级
- 353 点
- 经验
- 147986 点
- 帖子
- 2346
- 精华
- 1
- 在线时间
- 2047 小时
- 注册时间
- 2012-11-6
- 最后登录
- 2022-8-19
|
去github上搜代码啊
require(psych)这个包要安装好
- corstar <- function(x, y = NULL, use = "pairwise", method = "pearson", round = 3, row.labels, col.labels, ...) {
-
- require(psych)
- ct <- corr.test(x, y, use, method) # calculate correlation
- r <- ct$r # get correlation coefs
- p <- ct$p # get p-values
- stars <- ifelse(p < .001, "***", ifelse(p < .01, "** ", ifelse(p < .05, "* ", " "))) # generate significance stars
-
- m <- matrix(NA, nrow = nrow(r) * 2, ncol = ncol(r) + 1) # create empty matrix
- rlab <- if(missing(row.labels)) rownames(r) else row.labels # add row labels
- clab <- if(missing(col.labels)) {
- if(is.null(colnames(r)))
- deparse(substitute(y))
- else
- colnames(r)
- } else {
- col.labels # add column labels
- }
-
- rows <- 1:nrow(m) # row indices
- cols <- 2:ncol(m) # column indices
-
- odd <- rows %% 2 == 1 # odd rows
- even <- rows %% 2 == 0 # even rows
- m[odd, 1] <- rlab # add variable names
- m[even, 1] <- rep("", sum(even)) # add blank
-
- m[odd, cols] <- paste(format(round(r, round), nsmall = round, ...), stars, sep = "") # add r coefs
- m[even, cols] <- paste("(", format(round(p, round), nsmall = round, ...), ")", sep = "") # add p-values
-
- colnames(m) <- c(" ", clab) # add colnames
- m # return matrix
- }
- corstar(iris[,-5])
- Sepal.Length Sepal.Width Petal.Length Petal.Width
- [1,] "Sepal.Length" " 1.000***" "-0.118 " " 0.872***" " 0.818***"
- [2,] "" "(0.000)" "(0.152)" "(0.000)" "(0.000)"
- [3,] "Sepal.Width" "-0.118 " " 1.000***" "-0.428***" "-0.366***"
- [4,] "" "(0.152)" "(0.000)" "(0.000)" "(0.000)"
- [5,] "Petal.Length" " 0.872***" "-0.428***" " 1.000***" " 0.963***"
- [6,] "" "(0.000)" "(0.000)" "(0.000)" "(0.000)"
- [7,] "Petal.Width" " 0.818***" "-0.366***" " 0.963***" " 1.000***"
- [8,] "" "(0.000)" "(0.000)" "(0.000)" "(0.000)"
复制代码
|
-
总评分: 学术水平 + 1
热心指数 + 1
查看全部评分
|