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.

## 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