1 Correlation: 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
library(broom) # for the augment() command
library(ggplot2) # to visualize our results
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha

2 Correlation: Importing Data

# import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
# use ARC data downloaded previous for lab
d <- read.csv("Data/arc_data_finalclean.csv", header=T)

3 Correlation: State Your Hypothesis

We predict that intolerance of uncertainty (measured by the iou), mentalfFlexibility trait (measured by the mfq_26), pandemic anxiety scale (measured by the pas_covid), and perceived stress (pss) will all be correlated with each other. Furthermore, we predict that self-esteem will be lower in participants who are higher in stress or who report more symptoms of depression.

4 Correlation: Check Your Variables

# you only need to check the variables you're using in the current analysis
# although you checked them previously, it's always a good idea to look them over again and be sure that everything is correct
str(d)
## 'data.frame':    1182 obs. of  6 variables:
##  $ sleep_hours: chr  "3 7-8 hours" "2 5-6 hours" "3 7-8 hours" "2 5-6 hours" ...
##  $ mhealth    : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ iou        : num  3.19 4 1.59 3.37 1.7 ...
##  $ mfq_26     : num  4.2 3.35 4.65 4.65 4.5 4.3 5.25 4.45 4.7 4.05 ...
##  $ pas_covid  : num  3.22 4.56 3.33 4.22 3.22 ...
##  $ pss        : num  3.25 3.75 1 3.25 2 2 4 1.25 1.25 2.5 ...
# since we're focusing on our continuous variables, we're going to subset them into their own dataframe. this will make some stuff we're doing later easier.
cont <- subset(d, select=c(iou, mfq_26, pas_covid, pss))

# you can use the describe() command on an entire dataframe (d) or just on a single variable (d$pss)
describe(cont)
##           vars    n mean   sd median trimmed  mad min max range  skew kurtosis
## iou          1 1182 2.56 0.90   2.41    2.50 0.99 1.0   5   4.0  0.50    -0.59
## mfq_26       2 1182 4.31 0.67   4.35    4.33 0.67 1.8   6   4.2 -0.32     0.14
## pas_covid    3 1182 3.23 0.68   3.22    3.24 0.66 1.0   5   4.0 -0.19    -0.04
## pss          4 1182 2.94 0.95   3.00    2.93 1.11 1.0   5   4.0  0.06    -0.76
##             se
## iou       0.03
## mfq_26    0.02
## pas_covid 0.02
## pss       0.03
# our fake variable has high kurtosis, which I'll ignore. 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 manuscript

# also use histograms to examine your continuous variables
hist(d$iou) 

hist(d$mfq_26) 

hist(d$pas_covid)

hist(d$pss)

# last, use scatterplots to examine your continuous variables together
plot(d$iou, d$mfq_26)

plot(d$iou, d$pas_covid)

plot(d$iou, d$pss)

plot(d$mfq_26, d$pas_covid)

plot(d$mfq_26, d$pss)

plot(d$pas_covid, d$pss)

5 Correlation: Check Your Assumptions

5.1 Correlation: 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 Correlation: 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.

d$iou_std <- scale(d$iou, center=T, scale=T)
hist(d$iou_std)

sum(d$iou_std < -3 | d$iou_std > 3)
## [1] 0
d$mfq_26_std <- scale(d$mfq_26, center=T, scale=T)
hist(d$mfq_26_std)

sum(d$mfq_26_shq < -3 | d$mfq_26_shq > 3)
## [1] 0
d$pas_covid_std <- scale(d$pas_covid, center=T, scale=T)
hist(d$pas_covid_std)

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

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

5.2 Correlation: Issues with My Data

All but one of my variables meet all of the assumptions of Pearson’s correlation coefficient. One variable, the Pandemic Anxiety Scale (pas_covid) has 1 outlier. Outliers can distort the relationship between two variables and sway the correlation in their direction. This variable also appears to have non-linear relationships with the other variables. Pearson’s r may underestimate the strength of a non-linear relationship and distort the relationship direction. Any correlations with my fake measure of fakeness should be evaluated carefully due to these risks.

6 Correlation: Create a Correlation Matrix

corr_output_m <- corr.test(cont)

7 Correlation: View Test Output

corr_output_m
## Call:corr.test(x = cont)
## Correlation matrix 
##             iou mfq_26 pas_covid   pss
## iou        1.00  -0.60      0.37  0.63
## mfq_26    -0.60   1.00     -0.20 -0.52
## pas_covid  0.37  -0.20      1.00  0.34
## pss        0.63  -0.52      0.34  1.00
## Sample Size 
## [1] 1182
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##           iou mfq_26 pas_covid pss
## iou         0      0         0   0
## mfq_26      0      0         0   0
## pas_covid   0      0         0   0
## pss         0      0         0   0
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option

8 Correlation: Write Up Results

To test our hypothesis that intolerance of uncertainty, mental flexibility trait, pandemic anxiety, and perceived stress will all be correlated with each other, we calculated a series of Pearson’s correlation coefficients. Most of our data met the assumptions of the test, with all variables meeting the standards of normality and no outliers. One variable, pandemic anxiety, did have one outlier, so any significant results involving that variable should be evaluated carefully.

As predicted, we found that all three variables were significantly correlated (all ps < .01). But not all the effect sizes of all correlations were large (rs > .5; Cohen, 1988). This test also supported our second hypothesis, that self-esteem would be lower in participants who are higher in stress or who report more symptoms of depression, as can be seen by the correlation coefficients reported in Table 1.

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2 3
Intolerance of Uncertainty 2.56 0.90
Mental Flexibility Trait 4.31 0.67 -.60**
[-.63, -.56]
Pandemic Anxiety 3.23 0.68 .37** -.20**
[.32, .42] [-.25, -.14]
Perceived Stress 2.94 0.95 .63** -.52** .34**
[.60, .66] [-.56, -.47] [.29, .39]
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.

9 Regression: State Your Hypothesis

We hypothesize that mental flexibility will significantly predict perceived stress, and that the relationship will be negative.

10 Regression: Check Your Variables

# you only need to check the variables you're using in the current analysis
# although you checked them previously, it's always a good idea to look them over again and be sure that everything is correct
str(d)
## 'data.frame':    1182 obs. of  10 variables:
##  $ sleep_hours  : chr  "3 7-8 hours" "2 5-6 hours" "3 7-8 hours" "2 5-6 hours" ...
##  $ mhealth      : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ iou          : num  3.19 4 1.59 3.37 1.7 ...
##  $ mfq_26       : num  4.2 3.35 4.65 4.65 4.5 4.3 5.25 4.45 4.7 4.05 ...
##  $ pas_covid    : num  3.22 4.56 3.33 4.22 3.22 ...
##  $ pss          : num  3.25 3.75 1 3.25 2 2 4 1.25 1.25 2.5 ...
##  $ iou_std      : num [1:1182, 1] 0.694 1.596 -1.07 0.899 -0.947 ...
##   ..- attr(*, "scaled:center")= num 2.56
##   ..- attr(*, "scaled:scale")= num 0.903
##  $ mfq_26_std   : num [1:1182, 1] -0.165 -1.426 0.503 0.503 0.281 ...
##   ..- attr(*, "scaled:center")= num 4.31
##   ..- attr(*, "scaled:scale")= num 0.674
##  $ pas_covid_std: num [1:1182, 1] -0.00618 1.94253 0.15621 1.45535 -0.00618 ...
##   ..- attr(*, "scaled:center")= num 3.23
##   ..- attr(*, "scaled:scale")= num 0.684
##  $ pss_std      : num [1:1182, 1] 0.33 0.857 -2.042 0.33 -0.988 ...
##   ..- attr(*, "scaled:center")= num 2.94
##   ..- attr(*, "scaled:scale")= num 0.949
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d)
##               vars    n mean   sd median trimmed  mad   min  max range  skew
## sleep_hours*     1 1182 2.90 0.97   3.00    2.92 1.48  1.00 5.00  4.00  0.00
## mhealth*         2 1182 4.63 1.41   5.00    4.87 0.00  1.00 8.00  7.00 -1.45
## iou              3 1182 2.56 0.90   2.41    2.50 0.99  1.00 5.00  4.00  0.50
## mfq_26           4 1182 4.31 0.67   4.35    4.33 0.67  1.80 6.00  4.20 -0.32
## pas_covid        5 1182 3.23 0.68   3.22    3.24 0.66  1.00 5.00  4.00 -0.19
## pss              6 1182 2.94 0.95   3.00    2.93 1.11  1.00 5.00  4.00  0.06
## iou_std          7 1182 0.00 1.00  -0.17   -0.06 1.09 -1.73 2.70  4.43  0.50
## mfq_26_std       8 1182 0.00 1.00   0.06    0.03 0.99 -3.73 2.51  6.23 -0.32
## pas_covid_std    9 1182 0.00 1.00  -0.01    0.02 0.96 -3.25 2.59  5.85 -0.19
## pss_std         10 1182 0.00 1.00   0.07   -0.01 1.17 -2.04 2.17  4.22  0.06
##               kurtosis   se
## sleep_hours*     -0.48 0.03
## mhealth*          2.52 0.04
## iou              -0.59 0.03
## mfq_26            0.14 0.02
## pas_covid        -0.04 0.02
## pss              -0.76 0.03
## iou_std          -0.59 0.03
## mfq_26_std        0.14 0.03
## pas_covid_std    -0.04 0.03
## pss_std          -0.76 0.03
# also use histograms to examine your continuous variables
hist(d$mfq_26)

hist(d$pss)

# last, use scatterplots to examine your continuous variables together
plot(d$mfq_26, d$pss)

11 Regression: Run a Simple Regression

# to calculate standardized coefficients, we have to standardize our IV
d$mfq_26_std <- scale(d$mfq_26, center=T, scale=T)
hist(d$mfq_26_std)

# use the lm() command to run the regression
# dependent/outcome variable on the left, idependent/predictor variable on the right
reg_model <- lm(pss ~ mfq_26_std, data = d)

12 Regression: Check Your Assumptions

12.1 Regression: Simple Regression 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
  • Residuals should be normal and have constant variance note: we will not be evaluating whether our data meets these assumptions in this lab/homework – we’ll come back to them next week when we talk about multiple linear regression

12.2 Regression: Create plots and view residuals

model.diag.metrics <- augment(reg_model)

ggplot(model.diag.metrics, aes(x = mfq_26_std, y = pss)) +
  geom_point() +
  stat_smooth(method = lm, se = FALSE) +
  geom_segment(aes(xend = mfq_26_std, yend = .fitted), color = "red", size = 0.3)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'

12.3 Regression: Check linearity with Residuals vs Fitted plot

This plot (below) shows the residuals for each case and the fitted line. The red line is the average residual for the specified point of the dependent variable. If the assumption of linearity is met, the red line should be horizontal. This indicates that the residuals average to around zero. You can see that for this lab, the plot shows some non-linearity because there are more datapoints below the regression line than here are above it. Thus, there are some negative residuals that don’t have positive residuals to cancel them out. However, a bit of deviation is okay – just like with skewness and kurtosis, there’s a range that we can work in before non-normality or non-linearity becomes a critical issue.

For some examples of good Residuals vs Fitted plot and ones that show serious errors, check out this page. I’ve included the images in our video and talk about them more in-depth there. But to summarize quickly, you can see the first case has a plot in which the red line sticks pretty closely to the zero line, while the other cases show some serious deviation. Ours is much closer to the ‘good’ plot than it is to the ‘serious issues’ plots. So we’ll consider our data okay and proceed with our analysis. Obviously, this is quite subjective. I’ll talk a bit about why this is in the video, but the key takeaway is that these evaluations are closely tied to the context of our sample, our data, and what we’re studying. It’s almost always a judgement call.

You’ll notice in the bottom right corner, there are some points with numbers included: these are cases or participants (indicated by row number) who have the most influence on the regression line (and so they might outliers).We’ll talk more about outliers in the next section.

To summarize: our plot suggests there is some minor non-linearity, but overall it is a negative linear regression.

plot(reg_model, 1)

12.4 Regression: Check for outliers

The plots below both address leverage, or how much each data point is able to influence the regression line. Outliers are points that have undue influence on the regression line, the way that Bill Gates entering the room has an undue influence on the mean income.

The first plot, Cook’s distance, is a visualization of a score called (you guessed it) Cook’s distance, calculated for each case (aka row or participant) in the dataframe. Cook’s distance tells us how much the regression would change if the point was removed. Ideally, we want all points to have the same influence on the regression line, although we accept that there will be some variability. The cutoff for a high Cook’s distance score is .5 (not .05, which is our cutoff for statistical significance). For our data, some points do exert more influence than others but they’re generally equal, and none of them are close to the cutoff.

The second plot also includes the residuals in the examination of leverage. The standardized residuals are on the y-axis and leverage is on the x-axis; this shows us which points have high residuals (are far from the regression line) and high leverage. Point that have large residuals and high leverage are especially worrisome, because they are far from the regression line but are also exerting a large influence on it. The red line indicates the average residual across points with the same amount of leverage. As usual, we want this line to stay as close to the mean line (or the zero line) as possible.

Because the leverage in our plot is low, part of it actually cut off! If you check the first set of plots on this page (note that Residuals vs Leverage is the fourth in the grid) you can see there are curved red lines in the corners of the Residuals vs Leverage plots. This is the .5 cutoff for Cook’s distance, and so any points appearing past these lines is a serious outlier that needs to be removed. On this page you can also see Residuals vs Leverage plots with severe deviations from the mean line, which makes our deviations appear much less serious.

Our data doesn’t have any severe outliers. For your homework, you’ll simply need to generate these plots, assess Cook’s distance in your dataset, and then identify any potential cases that are prominent outliers. Since we have some cutoffs, that makes this process is a bit less subjective than some of the other assessments we’ve done here, which is a nice change!

# Cook's distance
plot(reg_model, 4)

# Residuals vs Leverage
plot(reg_model, 5)

12.5 Regression: Issues with My Data

Before interpreting our results, we assessed our variables to see if they met the assumptions for a simple linear regression. Analysis of a Residuals vs Fitted plot suggested that there is some minor non-linearity, but not enough to violate the assumption of linearity. We also checked Cook’s distance and a Residuals vs Leverage plot to detect outliers. One case had a large residuals and above-average leverage but was below the recommended cutoff for Cook’s distance.

13 Regression: View Test Output

summary(reg_model)
## 
## Call:
## lm(formula = pss ~ mfq_26_std, data = d)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3.07350 -0.58214 -0.04783  0.58642  2.42576 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.93697    0.02366  124.16   <2e-16 ***
## mfq_26_std  -0.48882    0.02367  -20.66   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8133 on 1180 degrees of freedom
## Multiple R-squared:  0.2656, Adjusted R-squared:  0.2649 
## F-statistic: 426.7 on 1 and 1180 DF,  p-value: < 2.2e-16
# note for section below: to type lowercase Beta below (ß) you need to hold down Alt key and type 225 on numeric keypad. If that doesn't work you should be able to copy/paste it from somewhere else

14 Regression: Write Up Results

To test our hypothesis that mental flexibility will significantly predict perceived stress, and that the relationship will be negative, and that the relationship will be positive, we used a simple linear regression to model the relationship between the variables. We confirmed that our data met the assumptions of a linear regression, checking the linearity of the relationship using a Residuals vs Fitted plot and checking for outliers using Cook’s distance and a Residuals vs Leverage plot. Note: we are skipping the assumptions of normality and homogeneity of variance for this assignment.

As predicted, we found that mental flexibility significantly predicted perceived stress, Adj. R2 = .26, F(1,1180) = 426.7, p < .001. The relationship between mental flexibility and perceived stress is negative, ß = -0.49, t(1180) = -20.66, p < .001 (refer to Figure 1). According to Cohen (1988), this constitutes a large effect size (> .50).

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.