1 Loading Libraries

#install.packages("sjPlot")

library(psych) # for the describe() command
library(car) # for the vif() command
## Warning: package 'car' was built under R version 4.5.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.5.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.5.3

2 Importing Data

# For HW, import the dataset you cleaned previously, this will be the dataset you'll use throughout the rest of the semester

d <- read.csv(file="Data/projectdata.csv", header=T)

3 State Your Hypothesis

We hypothesize that resilience and social support will significantly predict pandemic anxiety. The relationship between resilience and pandemic anxiety will be negative, and the relationship between social support and pandemic anxiety will be negative.

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':    290 obs. of  7 variables:
##  $ X        : int  7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
##  $ education: chr  "2 equivalent to high school completion" "1 equivalent to not completing high school" "2 equivalent to high school completion" "2 equivalent to high school completion" ...
##  $ mhealth  : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ pswq     : num  3.43 2.14 1.57 1.43 1.5 ...
##  $ pas_covid: num  4.44 3.11 3.22 1.78 2.78 ...
##  $ brs      : num  2 3.83 3.83 4 4.67 ...
##  $ support  : num  4.83 5 4.83 3.33 4.83 ...
# Place only continuous variables of interest in new dataframe, and name it "cont"
cont <- na.omit(subset(d, select=c(pas_covid, brs, support)))
cont$row_id <- 1:nrow(cont)

# Standardize all IVs
cont$brs <- scale(cont$brs, center=T, scale=T)
cont$support <- scale(cont$support, 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
## pas_covid    1 290   3.35  0.64   3.44    3.37   0.66  1.00   5.00   4.00 -0.40
## brs          2 290   0.00  1.00   0.01    0.00   1.13 -1.90   2.69   4.59  0.10
## support      3 290   0.00  1.00   0.00    0.01   1.14 -2.29   1.79   4.08 -0.07
## row_id       4 290 145.50 83.86 145.50  145.50 107.49  1.00 290.00 289.00  0.00
##           kurtosis   se
## pas_covid     0.71 0.04
## brs          -0.64 0.06
## support      -0.81 0.06
## row_id       -1.21 4.92
# also use histograms to examine your continuous variables (all IVs and DV)
hist(cont$pas_covid)

hist(cont$brs)

hist(cont$support)

# last, use scatterplots to examine each pairing of your continuous variables together
plot(cont$brs, cont$pas_covid)  # PUT YOUR DV 2ND (Y-AXIS)

plot(cont$support, cont$pas_covid)  # PUT YOUR DV 2ND (Y-AXIS)

plot(cont$brs, cont$support)  # Check relationship between IVs, order does not matter

5 View Your Correlations

corr_output_m <- corr.test(cont)
corr_output_m
## Call:corr.test(x = cont)
## Correlation matrix 
##           pas_covid   brs support row_id
## pas_covid      1.00 -0.30   -0.15   0.21
## brs           -0.30  1.00    0.37  -0.46
## support       -0.15  0.37    1.00  -0.55
## row_id         0.21 -0.46   -0.55   1.00
## Sample Size 
## [1] 290
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##           pas_covid brs support row_id
## pas_covid      0.00   0    0.01      0
## brs            0.00   0    0.00      0
## support        0.01   0    0.00      0
## row_id         0.00   0    0.00      0
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option
# CHECK FOR ANY CORRELATIONS AMONG YOUR IVs ABOVE .70 --> BAD (aka multicollinearity)

6 Run a Multiple Linear Regression

# ONLY use the commented out section below IF if you need to remove outliers AFTER examining the Cook's distance and a Residuals vs Leverage plots in your HW -- remember we practiced this in the ANOVA lab

cont <- subset(cont, row_id!=c(251))


# use the lm() command to run the regression. Put DV on the left,  IVs on the right separated by "+"
reg_model <- lm(pas_covid ~ brs + support, 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
  • Relationship between the variables should be linear
  • Homogeneity of variance [NOTE: We are skipping this here]
  • Residuals should be normal and have constant variance

New assumptions:

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

7.2 Count Number of Cases

needed <- 80 + 8*2
nrow(cont) >= needed
## [1] TRUE

NOTE: For your homework, if you don’t have the required number of cases reach out to me and we can figure out the best way to proceed!

7.3 Check for multicollinearity

  • Higher values indicate more multicollinearity
  • Cutoff is usually VIF > 5
# Variance Inflation Factor = VIF
vif(reg_model)
##      brs  support 
## 1.170941 1.170941

NOTE: For your homework, you will need to discuss multicollinearity and any high values in “Issues with My Data”, but you don’t have to drop any variables.

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.

plot(reg_model, 1)

NOTE: For your homework, you’ll simply need to generate this plot and talk about whether the assumption was met in “Issues with My Data”. This is going to be a judgement call, and that’s okay!

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

# Cook's distance
plot(reg_model, 4)

# Residuals vs Leverage
plot(reg_model, 5)

NOTE: For your homework, you’ll simply need to generate these plots, assess Cook’s distance in your dataset, and then identify and remove any potential cases that are prominent outliers (like we did in the ANOVA lab). You will make a note of this in the “Issues with My Data” write-up.

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

plot(reg_model, 2)

NOTE: For your homework, you’ll simply need to generate this plot and think about how your plot compares to the normal/non-normal plots pictured in the links above. Does it seem like the points lie mostly along the straight diagonal line with either no or some minor deviations along each of the tails? If so, your residuals are likely normal enough to meet the assumption. You will talk about this in the write-up below.

7.7 Issues with My Data

Before interpreting our results, we assessed our variables to see if they met the assumptions for a multiple linear regression. We detected slight issues with linearity in a Residuals vs Fitted plot, along with a single outlier (by visually analyzing Cook’s Distance and Residuals vs Leverage plots). We removed the outlier. We did not have any serious issues with the normality of our residuals (by visually analyzing a Q-Q plot), nor were there any issues of multicollinearity among our two independent variables.

8 View Test Output

summary(reg_model)
## 
## Call:
## lm(formula = pas_covid ~ brs + support, data = cont)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.09929 -0.37914  0.04469  0.43373  1.53387 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.34692    0.03585  93.368  < 2e-16 ***
## brs         -0.19799    0.03928  -5.040 8.27e-07 ***
## support     -0.01658    0.03885  -0.427     0.67    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6093 on 286 degrees of freedom
## Multiple R-squared:  0.1003, Adjusted R-squared:  0.09401 
## F-statistic: 15.94 on 2 and 286 DF,  p-value: 2.729e-07
# 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

Effect size, based on Regression ß (Beta Estimate) value in our output

  • Trivial: Less than 0.10 (ß < 0.10)
  • Small: 0.10–0.29 (0.10 < ß < 0.29)
  • Medium: 0.30–0.49 (0.30 < ß < 0.49)
  • Large: 0.50 or greater (ß > 0.50)

9 Write Up Results

To test our hypothesis that resilience and social support would significantly predict pandemic anxiety, we used a multiple linear regression to model the associations between these variables. While confirming that our data met the assumptions of a linear regression, we discovered that there were slight issues with linearity, alongside a single outlier. The outlier was removed. Our hypothesis was partially supported. The model was statistically significant, F(2, 286) = 15.94, p < .001, Adj. R2 = 9.40%. Our results indicate that resilience negatively predicted pandemic anxiety and had a small effect size (0.10 < ß < 0.29; per Cohen, 1988), while social support did not have any significant predictive power (p = .67). This means that people’s pandemic anxiety decreases by .18 units for every one unit increase in their resilience.

Table 1: Multiple Regression Model Predicting Pandemic Anxiety
  Pandemic Anxiety
Predictors Estimates SE CI p
Intercept 3.35 0.04 3.28 – 3.42 <0.001
Resilience -0.20 0.04 -0.28 – -0.12 <0.001
Social Support -0.02 0.04 -0.09 – 0.06 0.670
Observations 289
R2 / R2 adjusted 0.100 / 0.094

 

References

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