top of page

Correlation

KEY:

ds = dataset you are currently using.

Var1, Var2, etc. = Variables in the dataset

XYXY = dummy name for a variable, matrix, or data frame into which you are moving information.

Topics

Correlation between two variables in a data set

Note that IV = Variable that will go on X axis. DV = variable that will go on Y axis.

_____________________________

cor(ds$Var1, ds$Var2)

_____________________________

Anchor 1

Correlations between two variables in a data set...with p-value

Note that IV = Variable that will go on X axis. DV = variable that will go on Y axis.

_____________________________

cor.test(ds$Var1, ds$Var2, method="pearson")

_____________________________

Anchor 2

Correlations among all variables in a data set

Note [-1] removes the first column (i.e., subject number) from the analysis.

_____________________________

cor(ds[-1], method="pearson")

_____________________________

Anchor 3

Correlations among all variables in a data set...with p-values

You will need to download the "Hmisc" package in order to obtain the "rcorr" function.

Note [-c(1)] removes the first column (i.e., subject number) from the analysis.

_____________________________

library(Hmisc)

rcorr(as.matrix(ds[-c(1)]), type= "pearson")

_____________________________

Partial Correlation

You will need to download the "ppcor" package in order to obtain the "pcor.test" function.

Var1 and Var2 is the correlation of interest. VarZ is the factor that is partialled out.

_____________________________

library(ppcor)


pcor.test(x=ds$Var1, y=ds$Var2, z=ds$VarZ, method="pearson")

_____________________________

Anchor 4
Anchor 5

Semipartial Correlation

You will need to download the "ppcor" package in order to obtain the "spcor.test" function.

Var1 and Var2 is the correlation of interest. VarZ is the factor that is partialled out of Var2 only.

_____________________________

library(ppcor)


spcor.test(x=ds$Var1, y=ds$Var2, z=ds$VarZ, method="pearson")

_____________________________

Anchor 6

Bayesian correlation

Bayesian correlations can be helpful when you want to know how much trust you can place in an observed correlation value. This can be particularly helpful in the case of non-significant relationships.

You will need to download the "BayesFactor" (to create the model) and "bayestestR" (to expand the descriptive capabilities) packages.

These commands are intended to get you up and running. For extensive discussion of these packages, please visit https://richarddmorey.github.io/BayesFactor/ and https://easystats.github.io/bayestestR/

_____________________________

library(BayesFactor)

XYXY <- correlationBF(DS$Var1, DS$Var2)

library(bayestestR)

describe_posterior(XYXY)

bayesfactor(XYXY)

_____________________________

Anchor 7
bottom of page