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="/Users/ethanshinaver/Library/CloudStorage/OneDrive-Personal/Social Psych Lab/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

We predict there will be a significant relationship between unhappiness, life satisfaction, and fakeness (a fake variable we created for this lab). Additionally, unhappiness will be negatively related to life satisfaction, but positively related to fakeness

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
d$fake <- (d$unhappy*d$worry)/d$life_satis

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 non-linear 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.

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
print(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 non-linear 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 < .001). The effect sizes of all correlations were large (rs > .5; 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.

table_out <- apa.cor.table(d2, filename = "table1.doc", table.number = 1)
table_out2 <- as.data.frame(table_out$table.body)

# you will need to update the variable names (in the first set of quotes on each line) for your HW table.
# make sure you enter the variables here in the same order they appear in your matrix output. You can use the names() command to double-check the order
# also make sure you don't change any of the other code -- all you should update are the variable names/labels! changing anything else will cause R to error

table_out2$Variable <- c("Unhappiness","", 
                         " Life Satisfaction", "", "", 
                         "Fakeness", "", "")


# DO NOT make any changes to the remaining code

as.data.frame(table_out2) %>%
  kbl(row.names = F, 
      align = c("l", "c", "c", "c", "c", "c"),
      caption = paste("Table ",table_out$table.number,": ",table_out$table.title, sep=""),
      format = "html", table.attr = "style='width: 75%;'") %>%
  kable_classic() %>%
  footnote(
    general = "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.",
    symbol = c("indicates p < .05", "indicates p < .01."), symbol_manual = c("*","**"),
    threeparttable = T)
Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2
Unhappiness -0.00 1.00
Life Satisfaction -0.00 1.00 -.74**
[-.76, -.71]
Fakeness 0.00 1.00 .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.