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/projectdata.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 social support will be negatively correlated with general anxiety disorder, such that participants who report higher levels of social support will report lower levels of general anxiety.

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':    1238 obs. of  7 variables:
##  $ X       : int  1 321 401 469 520 1390 1422 1849 2183 2247 ...
##  $ gender  : chr  "female" "male" "female" "female" ...
##  $ mhealth : chr  "none or NA" "none or NA" "obsessive compulsive disorder" "depression" ...
##  $ mfq_26  : num  4.2 4.3 4.7 4.6 2.7 2.95 2.95 2.4 3.65 4.7 ...
##  $ support : num  2.5 2.5 3.83 4.67 2.83 ...
##  $ gad     : num  1.86 1 2.14 1.71 1.14 ...
##  $ big5_ext: num  2 4.67 4.67 6 2 ...
# 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(support, gad))

# 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 kurtosis
## support    1 1238 3.57 0.94   3.67    3.62 0.99   1   5     4 -0.43    -0.53
## gad        2 1238 2.04 0.91   1.71    1.95 0.85   1   4     3  0.69    -0.71
##           se
## support 0.03
## gad     0.03
# 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$support)

hist(d$gad)

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

plot(d$support, d$gad)

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 always check and then discuss in your write-up.

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

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

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

sum(d2$gad < -3 | d2$gad > 3)
## [1] 0

5.2 Issues with My Data

Both of my variables meet all of the assumptions of Pearson’s correlation coefficient. There were no outliers for either variable. Outliers can distort the relationship between two variables and sway the correlation in their direction. Pearson’s r may underestimate the strength of a non-linear relationship and distort the relationship direction.

6 Run a Single Correlation

corr_output <- corr.test(d$support, d$gad)

7 View Single Correlation

corr_output
## Call:corr.test(x = d$support, y = d$gad)
## Correlation matrix 
## [1] -0.46
## Sample Size 
## [1] 1238
## These are the unadjusted probability values.
##   The probability values  adjusted for multiple tests are in the p.adj object. 
## [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(d$support, d$gad)

9 View Test Output

corr_output_m
## Call:corr.test(x = d$support, y = d$gad)
## Correlation matrix 
## [1] -0.46
## Sample Size 
## [1] 1238
## These are the unadjusted probability values.
##   The probability values  adjusted for multiple tests are in the p.adj object. 
## [1] 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 social support and general anxiety would be correlated with one another, we calculated a series of Pearson’s correlation coefficients. Both of the variables met the required assumptions of the test, while also meeting the standards of normality and containing no outliers.

As predicted, we found that both variables were significantly correlated (-0.46 < .001). The effect sizes of all correlations were large (rs > .5; Cohen, 1988).

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1
support 3.57 0.94
gad 2.04 0.91 -.46**
[-.51, -.42]
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.