Correlation HW

Author

Gabriel Condon

Loading Libraries

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

Importing Data

d <- read.csv(file="Data/mydata.csv", header=T)
# CHANGE FOR HW
# since we're focusing on our continuous variables, we're going to drop our categorical variables. this will make some stuff we're doing later easier.
d <- subset(d, select=-c(urban_rural, sexual_orientation))

State Your Hypotheses - PART OF YOUR WRITEUP

I predict that stress, anxiety, and isolation will be positive correlated, and social support will be negatively correlated with the other three continuous variables.

Check Your Assumptions

Pearson’s Correlation Coefficient Assumptions

  • Should have two measurements for each participant for each variable (confirmed by earlier procedures – we dropped any participants with missing data)
  • Variables should be continuous and normally distributed, or assessments of the relationship may be inaccurate (confirmed above – if issues, make a note and continue)
  • Outliers should be identified and removed, or results will be inaccurate (will do below)
  • Relationship between the variables should be linear, or they will not be detected (will do below)

Checking for Outliers

Outliers can mask potential effects and cause Type II error (you assume there is no relationship when there really is one, e.g., false negative).

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.

# using the scale() command to standardize our variable, viewing a histogram, and then counting statistical outliers
d$gad <- scale(d$gad, center=T, scale=T)
hist(d$gad)

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

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

sum(d$pss < -3 | d$pss > 3)
[1] 0
d$isolation <- scale(d$isolation, center=T, scale=T)
hist(d$isolation)

sum(d$isolation < -3 | d$isolation > 3)
[1] 0

Checking for Linear Relationships

Non-linear relationships cannot be detected by Pearson’s correlation (the type of correlation we’re doing here). This means that you may underestimate the relationship between a pair of variables if they have a non-linear relationship, and thus your understanding of what’s happening in your data will be inaccurate.

Visually check that relationships are linear and write a brief description of any potential nonlinearity. You will have to use your judgement. There are no penalties for answering ‘wrong’, so try not to stress out about it too much – just do your best.

# use scatterplots to examine your continuous variables together
plot(d$gad, d$support)

plot(d$gad, d$pss)

plot(d$gad, d$isolation)

plot(d$support, d$pss)

plot(d$support, d$isolation)

plot(d$pss, d$isolation)

Check Your Variables

describe(d)
          vars    n mean sd median trimmed  mad   min  max range  skew kurtosis
gad          1 1253    0  1  -0.36   -0.10 0.93 -1.15 2.16  3.30  0.69    -0.71
support      2 1253    0  1   0.10    0.06 1.04 -2.71 1.50  4.21 -0.45    -0.53
pss          3 1253    0  1   0.07   -0.01 1.17 -2.02 2.17  4.19  0.08    -0.75
isolation    4 1253    0  1  -0.17   -0.03 1.33 -1.37 1.62  2.98  0.17    -1.28
            se
gad       0.03
support   0.03
pss       0.03
isolation 0.03
# also use histograms to examine your continuous variables
hist(d$gad)

hist(d$support)

hist(d$pss)

hist(d$isolation)

Issues with My Data - PART OF YOUR WRITEUP

I found no outliers in my data, and skew and kurtosis lie within the acceptable range for all four continuous variables (-2 to 2).

Run Pearson’s Correlation

There are two ways to run Pearson’s correlation in R. You can calculate each correlation one-at-a-time using multiple commands, or you can calculate them all at once and report the scores in a matrix. The matrix output can be confusing at first, but it’s more efficient. We’ll do it both ways.

Run a Single Correlation

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

View Single Correlation

  • Strong effect: Between |0.50| and |1|
  • Moderate effect: Between |0.30| and |0.49|
  • Weak effect: Between |0.10| and |0.29|
  • Trivial effect: Less than |0.09|
corr_output
Call:corr.test(x = d$gad, y = d$support)
Correlation matrix 
      [,1]
[1,] -0.46
Sample Size 
[1] 1253
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

Create a Correlation Matrix

corr_output_m <- corr.test(d)

View Test Output

  • Strong effect: Between |0.50| and |1|
  • Moderate effect: Between |0.30| and |0.49|
  • Weak effect: Between |0.10| and |0.29|
  • Trivial effect: Less than |0.09|
corr_output_m
Call:corr.test(x = d)
Correlation matrix 
            gad support   pss isolation
gad        1.00   -0.46  0.76      0.66
support   -0.46    1.00 -0.47     -0.63
pss        0.76   -0.47  1.00      0.65
isolation  0.66   -0.63  0.65      1.00
Sample Size 
[1] 1253
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
          gad support pss isolation
gad         0       0   0         0
support     0       0   0         0
pss         0       0   0         0
isolation   0       0   0         0

 To see confidence intervals of the correlations, print with the short=FALSE option

Write Up Results

My hypothesis was that stress, anxiety, and isolation would be positive correlated, and social support will be negatively correlated with the other three continuous variables. There were no issues found with the data, no outliers were present and skew and kurtosis were within acceptable levels. The results confirm my hypothesis, as shown in table 1. Stress, anxiety, and isolation all show a moderate to strong positive correlation, and support shows a moderate to strong negative correlation with the other variables.

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2 3
Anxiety Score (GAD) -0.00 1.00
Social Support -0.00 1.00 -.46**
[-.51, -.42]
Perceived Stress (PSS) 0.00 1.00 .76** -.47**
[.74, .78] [-.51, -.43]
Social Isolation 0.00 1.00 .66** -.63** .65**
[.62, .69] [-.66, -.59] [.62, .68]
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.