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="C:/Users/jd/OneDrive - Indiana University/M311/Research/Labs/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

There will be a significant relationship between unhappiness, worry, and life satisfaction. Specifically, unhappiness will be positively related to worry and negatively related to life satisfaction, and worry will be negatively related to life satisfaction.

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 - mean(d$unhappy, na.rm=TRUE))^8

# 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$fake)
##  num [1:1250] 9.79e-05 1.95e-01 1.96e+02 9.79e-05 5.82e-01 ...
# 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   5.00   4.00  0.09
## worry         2 1250  2.07  0.86   1.89    1.99 0.99   1   4.00   3.00  0.64
## life_satis    3 1250  2.63  0.71   2.70    2.65 0.74   1   4.00   3.00 -0.22
## fake          4 1250 15.68 50.87   0.05    2.83 0.07   0 331.16 331.16  4.61
##            kurtosis   se
## unhappy       -0.74 0.03
## worry         -0.69 0.02
## life_satis    -0.70 0.02
## fake          22.76 1.44
# 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(d2$unhappy)

hist(d2$worry)

hist(d2$life_satis)

hist(d2$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(d2$unhappy, d2$worry)

plot(d2$unhappy, d2$life_satis)

plot(d2$unhappy, d2$fake)

plot(d2$worry, d2$life_satis)

plot(d2$worry, d2$fake)

plot(d2$life_satis, d2$fake)

5 Check Your Assumptions

5.1 Pearson’s Correlation Coefficient Assumptions

  • Should have independence of observations 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.

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

sum(z_unhappy < -3 | z_unhappy > 3)
## [1] 0
# Worry
z_worry <- scale(d2$worry, center=TRUE, scale=TRUE)
hist(z_worry)

sum(z_worry < -3 | z_worry > 3)
## [1] 0
# Life satisfaction
z_life_satis <- scale(d2$life_satis, center=TRUE, scale=TRUE)
hist(z_life_satis)

sum(z_life_satis < -3 | z_life_satis > 3)
## [1] 0
# Fake variable
z_fake <- scale(d2$fake, center=TRUE, scale=TRUE)
hist(z_fake)

sum(z_fake < -3 | z_fake > 3)
## [1] 42
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   5.00   4.00  0.09
## worry         2 1250  2.07  0.86   1.89    1.99 0.99   1   4.00   3.00  0.64
## life_satis    3 1250  2.63  0.71   2.70    2.65 0.74   1   4.00   3.00 -0.22
## fake          4 1250 15.68 50.87   0.05    2.83 0.07   0 331.16 331.16  4.61
##            kurtosis   se
## unhappy       -0.74 0.03
## worry         -0.69 0.02
## life_satis    -0.70 0.02
## fake          22.76 1.44

5.2 Issues with My Data

Three of my variables (unhappiness, worry, and life satisfaction) approximately met the normality assumption of Pearson’s correlation coefficient and had no outliers. One variable, fake, had high kurtosis (22.76) and 42 outliers. Outliers can distort the relationship between two variables and sway the correlation in their direction. This variable, fake, also had high positive skewness (4.61), indicating that it was not normally distributed. Pearson’s r may underestimate the strength of a non-linear relationship and distort the relationship direction. Any correlations with fake 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 <- cor.test(d2$unhappy, d2$worry)

7 View Single Correlation

corr_output
## 
##  Pearson's product-moment correlation
## 
## data:  d2$unhappy and d2$worry
## t = 40.019, df = 1248, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.7243515 0.7730026
## sample estimates:
##       cor 
## 0.7496882

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 worry life_satis  fake
## unhappy       1.00  0.75      -0.74  0.13
## worry         0.75  1.00      -0.75  0.17
## life_satis   -0.74 -0.75       1.00 -0.10
## fake          0.13  0.17      -0.10  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!

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, worry, and life satisfaction would be correlated with each other, we calculated a matrix of Pearson’s correlation coefficients. Three of the variables (unhappiness, worry, and life satisfaction) approximately met the required assumptions of the test, with all three meeting the standards of normality and containing no outliers. One variable, fake, had high kurtosis (22.76), 42 outliers, and a highly skewed distribution (4.61); so any significant results involving fakeness should be evaluated carefully.

As predicted, we found that unhappiness, worry, and life satisfaction were significantly correlated (all ps < .001). The effect sizes of all three predicted correlations were strong (all rs > .50; Cohen, 1988). Additionally, unhappiness was found to be positively related to worry (r = .75, p < .001), unhappiness was negatively related to life satisfaction (r = -.74, p < .001), and worry was negatively related to life satisfaction (r = -.75, p < .001), 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 3
Unhappiness 2.93 0.95
2.07 0.86 .75**
Worry [.72, .77]
2.63 0.71 -.74** -.75**
Life Satisfaction [-.76, -.71] [-.77, -.72]
15.68 50.87 .13** .17** -.10**
Fake Variable [.08, .19] [.11, .22] [-.16, -.05]
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.