cor_test {rstatix} | R 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 |
optional character vector containing variable names for correlation analysis. Ignored when dot vars are specified.
. Accept unquoted
variable names: |
vars2 |
optional character vector. If specified, each element in
|
alternative |
indicates the alternative hypothesis and must be
one of |
method |
a character string indicating which correlation
coefficient is to be used for the test. One of |
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
|
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")
)