cor_test: Correlation Test

View source: R/cor_test.R

cor_testR Documentation

Correlation Test

Description

Provides a pipe-friendly framework to perform correlation test between paired samples, using Pearson, Kendall or Spearman method. Wrapper around the function cor.test().

Can also performs multiple pairwise correlation analyses between more than two variables or between two different vectors of variables. Using this function, you can also compute, for example, the correlation between one variable vs many.

Usage

cor_test(
  data,
  ...,
  vars = NULL,
  vars2 = NULL,
  alternative = "two.sided",
  method = "pearson",
  conf.level = 0.95,
  use = "pairwise.complete.obs"
)

Arguments

data

a data.frame containing the variables.

...

One or more unquoted expressions (or variable names) separated by commas. Used to select a variable of interest. Alternative to the argument vars.

vars

optional character vector containing variable names for correlation analysis. Ignored when dot vars are specified.

  • If vars is NULL, multiple pairwise correlation tests is performed between all variables in the data.

  • If vars contain only one variable, a pairwise correlation analysis is performed between the specified variable vs either all the remaining numeric variables in the data or variables in vars2 (if specified).

  • If vars contain two or more variables: i) if vars2 is not specified, a pairwise correlation analysis is performed between all possible combinations of variables. ii) if vars2 is specified, each element in vars is tested against all elements in vars2

. Accept unquoted variable names: c(var1, var2).

vars2

optional character vector. If specified, each element in vars is tested against all elements in vars2. Accept unquoted variable names: c(var1, var2).

alternative

indicates the alternative hypothesis and must be one of "two.sided", "greater" or "less". You can specify just the initial letter. "greater" corresponds to positive association, "less" to negative association.

method

a character string indicating which correlation coefficient is to be used for the test. One of "pearson", "kendall", or "spearman", can be abbreviated.

conf.level

confidence level for the returned confidence interval. Currently only used for the Pearson product moment correlation coefficient if there are at least 4 complete pairs of observations.

use

an optional character string giving a method for computing covariances in the presence of missing values. This must be (an abbreviation of) one of the strings "everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs".

Value

return a data frame with the following columns:

  • var1, var2: the variables used in the correlation test.

  • cor: the correlation coefficient.

  • statistic: Test statistic used to compute the p-value.

  • p: p-value.

  • conf.low,conf.high: Lower and upper bounds on a confidence interval.

  • method: the method used to compute the statistic.

Functions

  • cor_test(): correlation test between two or more variables.

See Also

cor_mat(), as_cor_mat()

Examples


# Correlation between the specified variable vs
# the remaining numeric variables in the data
#:::::::::::::::::::::::::::::::::::::::::
mtcars %>% cor_test(mpg)

# Correlation test between two variables
#:::::::::::::::::::::::::::::::::::::::::
mtcars %>% cor_test(wt, mpg)

# Pairwise correlation between multiple variables
#:::::::::::::::::::::::::::::::::::::::::
mtcars %>% cor_test(wt, mpg, disp)

# Grouped data
#:::::::::::::::::::::::::::::::::::::::::
iris %>%
  group_by(Species) %>%
 cor_test(Sepal.Width, Sepal.Length)

# Multiple correlation test
#:::::::::::::::::::::::::::::::::::::::::
# Correlation between one variable vs many
mtcars %>% cor_test(
  vars = "mpg",
  vars2 = c("disp", "hp", "drat")
 )

# Correlation between two vectors of variables
# Each element in vars is tested against all elements in vars2
mtcars %>% cor_test(
  vars = c("mpg", "wt"),
  vars2 = c("disp", "hp", "drat")
 )



kassambara/rstatix documentation built on Feb. 6, 2023, 3:36 a.m.