Calculate correlations for two or more variables

correlation(
  dataset,
  vars = "",
  method = "pearson",
  hcor = FALSE,
  hcor_se = FALSE,
  data_filter = "",
  envir = parent.frame()
)

Arguments

dataset

Dataset

vars

Variables to include in the analysis. Default is all but character and factor variables with more than two unique values are removed

method

Type of correlations to calculate. Options are "pearson", "spearman", and "kendall". "pearson" is the default

hcor

Use polycor::hetcor to calculate the correlation matrix

hcor_se

Calculate standard errors when using polycor::hetcor

data_filter

Expression entered in, e.g., Data > View to filter the dataset in Radiant. The expression should be a string (e.g., "price > 10000")

envir

Environment to extract data from

Value

A list with all variables defined in the function as an object of class compare_means

Details

See https://radiant-rstats.github.io/docs/basics/correlation.html for an example in Radiant

See also

summary.correlation to summarize results

plot.correlation to plot results

Examples

correlation(diamonds, c("price", "carat")) %>% str()
#> List of 13 #> $ descr : chr "## Correlation matrix\n\nCorrelations were calculated using the \"diamonds\" dataset\n\nVariables used:\n\n* price\n* carat" #> $ adj_text : chr "\n\n" #> $ cvmat : num [1:2, 1:2] 1.57e+07 1.74e+03 1.74e+03 2.25e-01 #> ..- attr(*, "dimnames")=List of 2 #> .. ..$ : chr [1:2] "price" "carat" #> .. ..$ : chr [1:2] "price" "carat" #> $ cmat :List of 11 #> ..$ r : num [1:2, 1:2] 1 0.927 0.927 1 #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:2] "price" "carat" #> .. .. ..$ : chr [1:2] "price" "carat" #> ..$ n : num 3000 #> ..$ t : num [1:2, 1:2] Inf 136 136 Inf #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:2] "price" "carat" #> .. .. ..$ : chr [1:2] "price" "carat" #> ..$ p : num [1:2, 1:2] 0 0 0 0 #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:2] "price" "carat" #> .. .. ..$ : chr [1:2] "price" "carat" #> ..$ se : num [1:2, 1:2] 0 0.00683 0.00683 0 #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:2] "price" "carat" #> .. .. ..$ : chr [1:2] "price" "carat" #> ..$ sef : num 0.0183 #> ..$ adjust: chr "holm" #> ..$ sym : logi TRUE #> ..$ ci :'data.frame': 1 obs. of 4 variables: #> .. ..$ lower: num 0.922 #> .. ..$ r : num 0.927 #> .. ..$ upper: num 0.932 #> .. ..$ p : num 0 #> ..$ ci.adj:'data.frame': 1 obs. of 2 variables: #> .. ..$ lower.adj: num 0.922 #> .. ..$ upper.adj: num 0.932 #> ..$ Call : language psych::corr.test(x = num_dat, method = method) #> ..- attr(*, "class")= chr [1:2] "psych" "corr.test" #> $ not_vary : chr(0) #> $ anyCategorical: Named logi [1:2] FALSE FALSE #> ..- attr(*, "names")= chr [1:2] "price" "carat" #> $ df_name : chr "diamonds" #> $ dataset : tibble [3,000 × 2] (S3: tbl_df/tbl/data.frame) #> ..$ price: int [1:3000] 580 650 630 706 1080 3082 3328 4229 1895 3546 ... #> ..$ carat: num [1:3000] 0.32 0.34 0.3 0.35 0.4 0.6 0.88 0.93 0.51 1.01 ... #> ..- attr(*, "description")= chr "## Diamond prices\n\nPrices of 3,000 round cut diamonds\n\n### Description\n\nA dataset containing the prices a"| __truncated__ #> $ vars : chr [1:2] "price" "carat" #> $ method : chr "pearson" #> $ hcor : logi FALSE #> $ hcor_se : logi FALSE #> $ data_filter : chr "" #> - attr(*, "class")= chr [1:3] "rcorr" "correlation" "list"
correlation(diamonds, "x:z") %>% str()
#> List of 13 #> $ descr : chr "## Correlation matrix\n\nCorrelations were calculated using the \"diamonds\" dataset\n\nVariables used:\n\n* x:z" #> $ adj_text : chr "\n\n" #> $ cvmat : num [1:3, 1:3] 1.263 1.251 0.773 1.251 1.242 ... #> ..- attr(*, "dimnames")=List of 2 #> .. ..$ : chr [1:3] "x" "y" "z" #> .. ..$ : chr [1:3] "x" "y" "z" #> $ cmat :List of 11 #> ..$ r : num [1:3, 1:3] 1 0.999 0.992 0.999 1 ... #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:3] "x" "y" "z" #> .. .. ..$ : chr [1:3] "x" "y" "z" #> ..$ n : num 3000 #> ..$ t : num [1:3, 1:3] Inf 1107 422 1107 Inf ... #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:3] "x" "y" "z" #> .. .. ..$ : chr [1:3] "x" "y" "z" #> ..$ p : num [1:3, 1:3] 0 0 0 0 0 0 0 0 0 #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:3] "x" "y" "z" #> .. .. ..$ : chr [1:3] "x" "y" "z" #> ..$ se : num [1:3, 1:3] 0 0.000902 0.00235 0.000902 0 ... #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:3] "x" "y" "z" #> .. .. ..$ : chr [1:3] "x" "y" "z" #> ..$ sef : num 0.0183 #> ..$ adjust: chr "holm" #> ..$ sym : logi TRUE #> ..$ ci :'data.frame': 3 obs. of 4 variables: #> .. ..$ lower: num [1:3] 0.999 0.991 0.991 #> .. ..$ r : num [1:3] 0.999 0.992 0.991 #> .. ..$ upper: num [1:3] 0.999 0.992 0.992 #> .. ..$ p : num [1:3] 0 0 0 #> ..$ ci.adj:'data.frame': 3 obs. of 2 variables: #> .. ..$ lower.adj: num [1:3] 0.999 0.991 0.991 #> .. ..$ upper.adj: num [1:3] 0.999 0.992 0.992 #> ..$ Call : language psych::corr.test(x = num_dat, method = method) #> ..- attr(*, "class")= chr [1:2] "psych" "corr.test" #> $ not_vary : chr(0) #> $ anyCategorical: Named logi [1:3] FALSE FALSE FALSE #> ..- attr(*, "names")= chr [1:3] "x" "y" "z" #> $ df_name : chr "diamonds" #> $ dataset : tibble [3,000 × 3] (S3: tbl_df/tbl/data.frame) #> ..$ x: num [1:3000] 4.43 4.45 4.27 4.6 4.72 5.35 6.14 6.34 5.09 6.31 ... #> ..$ y: num [1:3000] 4.45 4.42 4.23 4.65 4.68 5.43 6.18 6.23 5.06 6.37 ... #> ..$ z: num [1:3000] 2.71 2.81 2.68 2.74 2.94 3.38 3.8 3.86 3.22 4.05 ... #> ..- attr(*, "description")= chr "## Diamond prices\n\nPrices of 3,000 round cut diamonds\n\n### Description\n\nA dataset containing the prices a"| __truncated__ #> $ vars : chr "x:z" #> $ method : chr "pearson" #> $ hcor : logi FALSE #> $ hcor_se : logi FALSE #> $ data_filter : chr "" #> - attr(*, "class")= chr [1:3] "rcorr" "correlation" "list"