library(psych) # for the describe() command and the corr.test() command
library(apaTables) # to create our correlation table
library(kableExtra) # to create our correlation table
Correlation Lab
Loading Libraries
Importing Data
<- read.csv(file="Data/mydata.csv", header=T)
d #
# since we're focusing on our continuous variables, we're going to drop our categorical variables. this will make some stuff we're doing later easier.
<- subset(d, select=-c(gender, phq)) d
State Your Hypotheses - PART OF YOUR WRITEUP
Age, gender, and self-esteem (RSE) will significantly predict perceived stress (PSS), depressive symptoms (PHQ), and disordered eating behaviors (EDE-Q12), with lower self-esteem correlating with higher stress, depression, and disordered eating, and gender moderating these relationships.
Check Your Assumptions
Pearson’s Correlation Coefficient Assumptions
- Should have two measurements for each participant for each variable (confirmed by earlier procedures – we dropped any participants with missing data)
- Variables should be continuous and normally distributed, or assessments of the relationship may be inaccurate (confirmed above – if issues, make a note and continue)
- Outliers should be identified and removed, or results will be inaccurate (will do below)
- Relationship between the variables should be linear, or they will not be detected (will do below)
Checking for Outliers
Outliers can mask potential effects and cause Type II error (you assume there is no relationship when there really is one, e.g., false negative).
Note: You are not required to screen out outliers or take any action based on what you see here. This is something you will check and then discuss in your write-up.
# using the scale() command to standardize our variable, viewing a histogram, and then counting statistical outliers
$rse <- scale(d$rse, center=T, scale=T)
dhist(d$rse)
sum(d$rse < -3 | d$rse > 3)
[1] 0
$pss <- scale(d$pss, center=T, scale=T)
dhist(d$pss)
sum(d$pss < -3 | d$pss > 3)
[1] 0
$edeq12 <- scale(d$edeq12, center=T, scale=T)
dhist(d$edeq12)
sum(d$edeq12 < -3 | d$edeq12 > 3)
[1] 0
Checking for Linear Relationships
Non-linear relationships cannot be detected by Pearson’s correlation (the type of correlation we’re doing here). This means that you may underestimate the relationship between a pair of variables if they have a non-linear relationship, and thus your understanding of what’s happening in your data will be inaccurate.
Visually check that relationships are linear and write a brief description of any potential nonlinearity. You will have to use your judgement. There are no penalties for answering ‘wrong’, so try not to stress out about it too much – just do your best.
# use scatterplots to examine your continuous variables together
plot(d$rse, d$pss)
plot(d$rse, d$edeq12)
plot(d$pss, d$edeq12)
Check Your Variables
describe(d)
vars n mean sd median trimmed mad min max range skew kurtosis
age* 1 939 2.09 1.64 1.00 1.86 0.00 1.00 5.00 4.00 1.00 -0.86
rse 2 939 0.00 1.00 0.07 0.02 1.04 -2.32 1.89 4.22 -0.20 -0.75
pss 3 939 0.00 1.00 0.08 -0.01 1.16 -2.01 2.17 4.18 0.11 -0.80
edeq12 4 939 0.00 1.00 -0.18 -0.10 1.02 -1.21 2.93 4.14 0.69 -0.50
se
age* 0.05
rse 0.03
pss 0.03
edeq12 0.03
# Histograms for variable distributions
hist(d$rse)
hist(d$pss)
hist(d$edeq12)
Issues with My Data - PART OF YOUR WRITEUP
point out non linear groups as well as skew
Run Pearson’s Correlation
There are two ways to run Pearson’s correlation in R. You can calculate each correlation one-at-a-time using multiple commands, or you can calculate them all at once and report the scores in a matrix. The matrix output can be confusing at first, but it’s more efficient. We’ll do it both ways.
Run a Single Correlation
<- corr.test(d$rse, d$pss) corr_output
View Single Correlation
Strong effect: Between |0.50| and |1| Moderate effect: Between |0.30| and |0.49| Weak effect: Between |0.10| and |0.29| Trivial effect: Less than |0.09|
corr_output
Call:corr.test(x = d$rse, y = d$pss)
Correlation matrix
[,1]
[1,] -0.73
Sample Size
[1] 939
These are the unadjusted probability values.
The probability values adjusted for multiple tests are in the p.adj object.
[,1]
[1,] 0
To see confidence intervals of the correlations, print with the short=FALSE option
Create a Correlation Matrix
# corr_output_m <- corr.test(d)
View Test Output
Strong effect: Between |0.50| and |1| Moderate effect: Between |0.30| and |0.49| Weak effect: Between |0.10| and |0.29| Trivial effect: Less than |0.09|
# corr_output_m
Write Up Results
A Pearson correlation was conducted to examine the relationships between self-esteem (RSE), perceived stress (PSS), and disordered eating behaviors (EDE-Q12).
table_out <- apa.cor.table(d, filename = “table1.doc”, table.number = 1) # table_out2 <- as.data.frame(table_out$table.body)
Update variable names for clarity
table_out2$Variable <- c( “Perceived stress (PSS-4)”, ““,”Depression symptoms (PHQ-9)“,”“,”“,”Self-esteem (RSE-10)“,”“,”“,”Eating Disorder Examination Questionnaire (EDE-Q12)“,”“,”” )
Create the formatted correlation table
as.data.frame(table_out2) %>% kbl(row.names = F, align = c(“l”, “c”, “c”, “c”, “c”, “c”), caption = paste(“Table”, table_out\(table.number,": ",table_out\)table.title, sep=““), format =”html”, table.attr = “style=‘width: 75%;’”) %>% kable_classic() %>% footnote( general = “M and SD are used to represent mean and standard deviation, respectively. Values in square brackets indicate the 95% confidence interval. The confidence interval is a plausible range of population correlations that could have caused the sample correlation.”, symbol = c(“indicates p < .05”, “indicates p < .01.”), symbol_manual = c(“*“,”**“), threeparttable = T)
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.