library(psych) # for the describe() command and the corr.test() command
library(apaTables) # to create our correlation table
library(kableExtra) # to create our correlation tableCorrelation HW
Loading Libraries
Importing Data
d <- read.csv(file="Data/mydata.csv", header=T)
#
# 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.
d <- subset(d, select=-c(age, education))State Your Hypotheses - PART OF YOUR WRITEUP
I predict that worry, intolerance of uncertainty, and the patient health questionnaire will be positively correlated, and that all of these variables will be negatively correlated with self-esteem.
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 (will do below)
- 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
d$iou <- scale(d$iou, center=T, scale=T)
hist(d$iou) sum(d$iou < -3 | d$iou > 3)[1] 0
d$rse <- scale(d$rse, center=T, scale=T)
hist(d$rse) sum(d$rse < -3 | d$rse > 3)[1] 0
d$phq <- scale(d$phq, center=T, scale=T)
hist(d$phq) sum(d$phq < -3 | d$phq > 3)[1] 0
d$pswq <- scale(d$pswq, center=T, scale=T)
hist(d$pswq) sum(d$pswq < -3 | d$pss > 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$iou, d$rse) plot(d$iou, d$phq) plot(d$iou, d$pswq) plot(d$rse, d$phq) plot(d$rse, d$pswq) plot(d$phq, d$pswq)Check Your Variables
describe(d) vars n mean sd median trimmed mad min max range skew kurtosis se
pswq 1 926 0 1 0.07 0.00 1.25 -2.18 2.37 4.55 -0.04 -0.94 0.03
iou 2 926 0 1 -0.15 -0.06 1.11 -1.68 2.63 4.31 0.50 -0.62 0.03
phq 3 926 0 1 -0.21 -0.09 1.15 -1.24 2.26 3.50 0.64 -0.67 0.03
rse 4 926 0 1 0.06 0.02 1.04 -2.33 1.88 4.21 -0.20 -0.76 0.03
# also use histograms to examine your continuous variables
hist(d$iou)hist(d$rse)hist(d$phq)hist(d$pswq)Issues with My Data - PART OF YOUR WRITEUP
#Make a note here if you have any outliers, make sure you describe what variable the outliers are in and how many there are. Mention if the scores are.
#also make note of any indications of non-linearity in your variable pairings.
#make note of any skew/kurt non-normality. -2 to +2, describe which varibales it occurs inThere were no outliers with any of the continuous variables listed (iou,phq,rse,pswq).They are all linear relationships, so the Pearson’s Correlation test should run smoothly. There is also a normal range of skew and kurtosis.
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_output <- corr.test(d$iou, d$rse)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_outputCall:corr.test(x = d$iou, y = d$rse)
Correlation matrix
[,1]
[1,] -0.66
Sample Size
[1] 926
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_mCall:corr.test(x = d)
Correlation matrix
pswq iou phq rse
pswq 1.00 0.67 0.53 -0.57
iou 0.67 1.00 0.63 -0.66
phq 0.53 0.63 1.00 -0.75
rse -0.57 -0.66 -0.75 1.00
Sample Size
[1] 926
Probability values (Entries above the diagonal are adjusted for multiple tests.)
pswq iou phq rse
pswq 0 0 0 0
iou 0 0 0 0
phq 0 0 0 0
rse 0 0 0 0
To see confidence intervals of the correlations, print with the short=FALSE option
Write Up Results
I predicted that worry, intolerance of uncertainty, and the patient health questionnaire will be positively correlated, and that all of these variables will be negatively correlated with self-esteem. There were no outliers with any of the continuous variables listed (iou,phq,rse,pswq).They are all linear relationships, so the Pearson’s Correlation test should run smoothly. There is also a normal range of skew and kurtosis.The results were all statistically significant, with p<.01.They all have a strong effect size, being between |0.50| and |1|. See Table 1. As predicted, self-esteem was negatively correlated, while all the other variables were positively correlated.
| Variable | M | SD | 1 | 2 | 3 |
|---|---|---|---|---|---|
| Intolerance of Uncertainty | 0.00 | 1.00 | |||
| Self-esteem (RSE-10) | 0.00 | 1.00 | .67** | ||
| [.63, .70] | |||||
| Depression Score (PHQ-9) | 0.00 | 1.00 | .53** | .63** | |
| [.48, .58] | [.59, .67] | ||||
| Penn State Worry Questionnare (PSWQ) | 0.00 | 1.00 | -.57** | -.66** | -.75** |
| [-.61, -.53] | [-.69, -.62] | [-.78, -.72] | |||
| Note: | |||||
| 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. | |||||
| * indicates p < .05 | |||||
| ** indicates p < .01. |
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.