1 Loading Libraries

library(psych) # for the describe() command
library(car) # for the vif() command
## Warning: package 'car' was built under R version 4.3.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
library(sjPlot) # to visualize our results
## Warning: package 'sjPlot' was built under R version 4.3.3
## Learn more about sjPlot with 'browseVignettes("sjPlot")'.

2 Importing Data

# import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
# use EAMMi2 data
d <- read.csv(file="Data/Covid_Data_final.csv", header=T)

# DO NOT RUN FOR THE HOMEWORK
# set.seed(110)
# d$fake <- (rnorm(n=nrow(d))-d$efficacy)*d$swb
# d$fake <- d$fake+(d$fake*rnorm(n=nrow(d)))

3 State Your Hypothesis

I hypothesize that depression (measured by the PHQ), intolerance of uncertainty (measured by the IOU), and pandemic anxiety (measured by the PAS) will significantly predict generalized anxiety (measured by the GAD).

4 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':    1113 obs. of  6 variables:
##  $ health   : chr  "something else or not applicable" "something else or not applicable" "something else or not applicable" "two conditions" ...
##  $ mhealth  : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ gad      : num  1.86 3.86 1.14 2 1.43 ...
##  $ phq      : num  1.33 3.33 1 2.33 1.11 ...
##  $ iou      : num  3.19 4 1.59 3.37 1.7 ...
##  $ pas_covid: num  3.22 4.56 3.33 4.22 3.22 ...
cont <- na.omit(subset(d, select=c(gad,phq,iou,pas_covid)))
cont$phq <- scale(cont$phq, center=T, scale=T)
cont$iou <- scale(cont$iou, center=T, scale=T)
cont$pas_covid <- scale(cont$pas_covid, center=T, scale=T)

# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(cont)
##           vars    n mean   sd median trimmed  mad   min  max range  skew
## gad          1 1113 2.04 0.92   1.71    1.95 0.85  1.00 4.00  3.00  0.69
## phq          2 1113 0.00 1.00  -0.21   -0.09 1.14 -1.24 2.22  3.46  0.64
## iou          3 1113 0.00 1.00  -0.17   -0.06 1.09 -1.72 2.70  4.42  0.51
## pas_covid    4 1113 0.00 1.00  -0.01    0.02 0.95 -3.23 2.56  5.79 -0.18
##           kurtosis   se
## gad          -0.72 0.03
## phq          -0.68 0.03
## iou          -0.56 0.03
## pas_covid    -0.03 0.03
# also use histograms to examine your continuous variables
hist(cont$phq)

hist(cont$iou)

hist(cont$pas_covid)

hist(cont$gad)

# last, use scatterplots to examine your continuous variables together
plot(cont$phq, cont$gad)

plot(cont$phq, cont$iou)

plot(cont$phq, cont$pas_covid)

plot(cont$iou, cont$gad)

plot(cont$iou, cont$pas_covid)

plot(cont$pas_covid, cont$gad)

5 View Your Correlations

corr_output_m <- corr.test(cont)
corr_output_m
## Call:corr.test(x = cont)
## Correlation matrix 
##            gad  phq  iou pas_covid
## gad       1.00 0.84 0.71      0.40
## phq       0.84 1.00 0.64      0.34
## iou       0.71 0.64 1.00      0.39
## pas_covid 0.40 0.34 0.39      1.00
## Sample Size 
## [1] 1113
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##           gad phq iou pas_covid
## gad         0   0   0         0
## phq         0   0   0         0
## iou         0   0   0         0
## pas_covid   0   0   0         0
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option

6 Run a Multiple Linear Regression

# use the lm() command to run the regression
# dependent/outcome variable on the left, independent/predictor variables on the right
reg_model <- lm(gad ~ phq + iou + pas_covid, data = cont)

7 Check Your Assumptions

7.1 Multiple Linear Regression Assumptions

Assumptions we’ve discussed previously:

  • Observations should be independent
  • Variables should be continuous and normally distributed
  • Outliers should be identified and removed (for our class, we will not be removing outliers, just identifying them)
  • Relationship between the variables should be linear

New assumptions:

  • Number of cases should be adequate (N ≥ 80 + 8m, where m is the number of IVs)
  • Independent variables should not be too correlated (aka multicollinearity)
  • Residuals should be normal and have constant variance

7.2 Count Number of Cases

For your homework, if you don’t have the required number of cases you’ll need to drop one of your independent variables. Reach out to me and we can figure out the best way to proceed!

needed <- 80 + 8*3
nrow(cont) >= needed
## [1] TRUE
# Cutoff is met

7.3 Check multicollinearity

  • Higher values indicate more multicollinearity
  • Cutoff is usually 5

For your homework, you will need to discuss multicollinearity and any high values, but you don’t have to drop any variables.

vif(reg_model)
##       phq       iou pas_covid 
##  1.740339  1.815925  1.203144
# No issues with multicollinearity, none reach anywhere near the cutoff

7.4 Check linearity with Residuals vs Fitted plot

The 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. 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 becomes a critical issue. For some examples of good Residuals vs Fitted plot and ones that show serious errors, check out this page.

For your homework, you’ll simply need to generate this plot and talk about whether your assumptions are met. This is going to be a judgement call, and that’s okay! In practice, you’ll always be making these judgement calls as part of a team, so this assignment is just about getting experience with it, not making the perfect call.

plot(reg_model, 1)

# Very minor skew, but I feel that the assumptions are still met.

7.5 Check normality of residuals with a Q-Q plot

This plot is a bit new. It’s called a Q-Q plot and shows the standardized residuals plotted against a normal distribution. If our variables are perfectly normal, the points will fit on the dashed line perfectly. This page shows how different types of non-normality appear on a Q-Q plot.

It’s normal for Q-Q plots show a bit of deviation at the ends. This page shows some examples that help us put our Q-Q plot into context. For your homework, you’ll simply need to generate this plot and talk about how your plot compares to the ones pictured. Does it seem like any skew or kurtosis is indicated by your plot? Is it closer to the ‘good’/‘bad’ plots from the second link?

plot(reg_model, 2)

# Far closer to the line than the bad examples, but there are three or four outliers
qqnorm(d$gad)
qqline(d$gad)

# Fits the line well with only minor wiggle
qqnorm(d$phq)
qqline(d$phq)

# Minor wiggle
qqnorm(d$iou)
qqline(d$iou)

# No wiggle
qqnorm(d$pas_covid)
qqline(d$pas_covid)

# No wiggle

7.6 Check for outliers using Cook’s distance and a Residuals vs Leverage plot

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. 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. Points 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.

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)

# Seems to be just fine, no major outliers besides maybe 830, but it doesn't go above our cutoff

# Residuals vs Leverage
plot(reg_model, 5)

# A few outliers at the very end, but nowhere near concerning

7.7 LAB ONLY

#library(broom)
#library (ggplot2)
#model.diag.metrics <- augment(reg_model)

#ggplot(model.diag.metrics, aes(x = gad, y = fake)) +
 # geom_point() +
 # stat_smooth(method = lm, se = FALSE) +
 # geom_segment(aes(xend = gad, yend = .fitted), color = "red", size = 0.3)

7.8 Check homogeneity of variance in a Scale-Location plot

This plot shows us the standardized residuals across the range of the regression line. Because the residuals are standarized, large residuals (whether positive or negative) are at the top of the plot, while small residuals (whether positive or negative) are at the bottom of the plot. If the assumption of homogeneity of variance (also called homoscedasticity) is met, the red line should be mostly horizontal. If it deviates from the mean line, that means that the variance is smaller or larger at that point of the regression line.

Once again, you can check out this page for some other examples of this type of plot. For your homework, you’ll simply need to generate this plot and talk about how your plot compares to the ones pictured. Is it closer to the ‘good’ plots or one of the ‘bad’ plots? Again, this is a judgement call! It’s okay if feel uncertain, and you won’t be penalized for that.

plot(reg_model, 3)

# Generally fairly flat with only minor variation at the very end

7.9 Issues with My Data

Before interpreting my results, I assessed my variables to see if they fit the assumptions listed for a multiple linear regression. I did not find any major errors across any of my analyses, including homogeneity of variance, linearity, outliers, and normalcy. While all of my results had some minor variations, none went outside of a reasonable range.

8 View Test Output

summary(reg_model)
## 
## Call:
## lm(formula = gad ~ phq + iou + pas_covid, data = cont)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.73141 -0.25531 -0.02781  0.23030  1.57199 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.04017    0.01348 151.380  < 2e-16 ***
## phq          0.59724    0.01779  33.577  < 2e-16 ***
## iou          0.23249    0.01817  12.796  < 2e-16 ***
## pas_covid    0.07168    0.01479   4.847 1.43e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4496 on 1109 degrees of freedom
## Multiple R-squared:  0.7597, Adjusted R-squared:  0.7591 
## F-statistic:  1169 on 3 and 1109 DF,  p-value: < 2.2e-16
# you do not have to run the below code for the HW
# reg_model_test <- lm(DV ~ IV1, data = d)
# summary(reg_model_test)

plot_model(reg_model, type="std")

plot_model(reg_model, type="emm", terms = c("phq"))

plot_model(reg_model, type="emm", terms = c("iou"))

plot_model(reg_model, type="emm", terms = c("pas_covid"))

# 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

9 Write Up Results

To test my hypothesis that depression (measured by the PHQ), intolerance of uncertainty (measured by the IOU), and pandemic anxiety (measured by the PAS) would significantly predict generalized anxiety, I used a multiple linear regression to model the relationship between the variables. I confirmed that my data met the assumptions provided of a linear regression with little to no issues.

My model was statistically significant, Adj. R^2 = .76, F(3,1109) = 1169, p < .001. The relationship between all of the variables was positive but had different effect sizes. With depression it was large (per Cohen, 1988), with intolerance of uncertainty it was small, and with pandemic anxiety it was trivial. Full output from the regression model is reported in Table 1.

Table 1: Regression model of subjective wellbeing
  General Anxiety Scale (GAD)
Predictors Estimates SE CI p
Intercept 2.04 0.01 2.01 – 2.07 <0.001
Patient Health Questionnaire (PHQ) 0.60 0.02 0.56 – 0.63 <0.001
Intolerance of Uncertainty (IOU) 0.23 0.02 0.20 – 0.27 <0.001
Pandemic Anxiety Scale (PAS) 0.07 0.01 0.04 – 0.10 <0.001
Observations 1113
R2 / R2 adjusted 0.760 / 0.759


 

References

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