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(gender, mhealth))State Your Hypotheses - PART OF YOUR WRITEUP
We predict that neuroticism and intolerance of uncertainty, will be positively correlated, and that these two variables will be negatively correlated with conscientiousness and mental flexibility.
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$big5_neu <- scale(d$big5_neu, center=T, scale=T)
hist(d$big5_neu)sum(d$big5_neu < -3 | d$big5_neu > 3)[1] 0
d$big5_con <- scale(d$big5_con, center=T, scale=T)
hist(d$big5_con)sum(d$big5_con < -3 | d$big5_con > 3)[1] 2
d$iou <- scale(d$iou, center=T, scale=T)
hist(d$iou)sum(d$iou < -3 | d$iou > 3)[1] 0
d$mfq_26 <- scale(d$mfq_26, center=T, scale=T)
hist(d$mfq_26)sum(d$mfq_26 < -3 | d$mfq_26 > 3)[1] 9
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$big5_neu, d$big5_con)plot(d$big5_neu, d$iou)plot(d$big5_neu, d$mfq_26)plot(d$big5_con, d$iou)plot(d$big5_con, d$mfq_26)plot(d$iou, d$mfq_26)Check Your Variables
describe(d) vars n mean sd median trimmed mad min max range skew kurtosis
iou 1 1354 0 1 -0.15 -0.06 1.08 -1.73 2.65 4.37 0.48 -0.56
big5_neu 2 1354 0 1 0.19 0.03 0.98 -2.23 1.72 3.95 -0.29 -0.78
big5_con 3 1354 0 1 -0.11 0.03 1.25 -3.20 1.86 5.06 -0.25 -0.30
mfq_26 4 1354 0 1 0.09 0.04 0.96 -4.75 2.47 7.21 -0.47 0.68
se
iou 0.03
big5_neu 0.03
big5_con 0.03
mfq_26 0.03
# also use histograms to examine your continuous variables
hist(d$big5_neu)hist(d$big5_con)hist(d$iou)hist(d$mfq_26)Issues with My Data - PART OF YOUR WRITEUP
There were 2 outliers in the variable conscientiousness and 9 outliers in mental flexibility. Additionally, there were was a nonlinear relationship between neuroticism and conscientiousness. The skew and kurtosis are normally distributed for all variables ( between -2 and +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$big5_neu, d$mfq_26)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$big5_neu, y = d$mfq_26)
Correlation matrix
[,1]
[1,] -0.54
Sample Size
[1] 1354
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
iou big5_neu big5_con mfq_26
iou 1.00 0.64 -0.23 -0.57
big5_neu 0.64 1.00 -0.24 -0.54
big5_con -0.23 -0.24 1.00 0.34
mfq_26 -0.57 -0.54 0.34 1.00
Sample Size
[1] 1354
Probability values (Entries above the diagonal are adjusted for multiple tests.)
iou big5_neu big5_con mfq_26
iou 0 0 0 0
big5_neu 0 0 0 0
big5_con 0 0 0 0
mfq_26 0 0 0 0
To see confidence intervals of the correlations, print with the short=FALSE option
Write Up Results
We tested our hypothesis that the variables neuroticism and intolerance of uncertainty would be positively correlated. These two variables would both be negatively correlated with conscientiousness and mental flexibility.
There were a few issues with the data including, 2 outliers in the variable conscientiousness and 9 outliers in mental flexibility. Additionally, there were was a nonlinear relationship between neuroticism and conscientiousness. The skew and kurtosis are normally distributed for all variables ( between -2 and +2).
Using Pearson’s Correlation Test, we found a strong positive correlation between variables neuroticism and intolerance of uncertainty; r(1354)=0.64, p<.001. We also found a strong negative relationship between the variables intolerance of uncertainty and mental flexibility; r(1354)= -0.54, p<.001. Overall, the r-values for all variables were weak to strong (refer to Table 1).
| Variable | M | SD | 1 | 2 | 3 |
|---|---|---|---|---|---|
| Intolerance of Uncertainty | -0.00 | 1.00 | |||
| Neuroticism | 0.00 | 1.00 | .64** | ||
| [.61, .67] | |||||
| conscientiousness | -0.00 | 1.00 | -.23** | -.24** | |
| [-.28, -.18] | [-.29, -.19] | ||||
| Mental Flexibility | 0.00 | 1.00 | -.57** | -.54** | .34** |
| [-.61, -.54] | [-.57, -.50] | [.29, .39] | |||
| 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.