rcorr_clean: Modify rcorr results

View source: R/rcorr_clean.R

rcorr_cleanR Documentation

Modify rcorr results

Description

Modifies the 'rcorr' output from the 'Hmisc::rcorr()' function. The result is an array with additional matrices ('$P_simplified', '$label', and 'P_lab'). Optionally will replace the variable names with labels from a lookup table (see example)

Usage

rcorr_clean(rcmat, lookup_varTable = NULL)

Arguments

rcmat

An 'rcorr' class object created by the function 'Hmisc::rcorr“

lookup_varTable

A lookup table structured as a dataframe with variable lables. This is optional and by default is not used.

Details

Modify the Hmisc::rcorr output for nicer printing with labels in a heatmap

Value

An rcorr array with the following matrices:

  • correlation ('$r'). unchanged from the original

  • n ('$n'). unchanged from the original

  • P-value ('$P'). unchanged from the original

  • Simplified P-value ('$P_simplified'). a text matrix with P-values

  • Labeled P-value ('P_lab'). a text matrix with significance of P-values indicated with '*', '**', and '***'. Results are unadjusted.

  • label ('$label'). a text matrix with the 'r' and 'p-value' summary, useful as a hovertext

  • label ('$label_html'). a text matrix with the 'r' and 'p-value' summary, useful to pass to 'htmlTable'

See Also

rcorr, heatmaply

Examples

require(Hmisc)
require(tibble)
require(heatmaply)
require(ggplot2)
# create a LOOKUP TABLE
lookup_varTable <-
  tibble::tribble(~varname, ~labels,
          "mpg",	"Miles/(US) gallon",
          "cyl",	"Number of cylinders",
          "disp",	"Displacement (in.<sup>3</sup>)",
          "hp",	  "Gross horsepower",
          "drat",	"Rear axle ratio",
          "wt",	  "Weight (1000 lbs)",
          "qsec",	"1/4 mile time",
          "vs",	  "Engine (0 = V-shaped, 1 = straight)",
          "am",	  "Transmission (0 = automatic, 1 = manual)",
          "gear",	"Number of forward gears",
          "carb",	"Number of carburetors"  )
test <- Hmisc::rcorr(as.matrix(mtcars) )
test_result <- rcorr_clean(test, lookup_varTable = lookup_varTable)

# make an interactive HEATMAP with heatmaply
heatmaply::heatmaply(test_result$r,
                     custom_hovertext = test_result$label,
                     cellnote = test_result$P_lab,
                     na.value = "black",
                     scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(low = "blue", high = "red", midpoint = 0, limits = c(-1, 1)),
                     cellnote_textposition = "bottom center",
                     key.title = "Spearman's rho:")

JMLuther/tabletools documentation built on July 1, 2024, 2:01 p.m.