我开始学习如何使用r进行统计分析,我想改善我的报告并为我的数据提供一些漂亮的表格。现在,我正在处理相关矩阵输出。我希望它像SPSS一样,但是即使我在互联网上找到了一些代码,也似乎无法做到这一点。这个:
corstarsl <- function(x){
require(Hmisc)
x <- as.matrix(mydata)
R <- rcorr(x)$r
p <- rcorr(x)$P
mystars <- ifelse(p < .001, "***", ifelse(p < .01, "** ", ifelse(p < .05, "* ", " ")))
R <- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[,-1]
Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
diag(Rnew) <- paste(diag(R), " ", sep="")
rownames(Rnew) <- colnames(x)
colnames(Rnew) <- paste(colnames(x), "", sep="")
Rnew <- as.matrix(Rnew)
Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
Rnew <- as.data.frame(Rnew)
Rnew <- cbind(Rnew[1:length(Rnew)-1])
return(Rnew)
}
xtable(corstarsl(swiss[,1:13]))
但是它并没有给我带来一张桌子。 有人可以帮我吗?而且,如果有人能给我一些有关如何改善数据报告的提示,我将不胜感激! 我希望我能对自己有所解释,因为英语不是我的母语。预先感谢!
答案 0 :(得分:0)
不清楚您希望输出的外观如何,但这应该有所帮助:
corstarsl <- function(d){
require(Hmisc)
x <- as.matrix(d)
R <- rcorr(x)$r
p <- rcorr(x)$P
mystars <- ifelse(p < .001, "***", ifelse(p < .01, "** ", ifelse(p < .05, "* ", " ")))
R <- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[,-1]
Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
diag(Rnew) <- paste(diag(R), " ", sep="")
rownames(Rnew) <- colnames(x)
colnames(Rnew) <- paste(colnames(x), "", sep="")
Rnew <- as.matrix(Rnew)
Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
Rnew <- as.data.frame(Rnew)
Rnew <- cbind(Rnew[1:length(Rnew)-1])
return(Rnew)
}
corstarsl(swiss)
# Fertility Agriculture Examination Education Catholic
# Fertility
# Agriculture 0.35*
# Examination -0.65*** -0.69***
# Education -0.66*** -0.64*** 0.70***
# Catholic 0.46** 0.40** -0.57*** -0.15
# Infant.Mortality 0.42** -0.06 -0.11 -0.10 0.18
另一种更快获取更多信息的方法是:
library(PerformanceAnalytics)
chart.Correlation(swiss)