Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Pearson相关矩阵打印到r中的Word_R_Export_Correlation - Fatal编程技术网

如何将Pearson相关矩阵打印到r中的Word

如何将Pearson相关矩阵打印到r中的Word,r,export,correlation,R,Export,Correlation,我对R很陌生,我想在word文档中绘制一个奇特的相关矩阵 下面是我的数据的一个小例子: provaSO1 <- structure(list(TotalDebt = c(0.637, 0.517, 0.581, 0.785, 0.687,0.703, 0.474), Long_ok = c(0.157, 0.121, 0.051, 0.29, 0.153,0.301, 0.102), Short_ok = c(0.48, 0.

我对R很陌生,我想在word文档中绘制一个奇特的相关矩阵

下面是我的数据的一个小例子:

provaSO1 <- structure(list(TotalDebt = c(0.637, 0.517, 0.581, 0.785, 0.687,0.703, 0.474), 
               Long_ok = c(0.157, 0.121, 0.051, 0.29, 0.153,0.301, 0.102), 
               Short_ok = c(0.48, 0.396, 0.531, 0.495, 0.535,0.402, 0.372), 
               Size = c(6.184, 6.184, 6.663, 7.302, 6.714, 6.949,7.627), 
               ln_Age = c(3.638, 3.664, 3.689, 3.714, 3.738, 3.761, 3.784), 
               liquidity = c(0.99, 0.988, 0.995, 0.965, 0.949, 0.949,0.53),
               Asset_Tangibility = c(0.005, 0.006, 0.003, 0.023, 0.033, 0.03, 0.457), 
               profitability = c(-0.058, -0.202, -0.106, 0.032, 0.03, 0.013, 0.042)), 
          row.names = c(NA, 7L), class = "data.frame")
我使用
corstars函数
添加显著性水平,仅获得较低的三角形:

corstarsl <- function(x){
  require(Hmisc)
  x <- as.matrix(x)
  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), 3))[,-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您可以使用将关联表直接保存到Word文档中。(免责声明:我是作者。)使用
datasummary\u correlation
函数及其
output
参数,我们有:

库(模型摘要)
数据汇总与相关性(provaSO1,
output=“correlation\u table.docx”)

您可以直接使用xlsx(编写excel)包编写。不确定是否有直接用于docx扩展的功能,但在编写excel工作表后,将其移动到任何其他office文件会更容易谢谢@SametSökel,我想很难找到我要找的内容,希望有人知道如何在.docx或“html”中打印“stargazer”直接放入文档,如果您正在使用R Studio,则可以直接将笔记本导出为html页面。你可以选择在R中打印,然后将记事本编织成html
corstarsl <- function(x){
  require(Hmisc)
  x <- as.matrix(x)
  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), 3))[,-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)
}
matrix_correlation <- corstarsl(provaSO1)

library(xtable)
corr_print <- xtable(matrix_correlation)

library(rio) 
library(openxlsx)
export(corr_print,"correlation_ok.xlsx")