How to to achieve clean correlation table output w/ corstarsl() function?

有些话、适合烂在心里 提交于 2019-12-24 06:34:23

问题


There's a nice correlation table function around (the only one I know producing this special kind of familiarity to ones eye). Bertold already has modified the code in order to achieve a nicer output. However, there are situations e. g. w/ negative correlations when the output still looks somewhat messed up.

In the following I show the function first and below a minimal example:

corstarsl <- function(x){
  # corstars1() computes a correlation matrix w/ significance stars
  require(Hmisc)
  x <- as.matrix(x)
  R <- rcorr(x)$r
  p <- rcorr(x)$P

  ## define significance levels
  mystars <- ifelse(p < .001, "***", 
                    ifelse(p < .01, "** ", 
                           ifelse(p < .05, "* ", " ")))

  ## correlation matrix w/ two digits
  R <- format(round(cbind(rep(- 1.11, ncol(x)), R), 2))[, -1]

  ## build a new matrix that includes the correlations w/ appropriate stars
  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 = "")

  ## remove upper triangle
  Rnew <- as.matrix(Rnew)
  Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
  Rnew <- as.data.frame(Rnew)

  ## remove last column and return the matrix (which is now a data frame)
  Rnew <- cbind(Rnew[1:length(Rnew) - 1])
  return(Rnew)
} 

Example:

library(MASS)
n <- 100
mymeans <- c(10, 12, 15, 17) # means of each var
Sigma <- matrix(c(1, -.45, .16, -.71,
                  -.45, 1, -.71, .09,
                  .16, -.71, 1, -.17,
                  -.71, .09, -.17, 1), ncol = 4)
dat <- mvrnorm(n = n, mu = mymeans, Sigma, empirical = TRUE)

(cortab <- corstarsl(as.data.frame(dat)))
##          V1       V2     V3
## V1                         
## V2 -0.45***                
## V3    0.16  -0.71***       
## V4 -0.71***    0.09  -0.17 

# or with htmlTable():
library(htmlTable)
htmlTable(cortab, 
      align = paste(rep('l', ncol(cortab)), collapse = ''))

Disrupted output:

Edit: With the left-align I'm almost there, but how to give the positive values more space?

Expected output:

Does anyone know how to achieve a better formatting of the result inside the function (decimal points should be aligned)?


回答1:


You can use tableHTML and use the widths argument to control the widths of columns. Also, you would need to use &nbsp; which is the html character for a space like this:

#convert to character
cortab[] <- lapply(cortab, as.character)
#if the cell does not start with a minus add an html space
cortab[] <- lapply(cortab, function(x) {

 ifelse(!startsWith(x, '-'), paste0('&nbsp;', x), x)

})

#convert to html with tableHTML
#control the column widths with the widths argument
library(tableHTML)
tableHTML(cortab, widths = c(40, 60, 60, 60), theme = 'scientific') %>%
 add_css_column(list('text-align', 'left !important'), columns = 1:4)



来源:https://stackoverflow.com/questions/48043464/how-to-to-achieve-clean-correlation-table-output-w-corstarsl-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!