0

I have a correlation matrix produced using the Hmisc package. It produces the correlations to 2 decimal places, however I would like it to show 3 decimals places. How do I force it to do so? I am using the rcorr function.

Data:

df <- structure(list(X1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    X2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0), Y1 = c(3.93333333333333, 3.13333333333333, 4.3, 4.13333333333333, 
    3.2, 3.6, 3.66666666666667, 1.8, 3.8, 4.13333333333333, 4.13333333333333, 
    1.6, 3.4, 3.26666666666667, 2.53333333333333, 4.06666666666667, 
    4.53333333333333, 4.13333333333333, 3.4, 3.8, 3.33333333333333, 
    3.86666666666667, 4, 4.2, 2.53333333333333, 1.73333333333333, 
    1.8, 2.73333333333333, 1.66666666666667, 1.33333333333333, 
    2, 2.4, 3, 3.26666666666667, 3.2, 3.53333333333333, 3.66666666666667, 
    2.8, 3.33333333333333, 3.06666666666667, 3.46666666666667, 
    3.13333333333333, 3.93333333333333, 2.46666666666667, 1.26666666666667, 
    4.13333333333333, 1.8, 3, 2.93333333333333, 1.53333333333333, 
    4.06666666666667, 3.6, 2.06666666666667, 4.13333333333333, 
    3.3, 3.53333333333333, 3.4, 3.93333333333333, 3.73333333333333, 
    3, 3.13333333333333, 2.2, 4, 5, 3.66666666666667, 3.2, 3.4, 
    3.8, 3.66666666666667, 4.3, 4.2, 4.46666666666667, 3.33333333333333, 
    4.4, 4.2), Y2 = c(3.6, 2.2, 3.5, 4.2, 3, 2.8, 5, 2, 4.8, 
    4.4, 4.6, 1.6, 3.8, 3, 3, 3.4, 3.8, 4.2, 3.4, 3.4, 3.4, 4, 
    4.8, 4, 2.8, 1, 1.4, 1.2, 1.6, 3.8, 2.2, 1.4, 3.2, 1, 3.4, 
    3.2, 3.4, 1.8, 3.2, 1, 3, 2.8, 2.4, 1, 1, 4, 1.8, 2, 1, 1.2, 
    4.4, 3.2, 2, 4.2, 3.2, 3.2, 3.2, 3.6, 2.2, 2.8, 3.4, 2.6, 
    3.8, 4.2, 2.8, 3, 3.2, 4.8, 4.8, 4, 5, 5, 4.2, 4.6, 4.5)), row.names = c(NA, 
-75L), groups = structure(list(filter = c(0, 1, 2), .rows = list(
    1:25, 26:50, 51:75)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

correlation.matrix <- as.matrix(df[, c(
  "X1", "X2", "Y1", "Y2")])
cor.table <- Hmisc::rcorr(correlation.matrix)
cor.table
aspark2020
  • 341
  • 2
  • 17

2 Answers2

4

I think that you cannot (natively).

From the source code of Hmisc:::print.rcorr (not exported but used as an S3 method):

  P <- ifelse(P < .0001, 0, P)
  p <- format(round(P, 4))

The function hard-codes replacing low values with 0 as well as 4 digits in the print. I suggest you open an issue with a feature-request.

In the meantime, here's a replacement that allows you to control it a little:

print.rcorr <- function(x, ..., digits = getOption("Hmisc.rcorr.digits", 4))
{
  print(round(x$r,2))
  n <- x$n
  if(all(n == n[1,1]))
    cat("\nn=", n[1,1], "\n\n")
  else {
    cat("\nn\n")
    print(n)
  }

  cat("\nP\n")
  P <- x$P
  P <- ifelse(P < .0001, 0, P)
  p <- format(round(P, digits))
  p[is.na(P)] <- ""
  print(p, quote=FALSE)
  invisible()
}

print(cor.table, digits=2)
#       X1    X2    Y1    Y2
# X1  1.00 -0.50  0.31  0.34
# X2 -0.50  1.00 -0.50 -0.61
# Y1  0.31 -0.50  1.00  0.74
# Y2  0.34 -0.61  0.74  1.00
# n= 75 
# P
#    X1   X2   Y1   Y2  
# X1      0.00 0.01 0.00
# X2 0.00      0.00 0.00
# Y1 0.01 0.00      0.00
# Y2 0.00 0.00 0.00     

This "should" always mask Hmisc:::print.rcorr. I added the ability to use an options controller, so you can even do

options("Hmisc.rcorr.digits" = 2)
cor.table
#       X1    X2    Y1    Y2
# X1  1.00 -0.50  0.31  0.34
# X2 -0.50  1.00 -0.50 -0.61
# Y1  0.31 -0.50  1.00  0.74
# Y2  0.34 -0.61  0.74  1.00
# n= 75 
# P
#    X1   X2   Y1   Y2  
# X1      0.00 0.01 0.00
# X2 0.00      0.00 0.00
# Y1 0.01 0.00      0.00
# Y2 0.00 0.00 0.00     

However, two words of caution:

  • though I didn't test well and I think it's robust enough, it might be possible for some method of loading packages to use Hmisc:::print.rcorr instead of your global-defined version ... I'll have to think a little about namespaces and search path to know if this can unintentionally happen; and
  • if/when Hmisc updates their function, you should probably update this function as well. While this is unlikely (the last update to that function was 6 years ago), it is certainly feasible.
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • This worked well, thank you. I adjusted the r readout to also round to the decimals specified in the options controller: `print(round(x$r,digits))`. Thank you very much. – aspark2020 Oct 22 '19 at 02:35
2

Using the print function, you can specify the number of digits you need.

print( cor.table$r, digits=3)
Foxhound
  • 585
  • 5
  • 16