
Last chance! 50% off unlimited learning
Sale ends in
Compute correlation matrix with p-values. Numeric columns in the data are detected and automatically selected for the analysis. You can also specify variables of interest to be used in the correlation analysis.
cor_mat(
data,
...,
vars = NULL,
method = "pearson",
alternative = "two.sided",
conf.level = 0.95
)cor_pmat(
data,
...,
vars = NULL,
method = "pearson",
alternative = "two.sided",
conf.level = 0.95
)
cor_get_pval(x)
a data.frame containing the variables.
One or more unquoted expressions (or variable names) separated by commas. Used to select a variable of interest.
a character vector containing the variable names of interest.
a character string indicating which correlation
coefficient is to be used for the test. One of "pearson"
,
"kendall"
, or "spearman"
, can be abbreviated.
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.
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.
an object of class cor_mat
a data frame
cor_mat
: compute correlation matrix with p-values. Returns a data
frame containing the matrix of the correlation coefficients. The output has
an attribute named "pvalue", which contains the matrix of the correlation
test p-values.
cor_pmat
: compute the correlation matrix but returns only the p-values of the tests.
cor_get_pval
: extract a correlation matrix p-values from an object of
class cor_mat()
.
cor_test()
, cor_reorder()
,
cor_gather()
, cor_select()
,
cor_as_symbols()
, pull_triangle()
,
replace_triangle()
# NOT RUN {
# Data preparation
#:::::::::::::::::::::::::::::::::::::::::::
mydata <- mtcars %>%
select(mpg, disp, hp, drat, wt, qsec)
head(mydata, 3)
# Compute correlation matrix
#::::::::::::::::::::::::::::::::::::::::::
# Correlation matrix between all variables
cor.mat <- mydata %>% cor_mat()
cor.mat
# Specify some variables of interest
mydata %>% cor_mat(mpg, hp, wt)
# Or remove some variables in the data
# before the analysis
mydata %>% cor_mat(-mpg, -hp)
# Significance levels
#::::::::::::::::::::::::::::::::::::::::::
cor.mat %>% cor_get_pval()
# Visualize
#::::::::::::::::::::::::::::::::::::::::::
# Insignificant correlations are marked by crosses
cor.mat %>%
cor_reorder() %>%
pull_lower_triangle() %>%
cor_plot(label = TRUE)
# Gather/collapse correlation matrix into long format
#::::::::::::::::::::::::::::::::::::::::::
cor.mat %>% cor_gather()
# }
Run the code above in your browser using DataLab