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 there will be a significant relationship between unhappiness, life satisfaction, and fakeness (a fake variable we created for this lab). Specifically, unhappiness will be negatively related to life satisfaction but positively related to fakeness.

In your HW, paste in your correlation hypothesis from your Hypothesis Activity assignment – make any necessary revisions based on grader feedback. Delete this reminder in your HW.]

4 Check Your Variables

# 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! DELETE LINES 48-52!

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

# 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  7 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 ...
##  $ fake      : num  1.884 7.812 0.256 4.461 0.57 ...
# 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, 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
## life_satis    2 1250 2.63 0.71   2.70    2.65 0.74 1.00   4  3.00 -0.22
## fake          3 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
## 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 because we created it to be problematic. If you have high skew or kurtosis for any of your project variables, you will need to discuss it below in the Issues with My Data and Write up Results sections, as well as in your final project manuscript if your data does not meet the normality assumption.


# also use histograms to examine your continuous variables
# Because we are looking at 3 variables, we will have 3 histograms.

hist(d$unhappy)

hist(d$life_satis)

hist(d$fake)

# last, use scatterplots to examine your continuous variables together, for each pairing
# because we are looking at 3 variables, we will have 3 pairings/plots. 

plot(d$unhappy, d$life_satis)

plot(d$unhappy,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: For correlations, you will NOT screen out outliers or take any action based on what you see here. This is something you will simply check and then discuss in your write-up.We will learn how to removed outliers in later analyses.

# We are going to standardize (z-score) all of our 3 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$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

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

[Make sure to revise the above paragraph for your HW. If you do not have any non-linear relationships (which hopefully you won’t), remove those sentences as they will no longer be relevant. Delete this reminder in your HW.]

6 Run a Single Correlation

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

7 View Single Correlation

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

corr_output_m <- corr.test(d2)

9 View Test Output

corr_output_m
## Call:corr.test(x = d2)
## Correlation matrix 
##            unhappy life_satis  fake
## unhappy       1.00      -0.74  0.79
## life_satis   -0.74       1.00 -0.83
## fake          0.79      -0.83  1.00
## Sample Size 
## [1] 1250
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##            unhappy life_satis fake
## unhappy          0          0    0
## life_satis       0          0    0
## fake             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!

Remember, Pearson’s r is also an effect size! We don’t report effect sizes for non-sig correlations.

  • 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|

10 Write Up Results

To test our hypothesis that unhappiness, life satisfaction, and fakeness (a fake variable we created for this lab) would be correlated with one another, we calculated a series of Pearson’s correlation coefficients. Two of the variables (unhappiness and life satisfaction) met the required assumptions of the test, with both meeting the standards of normality and containing no outliers. One variable, fakeness, had high kurtosis (3.75), 31 outliers as well as nonlinear relationships with the other variables; so any significant results involving fakeness should be evaluated carefully.

As predicted, we found that all three variables were significantly correlated (all ps < 0.001). The effect sizes of all correlations were strong (rs > 0.05; Cohen, 1988). Additionally, unhappiness was found to be negatively related to life satisfaction, and positively related to fakeness, as predicted. Please refer to 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 whether supported or not. Always report the Pearson’s r and p-value for every prediction. If your p-values vary (some are < .05 while others are <.001), then you can say that they are all less than the largest of the major sig division: .05, .01, .001. Similarly, if your effect sizes vary, make sure to update the wording above and specify which were trivial/small/medium/large based on what you have (i.e., do not state any size you did not find). Delete this reminder in your HW.]

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