Correlation HW

Author

Jacie Rayburn

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 mindfulness and efficacy will be negatively correlated with stress, and social media use will be positively 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$mindful <- scale(d$mindful, center=T, scale=T)
hist(d$mindful)

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

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

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

sum(d$socmeduse < -3 | d$socmeduse > 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$mindful, d$efficacy)

plot(d$mindful, d$stress)

plot(d$mindful, d$socmeduse)

plot(d$efficacy, d$stress)

plot(d$efficacy, d$socmeduse)

plot(d$stress, d$socmeduse)

Check Your Variables

describe(d)
          vars    n mean sd median trimmed  mad   min  max range  skew kurtosis
mindful      1 2153    0  1   0.02    0.01 0.94 -3.07 2.71  5.79 -0.04    -0.15
efficacy     2 2153    0  1  -0.03    0.01 1.00 -4.31 2.00  6.31 -0.19     0.36
stress       3 2153    0  1   0.06    0.00 0.99 -2.96 2.57  5.53 -0.01    -0.15
socmeduse    4 2153    0  1   0.09    0.03 0.86 -2.71 2.42  5.12 -0.31     0.20
            se
mindful   0.02
efficacy  0.02
stress    0.02
socmeduse 0.02
# also use histograms to examine your continuous variables
hist(d$mindful)

hist(d$efficacy)

hist(d$stress)

hist(d$socmeduse)

Issues with My Data - PART OF YOUR WRITEUP

We had 1 outlier for the variable mindfulness and 10 outliers for the variable efficacy. Outliers are important because it can skew your data. There were no problems with linearity, skew, or kurtosis.

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$mindful, 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$mindful, y = d$efficacy)
Correlation matrix 
     [,1]
[1,] 0.25
Sample Size 
[1] 2153
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 
          mindful efficacy stress socmeduse
mindful      1.00     0.25  -0.40     -0.12
efficacy     0.25     1.00  -0.40      0.03
stress      -0.40    -0.40   1.00      0.11
socmeduse   -0.12     0.03   0.11      1.00
Sample Size 
[1] 2153
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
          mindful efficacy stress socmeduse
mindful         0     0.00      0      0.00
efficacy        0     0.00      0      0.23
stress          0     0.00      0      0.00
socmeduse       0     0.23      0      0.00

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

Write Up Results

We predicted that there would be a negative relationship between mindfulness and efficacy with stress, and a positive relationship between social media use and stress. We had 1 outlier for the variable mindfulness and 10 outliers for the variable efficacy. Outliers are important because it can skew your data. There were no problems with linearity, skew, or kurtosis. Our data supported our hypothesis, except there was no significant relationship between efficacy and social media use (see table 1). The effect sizes between mindfulness and efficacy with stress and are trivial (see talbe 1). The effect size between stress and social media, and efficacy and mindfulness use is weak (see table 1).

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2 3
Mindfulness 0.00 1.00
Efficacy -0.00 1.00 .25**
[.21, .29]
Stress -0.00 1.00 -.40** -.40**
[-.44, -.37] [-.43, -.36]
Social Meida Use 0.00 1.00 -.12** .03 .11**
[-.16, -.07] [-.02, .07] [.07, .15]
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.