library(lsr)
# correlate(
# x, y = NULL, test = FALSE,
# corr.method = "pearson",
# p.adjust.method = "holm")
x → Matrix or data frame containing variables to be correlated
y → Optionally, a second set of variables to be correlated with those in x
test → Should hypothesis tests be displayed? (Default=FALSE)
corr.method → What kind of correlations should be computed? Default is “pearson”, but “spearman” and “kendall” are also supported
p.adjust.method → What method should be used to correct for multiple comparisons. Default value is “holm”, and the allowable values are the same as for p.adjust
# data frame with factors and missing values
data <- data.frame(
anxiety = c(1.31,2.72,3.18,4.21,5.55,NA),
stress = c(2.01,3.45,1.99,3.25,4.27,6.80),
depression = c(2.51,1.77,3.34,5.83,9.01,7.74),
happiness = c(4.02,3.66,5.23,6.37,7.83,1.18),
gender = factor(c("male","female","female","male","female","female") ), ssri = factor( c("no","no","no",NA,"yes","yes") )
)
data
## anxiety stress depression happiness gender ssri
## 1 1.31 2.01 2.51 4.02 male no
## 2 2.72 3.45 1.77 3.66 female no
## 3 3.18 1.99 3.34 5.23 female no
## 4 4.21 3.25 5.83 6.37 male <NA>
## 5 5.55 4.27 9.01 7.83 female yes
## 6 NA 6.80 7.74 1.18 female yes
# default output is just the (Pearson) correlation matrix
correlate( data )
##
## CORRELATIONS
## ============
## - correlation type: pearson
## - correlations shown only when both variables are numeric
##
## anxiety stress depression happiness gender ssri
## anxiety . 0.784 0.906 0.924 . .
## stress 0.784 . 0.693 -0.453 . .
## depression 0.906 0.693 . 0.247 . .
## happiness 0.924 -0.453 0.247 . . .
## gender . . . . . .
## ssri . . . . . .
# other types of correlation:
correlate( data, corr.method="spearman" )
##
## CORRELATIONS
## ============
## - correlation type: spearman
## - correlations shown only when both variables are numeric
##
## anxiety stress depression happiness gender ssri
## anxiety . 0.500 0.900 0.900 . .
## stress 0.500 . 0.543 -0.257 . .
## depression 0.900 0.543 . 0.429 . .
## happiness 0.900 -0.257 0.429 . . .
## gender . . . . . .
## ssri . . . . . .
# the same examples, with Holm-corrected p-values
correlate( data, test=TRUE )
##
## CORRELATIONS
## ============
## - correlation type: pearson
## - correlations shown only when both variables are numeric
##
## anxiety stress depression happiness gender ssri
## anxiety . 0.784 0.906 0.924 . .
## stress 0.784 . 0.693 -0.453 . .
## depression 0.906 0.693 . 0.247 . .
## happiness 0.924 -0.453 0.247 . . .
## gender . . . . . .
## ssri . . . . . .
##
## ---
## Signif. codes: . = p < .1, * = p<.05, ** = p<.01, *** = p<.001
##
##
## p-VALUES
## ========
## - total number of tests run: 6
## - correction for multiple testing: holm
##
## anxiety stress depression happiness gender ssri
## anxiety . 0.467 0.171 0.150 . .
## stress 0.467 . 0.467 0.733 . .
## depression 0.171 0.467 . 0.733 . .
## happiness 0.150 0.733 0.733 . . .
## gender . . . . . .
## ssri . . . . . .
##
##
## SAMPLE SIZES
## ============
##
## anxiety stress depression happiness gender ssri
## anxiety 5 5 5 5 5 4
## stress 5 6 6 6 6 5
## depression 5 6 6 6 6 5
## happiness 5 6 6 6 6 5
## gender 5 6 6 6 6 5
## ssri 4 5 5 5 5 5