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(education,sleep_hours))State Your Hypotheses - PART OF YOUR WRITEUP
I hypothesize that Depression will be negatively correlated with Mental Flexibility and Resilience but positively correlated with Intolerance of Uncertainty, Intolerance of Uncertainty will be negatively correlated with Mental Flexibility and Resilience, and Mental Flexibility and Resilience will be positively correlated.
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) *if we want to look at the relationship between two variables, there must be two responses to both variables
- 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$brs <- scale(d$brs, center=T, scale=T)
hist(d$brs)sum(d$brs < -3 | d$brs > 3)[1] 0
d$iou <- scale(d$iou, center=T, scale=T)
hist(d$iou)sum(d$iou < -3 | d$iou > 3)[1] 0
d$mfq_state <- scale(d$mfq_state, center=T, scale=T)
hist(d$mfq_state)sum(d$mfq_state < -3 | d$mfq_state > 3)[1] 0
d$phq <- scale(d$phq, center=T, scale=T)
hist(d$phq)sum(d$phq < -3 | d$phq > 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$brs, d$iou)plot(d$brs, d$mfq_state)plot(d$brs, d$phq)plot(d$iou, d$mfq_state)plot(d$iou, d$phq)plot(d$phq, d$mfq_state)Check Your Variables
describe(d) vars n mean sd median trimmed mad min max range skew kurtosis
brs 1 402 0 1 -0.04 -0.01 1.12 -1.92 2.61 4.53 0.10 -0.69
iou 2 402 0 1 0.02 0.00 1.22 -2.02 2.19 4.20 0.01 -0.96
mfq_state 3 402 0 1 0.02 0.03 1.10 -2.70 2.24 4.95 -0.26 -0.28
phq 4 402 0 1 0.01 0.00 1.13 -1.77 1.66 3.43 -0.01 -1.08
se
brs 0.05
iou 0.05
mfq_state 0.05
phq 0.05
# also use histograms to examine your continuous variables
hist(d$brs)hist(d$mfq_state)hist(d$phq)hist(d$iou)Issues with My Data - PART OF YOUR WRITEUP
I checked for any univariate outliers and found none, it is important to check for outliers because they can distort findings and over exaggerate or weaken relationships and we need to interpret those results cautiously. I did not see any problems with linearity or non linearity. The skew and kurtosis were between the limits of positive and negative 2.
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$mfq_state, d$brs)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$mfq_state, y = d$brs)
Correlation matrix
[,1]
[1,] 0.66
Sample Size
[1] 402
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
brs iou mfq_state phq
brs 1.00 -0.61 0.66 -0.58
iou -0.61 1.00 -0.61 0.62
mfq_state 0.66 -0.61 1.00 -0.54
phq -0.58 0.62 -0.54 1.00
Sample Size
[1] 402
Probability values (Entries above the diagonal are adjusted for multiple tests.)
brs iou mfq_state phq
brs 0 0 0 0
iou 0 0 0 0
mfq_state 0 0 0 0
phq 0 0 0 0
To see confidence intervals of the correlations, print with the short=FALSE option
Write Up Results
I hypothesize that Depression will be negatively correlated with Mental Flexibility and Resilience but positively correlated with Intolerance of Uncertainty, Intolerance of Uncertainty will be negatively correlated with Mental Flexibility and Resilience, and Mental Flexibility and Resilience will be positively correlated. I did not find any issues with my data after running tests to look for univariate outliers and issues with linearity or nonlinearity. The skew and kurtosis were also between the limits of -2 and 2. According to the Correlation tests results (Cohen 1988), there is a large effect size for all variables Between the absolute values of .5 and 1. All p values are less than .01 indicated in Table 1 (Refer to Table 1).
| Variable | M | SD | 1 | 2 | 3 |
|---|---|---|---|---|---|
| Brief Resilience Scale (BRS) | -0.00 | 1.00 | |||
| Mental Flexibility Questionnaire (State) | 0.00 | 1.00 | -.61** | ||
| [-.67, -.55] | |||||
| Intolerance of Uncertainty (IOU) | 0.00 | 1.00 | .66** | -.61** | |
| [.60, .71] | [-.67, -.54] | ||||
| Depression Score (PHQ-9) | 0.00 | 1.00 | -.58** | .62** | -.54** |
| [-.64, -.51] | [.55, .67] | [-.61, -.47] | |||
| 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.