Correlation HW

Author

Morgan Bejna

Loading Libraries

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

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, age))

State Your Hypotheses - PART OF YOUR WRITEUP

We predict that satisfaction with life, self-efficacy, and perceived social support will be positively correlated, and all three variables will be negatively correlated with 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
d$swb <- scale(d$swb, center=T, scale=T)
hist(d$swb)

sum(d$swb < -3 | d$swb > 3)
[1] 0
d$efficacy <- scale(d$efficacy, center=T, scale=T)
hist(d$efficacy)

sum(d$efficacy < -3 | d$rse > 3)
[1] 0
d$support <- scale(d$support, center=T, scale=T)
hist(d$support)

sum(d$support < -3 | d$support > 3)
[1] 28
d$stress <- scale(d$stress, center=T, scale=T)
hist(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$swb, d$efficacy)

plot(d$swb, d$support)

plot(d$swb, d$stress)

plot(d$efficacy, d$support)

plot(d$efficacy, d$stress)

plot(d$support, d$stress)

Check Your Variables

describe(d)
         vars    n mean sd median trimmed  mad   min  max range  skew kurtosis
swb         1 2163    0  1   0.05    0.04 1.12 -2.59 1.93  4.52 -0.35    -0.49
efficacy    2 2163    0  1  -0.03    0.01 1.00 -4.31 2.00  6.31 -0.19     0.36
support     3 2163    0  1   0.19    0.11 0.87 -4.86 1.29  6.16 -1.09     1.34
stress      4 2163    0  1   0.06    0.00 0.99 -2.96 2.57  5.53 -0.01    -0.15
           se
swb      0.02
efficacy 0.02
support  0.02
stress   0.02
# also use histograms to examine your continuous variables
hist(d$swb)

hist(d$efficacy)

hist(d$support)

hist(d$stress)

Issues with My Data - PART OF YOUR WRITEUP

Our analysis identified 28 outliers in the support variable. This is a potential issue because outliers can significantly skew results or cause misleading conclusions. However, there were no other potential issues with our 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_output <- corr.test(d$swb, d$efficacy)

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$swb, y = d$efficacy)
Correlation matrix 
     [,1]
[1,] 0.37
Sample Size 
[1] 2163
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_m
Call:corr.test(x = d)
Correlation matrix 
           swb efficacy support stress
swb       1.00     0.37    0.47  -0.49
efficacy  0.37     1.00    0.20  -0.40
support   0.47     0.20    1.00  -0.21
stress   -0.49    -0.40   -0.21   1.00
Sample Size 
[1] 2163
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
         swb efficacy support stress
swb        0        0       0      0
efficacy   0        0       0      0
support    0        0       0      0
stress     0        0       0      0

 To see confidence intervals of the correlations, print with the short=FALSE option

Write Up Results

We hypothesized that satisfaction with life, self-efficacy, and perceived social support would be positively correlated and that all three variables would be negatively correlated with perceived stress.

Regarding potential issues with our data, we identified 28 outliers in the support variable. Outliers can skew results and lead to misleading conclusions; however, no other data issues were detected.

As shown in Table 1, our results supported our hypothesis. Satisfaction with life was significantly and positively correlated with both self-efficacy (p < .01) and perceived social support (p < .01), with both demonstrating moderate effects (Cohen, 1988). Self-efficacy was positively correlated with perceived social support (p < .01), though this relationship demonstrated a weak effect (Cohen, 1988).

Finally, as predicted, perceived stress was significantly and negatively correlated with satisfaction with life (p < .01), self-efficacy (p < .01), and perceived social support (p < .01). The correlations with both satisfaction with life and self-efficacy demonstrated moderate effects, while the correlation with perceived social support demonstrated a weak effect (Cohen, 1988).

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2 3
Satisfaction with Life (SWB) -0.00 1.00
Self-Efficacy -0.00 1.00 .37**
[.33, .41]
Perceived Social Support 0.00 1.00 .47** .20**
[.43, .50] [.16, .24]
Perceived Stress -0.00 1.00 -.49** -.40** -.21**
[-.52, -.46] [-.43, -.36] [-.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.