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(edu, party_rc)) d
State Your Hypotheses - PART OF YOUR WRITEUP
We predict that mindfulness, support, and satisfaction with life will be positively correlated, and that all three of these variables will be negatively correlated with narcissistic personality inventory.
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
In the npi, and swb variables there are no outliers present. Meanwhile, the mindful variable has two outliers and the support variable has 46. 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
$npi <- scale(d$npi, center=T, scale=T)
dhist(d$npi)
sum(d$npi < -3 | d$npi > 3)
[1] 0
$mindful <- scale(d$mindful, center=T, scale=T)
dhist(d$mindful)
sum(d$mindful < -3 | d$mindful > 3)
[1] 2
$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] 46
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$npi, d$mindful)
plot(d$npi, d$swb)
plot(d$npi, d$support)
plot(d$mindful, d$swb)
plot(d$mindful, d$support)
plot(d$swb, d$support)
Check Your Variables
describe(d)
vars n mean sd median trimmed mad min max range skew kurtosis
npi 1 3141 0 1 -0.41 -0.13 0.74 -0.91 2.34 3.25 0.94 -0.69
mindful 2 3141 0 1 0.03 0.01 0.94 -3.05 2.72 5.77 -0.06 -0.14
swb 3 3141 0 1 0.14 0.04 1.12 -2.63 1.91 4.54 -0.36 -0.46
support 4 3141 0 1 0.19 0.11 0.87 -4.88 1.29 6.17 -1.12 1.50
se
npi 0.02
mindful 0.02
swb 0.02
support 0.02
#
# also use histograms to examine your continuous variables
hist(d$npi)
hist(d$mindful)
hist(d$swb)
hist(d$support)
Issues with My Data - PART OF YOUR WRITEUP
There is little to no linearity between the npi variable and any other variable, but there is also no skew or kurtosis within my data.
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$npi, d$mindful) 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$npi, y = d$mindful)
Correlation matrix
[,1]
[1,] -0.02
Sample Size
[1] 3141
These are the unadjusted probability values.
The probability values adjusted for multiple tests are in the p.adj object.
[,1]
[1,] 0.29
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
npi mindful swb support
npi 1.00 -0.02 0.03 -0.02
mindful -0.02 1.00 0.29 0.18
swb 0.03 0.29 1.00 0.47
support -0.02 0.18 0.47 1.00
Sample Size
[1] 3141
Probability values (Entries above the diagonal are adjusted for multiple tests.)
npi mindful swb support
npi 0.00 0.4 0.19 0.4
mindful 0.29 0.0 0.00 0.0
swb 0.06 0.0 0.00 0.0
support 0.20 0.0 0.00 0.0
To see confidence intervals of the correlations, print with the short=FALSE option
Write Up Results
We predicted that mindfulness, support, and satisfaction with life will be positively correlated, and that all three of these variables will be negatively correlated with narcissistic personality inventory. In the npi, and swb variables there are no outliers present. Meanwhile, the mindful variable has two outliers and the support variable has 46. 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). In the single correlation test, there was a weak effect (p=0.29), as well as a weak effect in the correlation matrix (p=.29). Refer to Table 1.
Variable M SD 1 2 3
X. 1. npi 0.00 1.00
X..1
X..2 2. mindful 0.00 1.00 -.02
X..3 [-.05, .02]
X..4
X..5 3. swb -0.00 1.00 .03 .29**
X..6 [-.00, .07] [.25, .32]
X..7
X..8 4. support 0.00 1.00 -.02 .18** .47**
X..9 [-.06, .01] [.15, .22] [.44, .49]
X..10
Variable | M | SD | 1 | 2 | 3 |
---|---|---|---|---|---|
Intolerance of Uncertainty | 0.00 | 1.00 | |||
Self-esteem (RSE-10) | 0.00 | 1.00 | -.02 | ||
[-.05, .02] | |||||
Depression Score (PHQ-9) | -0.00 | 1.00 | .03 | .29** | |
[-.00, .07] | [.25, .32] | ||||
Percieved Stress (PSS) | 0.00 | 1.00 | -.02 | .18** | .47** |
[-.06, .01] | [.15, .22] | [.44, .49] | |||
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.