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 HW
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(race_rc, age)) d
State Your Hypotheses - PART OF YOUR WRITEUP
We predict that Satisfaction with life, Perceived social support, and safety will all positively correlate with each other, and all three of those variables will negatively correlate with perceived stress.
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
$moa_safety <- scale(d$moa_safety, center=T, scale=T)
dhist(d$moa_safety)
sum(d$moa_safety < -3 | d$moa_safety > 3)
[1] 15
$swb <- scale(d$swb, center=T, scale=T)
dhist(d$swb)
sum(d$swb < -3 | d$swb > 3)
[1] 0
$support <- scale(d$support, center=T, scale=T)
dhist(d$support)
sum(d$support < -3 | d$support > 3)
[1] 27
$stress <- scale(d$stress, center=T, scale=T)
dhist(d$stress)
sum(d$stress < -3 | d$stress > 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$moa_safety, d$swb)
plot(d$moa_safety, d$support)
plot(d$moa_safety, d$stress)
plot(d$swb, d$support)
plot(d$swb, d$stress)
plot(d$support, d$stress)
Check Your Variables
describe(d)
vars n mean sd median trimmed mad min max range skew
moa_safety 1 2121 0 1 0.05 0.10 1.15 -3.43 1.22 4.64 -0.71
swb 2 2121 0 1 0.05 0.04 1.11 -2.58 1.93 4.51 -0.36
support 3 2121 0 1 0.19 0.11 0.87 -4.87 1.29 6.16 -1.10
stress 4 2121 0 1 0.06 0.00 0.99 -2.96 2.57 5.53 -0.02
kurtosis se
moa_safety -0.06 0.02
swb -0.48 0.02
support 1.36 0.02
stress -0.14 0.02
# also use histograms to examine your continuous variables
hist(d$moa_safety)
hist(d$swb)
hist(d$support)
hist(d$stress)
Issues with My Data - PART OF YOUR WRITEUP
We did find outliers in 2 of our variables. In the variable of Safety there are 15 outliers, and in the variable of Support there is 27 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). All variables are show linearity. There are no issues in kurtosis or 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$moa_safety, d$swb) 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$moa_safety, y = d$swb)
Correlation matrix
[,1]
[1,] 0.13
Sample Size
[1] 2121
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.test(d) corr_output_m
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
Call:corr.test(x = d)
Correlation matrix
moa_safety swb support stress
moa_safety 1.00 0.13 0.10 -0.05
swb 0.13 1.00 0.47 -0.49
support 0.10 0.47 1.00 -0.21
stress -0.05 -0.49 -0.21 1.00
Sample Size
[1] 2121
Probability values (Entries above the diagonal are adjusted for multiple tests.)
moa_safety swb support stress
moa_safety 0.00 0 0 0.01
swb 0.00 0 0 0.00
support 0.00 0 0 0.00
stress 0.01 0 0 0.00
To see confidence intervals of the correlations, print with the short=FALSE option
Write Up Results
We predict that Satisfaction with life, Perceived social support, and safety will all positively correlate with each other, and all three of those variables will negatively correlate with perceived stress. We did find outliers in 2 of our variables. In the variable of Safety there are 15 outliers, and in the variable of Support there is 27 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) Cohen J. (1988). p-value is < .001.The variables all have strong/large effect sizes. All variables are show linearity. There are no issues in kurtosis or skew. The resutls match up with our hypothesis. Reference Table 1.
Variable | M | SD | 1 | 2 | 3 |
---|---|---|---|---|---|
Safety (MOA_SAFETY | -0.00 | 1.00 | |||
Satisfaction With Life (SWB) | -0.00 | 1.00 | .13** | ||
[.08, .17] | |||||
Percieved Support (SUPPORT) | -0.00 | 1.00 | .10** | .47** | |
[.06, .14] | [.43, .50] | ||||
Percieved Stress (STRESS) | 0.00 | 1.00 | -.05* | -.49** | -.21** |
[-.10, -.01] | [-.52, -.46] | [-.25, -.17] | |||
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.