1 Loading Libraries

#install.packages("apaTables")
#install.packages("kableExtra")

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

2 Importing Data

d <- read.csv(file="Data/correlation_labdata.csv", header=T)

# For HW, import the your project dataset you cleaned previously; this will be the dataset you'll use throughout the rest of the semester

3 State Your Hypothesis

We predict that unhappiness, worry, life satisfaction, and fakeness (a fake measure we created) will all be correlated with each other. Additionally, we predict that life satisfaction will be negatively correlated with worry, such that participants who report higher levels of worry will report lower life satisfaction.

4 Check Your Variables

# you only need to check the variables you're using in the current analysis
# it's always a good idea to look them to be sure that everything is correct
str(d)
## 'data.frame':    1250 obs. of  6 variables:
##  $ ID        : int  1 20 30 31 33 57 68 81 86 104 ...
##  $ gender    : chr  "f" "m" "f" "f" ...
##  $ ethnicity : chr  "white" "white" "white" "white" ...
##  $ unhappy   : num  3.25 3.75 1 3.25 2 4 3.75 1.25 2.5 2.5 ...
##  $ worry     : num  1.33 3.33 1 2.33 1.11 ...
##  $ life_satis: num  2.3 1.6 3.9 1.7 3.9 1.8 1.3 3.5 2.6 3 ...
# We're going to create a fake variable for this lab, so that we have four variables. 
# NOTE: YOU WILL SKIP THIS STEP FOR THE HOMEWORK!

d$fake <- (d$unhappy*d$worry)/d$life_satis

# Since we're focusing only on our continuous variables, we're going to subset them into their own dataframe. This will make some stuff we're doing later on easier.

d2 <- subset(d, select=c(unhappy, worry, life_satis, fake))

# You can use the describe() command on an entire dataframe (d) or just on a single variable (d$pss)

describe(d2)
##            vars    n mean   sd median trimmed  mad  min max range  skew
## unhappy       1 1250 2.93 0.95   3.00    2.93 1.11 1.00   5  4.00  0.09
## worry         2 1250 2.07 0.86   1.89    1.99 0.99 1.00   4  3.00  0.64
## life_satis    3 1250 2.63 0.71   2.70    2.65 0.74 1.00   4  3.00 -0.22
## fake          4 1250 3.31 3.39   1.94    2.65 1.77 0.25  20 19.75  1.89
##            kurtosis   se
## unhappy       -0.74 0.03
## worry         -0.69 0.02
## life_satis    -0.70 0.02
## fake           3.75 0.10
# NOTE: Our fake variable has high kurtosis, which we'll ignore for the lab. You don't need to discuss univariate normality in the results write-ups for the labs/homework, but you will need to discuss it in your final project manuscript.

# also use histograms to examine your continuous variables

hist(d$unhappy)

hist(d$worry)

hist(d$life_satis)

hist(d$fake)

# last, use scatterplots to examine your continuous variables together, for each pairing

plot(d$unhappy, d$worry)

plot(d$unhappy, d$life_satis)

plot(d$unhappy, d$fake)

plot(d$worry, d$life_satis)

plot(d$worry, d$fake)

plot(d$life_satis, d$fake)

5 Check Your Assumptions

5.1 Pearson’s Correlation Coefficient Assumptions

  • Should have two measurements for each participant
  • Variables should be continuous and normally distributed
  • Outliers should be identified and removed
  • Relationship between the variables should be linear

5.1.1 Checking for Outliers

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.

# We are going to standardize (z-score) all of our variables, and check them for outliers.

d2$unhappy <- scale(d2$unhappy, center=T, scale=T)
hist(d2$unhappy)

sum(d2$unhappy < -3 | d2$unhappy > 3)
## [1] 0
d2$worry <- scale(d2$worry, center=T, scale=T)
hist(d2$worry)

sum(d2$worry < -3 | d2$worry > 3)
## [1] 0
d2$life_satis <- scale(d2$life_satis, center=T, scale=T)
hist(d2$life_satis)

sum(d2$life_satis < -3 | d2$life_satis > 3)
## [1] 0
d2$fake <- scale(d2$fake, center=T, scale=T)
hist(d2$fake)

sum(d2$fake < -3 | d2$fake > 3)
## [1] 31

5.2 Issues with My Data

Three of my variables meet all of the assumptions of Pearson’s correlation coefficient. One variable, a fake measure of something fake had high kurtosis (3.75) and had 31 outliers. Outliers can distort the relationship between two variables and sway the correlation in their direction. This variable, fake, also appears to have non-linear relationships with the other three variables. Pearson’s r may underestimate the strength of a non-linear relationship and distort the relationship direction. Any correlations with my fake measure of fakeness should be evaluated carefully due to these risks.

[Make sure to revise the above paragraph for your HW.]

6 Run a Single Correlation

corr_output <- corr.test(d2$unhappy, d2$worry)

7 View Single Correlation

corr_output
## Call:corr.test(x = d2$unhappy, y = d2$worry)
## Correlation matrix 
##      [,1]
## [1,] 0.75
## Sample Size 
## [1] 1250
## 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

8 Create a Correlation Matrix

Strong: Between |0.50| and |1| Moderate: Between |0.30| and |0.49| Weak: Between |0.10| and |0.29| Trivial: Less than |0.09|

Remember, Pearson’s r is also an effect size!

corr_output_m <- corr.test(d2)

9 View Test Output

corr_output_m
## Call:corr.test(x = d2)
## Correlation matrix 
##            unhappy worry life_satis  fake
## unhappy       1.00  0.75      -0.74  0.79
## worry         0.75  1.00      -0.75  0.87
## life_satis   -0.74 -0.75       1.00 -0.83
## fake          0.79  0.87      -0.83  1.00
## Sample Size 
## [1] 1250
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##            unhappy worry life_satis fake
## unhappy          0     0          0    0
## worry            0     0          0    0
## life_satis       0     0          0    0
## fake             0     0          0    0
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option
# Remember to report the p-values from the matrix that are ABOVE the diagonal

10 Write Up Results

To test our hypothesis that unhappiness, worry, life satisfaction, and fakeness (a fake measure we created) would be correlated with one another, we calculated a series of Pearson’s correlation coefficients. Three of the variables (unhappiness, worry, and life satisfaction) met the required assumptions of the test, with all three meeting the standards of normality and containing no outliers. One variable, fakeness, had 31 outliers as well as non-linear relationships with the other variables; so any significant results involving fakeness should be evaluated carefully.

As predicted, we found that all four variables were significantly correlated (all ps < .001). The effect sizes of all correlations were large (rs > .5; Cohen, 1988). Our second hypothesis was also supported, that life satisfaction would be lower in participants who reported higher levels of worry, as can be seen by the correlation coefficients reported in Table 1.

[In your HW, revise the above two paragraphs to fit your results. Make sure to discuss ALL predicted correlations and if sig or not.]

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2 3
Unhappiness 2.93 0.95
Worry 2.07 0.86 .75**
[.72, .77]
Life Satisfaction 2.63 0.71 -.74** -.75**
[-.76, -.71] [-.77, -.72]
Fakeness 3.31 3.39 .79** .87** -.83**
[.77, .81] [.85, .88] [-.84, -.81]
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.