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 mental well being will be negatively correlated with disordered eating, such that participants who report higher levels of disordered eating will report lower mental well being.

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':    390 obs. of  7 variables:
##  $ X          : int  6291 6294 6298 6312 6326 6330 6331 6335 6348 6350 ...
##  $ urban_rural: chr  "town" "city" "town" "town" ...
##  $ education  : chr  "4 equivalent to AP/IB completion" "prefer not to say" "4 equivalent to AP/IB completion" "4 equivalent to AP/IB completion" ...
##  $ edeq12     : num  2.5 2.17 2.08 1.17 2.17 ...
##  $ mfq_state  : num  4 4 2.75 3.12 3.12 ...
##  $ swemws     : num  3.14 2.57 2.14 2.29 1.86 ...
##  $ brs        : num  4 2.5 1.17 2.33 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(edeq12, mfq_state, swemws, brs))

# 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
## edeq12       1 390 2.11 0.80   2.00    2.07 0.99   1   4     3  0.34    -1.00
## mfq_state    2 390 3.74 1.01   3.81    3.77 1.02   1   6     5 -0.28    -0.29
## swemws       3 390 2.79 0.86   2.71    2.79 0.85   1   5     4  0.06    -0.48
## brs          4 390 2.69 0.88   2.67    2.69 0.99   1   5     4  0.09    -0.65
##             se
## edeq12    0.04
## mfq_state 0.05
## swemws    0.04
## brs       0.04
# 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$edeq12)

hist(d$swemws)

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

plot(d$edeq12, d$swemws)

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

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

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

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

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

5.2 Issues with My Data

Both of my variables meet all of the assumptions of Pearson’s correlation coefficient.

[Make sure to revise the above paragraph for your HW.]

6 Run a Single Correlation

corr_output <- corr.test(d2$edeq12, d2$swemws)

7 View Single Correlation

corr_output
## Call:corr.test(x = d2$edeq12, y = d2$swemws)
## Correlation matrix 
##       [,1]
## [1,] -0.48
## Sample Size 
## [1] 390
## 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

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(d2)

9 View Test Output

corr_output_m
## Call:corr.test(x = d2)
## Correlation matrix 
##           edeq12 mfq_state swemws   brs
## edeq12      1.00     -0.33  -0.48 -0.41
## mfq_state  -0.33      1.00   0.70  0.68
## swemws     -0.48      0.70   1.00  0.66
## brs        -0.41      0.68   0.66  1.00
## Sample Size 
## [1] 390
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##           edeq12 mfq_state swemws brs
## edeq12         0         0      0   0
## mfq_state      0         0      0   0
## swemws         0         0      0   0
## brs            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

10 Write Up Results

To test our hypothesis that mental well being and disordered eating would be correlated with one another, we calculated a series of Pearson’s correlation coefficients. Both of the variables (mental well being and disordered eating) met the required assumptions of the test, with both meeting the standards of normality and containing no outliers.

As predicted, we found that both variables were significantly correlated (all ps < .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 2 3
Disordered Eating 2.11 0.80
Mental Flexibility 2.79 0.86 -.48**
[-.55, -.40]
Mental Wellbeing 3.74 1.01 -.33** .70**
[-.41, -.24] [.64, .74]
Resilience 2.69 0.88 -.41** .66** .68**
[-.49, -.32] [.60, .72] [.62, .73]
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.