library(psych) # for the describe() command
library(car) # for the vif() command
## Warning: 程辑包'car'是用R版本4.3.3 来建造的
## 载入需要的程辑包:carData
## Warning: 程辑包'carData'是用R版本4.3.3 来建造的
##
## 载入程辑包:'car'
## The following object is masked from 'package:psych':
##
## logit
library(sjPlot) # to visualize our results
## Warning: 程辑包'sjPlot'是用R版本4.3.3 来建造的
# 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/arc_data_final-1_final.csv", header=T)
We hypothesize that pandemic anxiety (measured by the CPAS-11), social support (measured by the SSS) and self-esteem (measured by the RSE-10) will significantly predict general anxiety (measured by the GAD-7).
# 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': 960 obs. of 6 variables:
## $ gender : chr "male" "female" "female" "female" ...
## $ age : chr "1 under 18" "1 under 18" "4 between 36 and 45" "4 between 36 and 45" ...
## $ gad : num 3.86 1.14 2 1.43 1.57 ...
## $ pas_covid: num 4.56 3.33 4.22 3.22 3.56 ...
## $ support : num 2.17 5 2.5 3.67 3.83 ...
## $ rse : num 1.6 3.9 1.7 3.9 2.4 1.8 3.5 3 3.5 2.5 ...
cont <- na.omit(subset(d, select=c(gad,pas_covid,support,rse)))
cont$pas_covid <- scale(cont$pas_covid, center=T, scale=T)
cont$support <- scale(cont$support, center=T, scale=T)
cont$rse <- scale(cont$rse, 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 kurtosis
## gad 1 960 2 0.9 1.71 1.90 0.85 1.00 4.00 3.00 0.74 -0.61
## pas_covid 2 960 0 1.0 -0.02 0.02 0.97 -3.28 2.58 5.86 -0.21 0.14
## support 3 960 0 1.0 0.08 0.06 1.05 -2.75 1.49 4.24 -0.42 -0.62
## rse 4 960 0 1.0 0.05 0.03 1.04 -2.34 1.89 4.23 -0.23 -0.73
## se
## gad 0.03
## pas_covid 0.03
## support 0.03
## rse 0.03
# also use histograms to examine your continuous variables
hist(cont$gad)
hist(cont$pas_covid)
hist(cont$support)
hist(cont$rse)
# last, use scatterplots to examine your continuous variables together
plot(cont$gad, cont$support)
plot(cont$support, cont$pas_covid)
plot(cont$support, cont$rse)
plot(cont$gad, cont$pas_covid)
plot(cont$gad, cont$rse)
plot(cont$pas_covid, cont$rse)
corr_output_m <- corr.test(cont)
corr_output_m
## Call:corr.test(x = cont)
## Correlation matrix
## gad pas_covid support rse
## gad 1.00 0.38 -0.47 -0.70
## pas_covid 0.38 1.00 -0.16 -0.30
## support -0.47 -0.16 1.00 0.54
## rse -0.70 -0.30 0.54 1.00
## Sample Size
## [1] 960
## Probability values (Entries above the diagonal are adjusted for multiple tests.)
## gad pas_covid support rse
## gad 0 0 0 0
## pas_covid 0 0 0 0
## support 0 0 0 0
## rse 0 0 0 0
##
## To see confidence intervals of the correlations, print with the short=FALSE option
# use the lm() command to run the regression
# dependent/outcome variable on the left, independent/predictor variables on the right
reg_model <- lm( gad ~ pas_covid + support + rse, data = cont)
Assumptions we’ve discussed previously:
New assumptions:
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
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)
## pas_covid support rse
## 1.098804 1.406621 1.503907
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)
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)
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)
# Residuals vs Leverage
plot(reg_model, 5)
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)
Before interpreting our results, we assessed our variables to see if they met the assumptions for a multiple linear regression. We analyzed a Scale-Location plot and detected some issues with homogeneity of variance. However, we did not detect any outliers (visually analyzing a Residuals vs Leverage plot), or any serious issues with the normality of our residuals (visually analyzing a Q-Q plot), or any serious issues with the linearity in a Residuals vs Fitted plot.
summary(reg_model)
##
## Call:
## lm(formula = gad ~ pas_covid + support + rse, data = cont)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.70892 -0.42319 -0.05663 0.36555 2.30810
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.99762 0.01995 100.116 < 2e-16 ***
## pas_covid 0.16380 0.02093 7.827 1.32e-14 ***
## support -0.11590 0.02368 -4.895 1.15e-06 ***
## rse -0.51484 0.02448 -21.030 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6182 on 956 degrees of freedom
## Multiple R-squared: 0.5281, Adjusted R-squared: 0.5266
## F-statistic: 356.6 on 3 and 956 DF, p-value: < 2.2e-16
# you do not have to run the below code for the HW
# reg_model_test <- lm(swb ~ belong, data = cont)
# summary(reg_model_test)
plot_model(reg_model, type="std")
plot_model(reg_model, type="emm", terms = c("pas_covid"))
plot_model(reg_model, type="emm", terms = c("support"))
plot_model(reg_model, type="emm", terms = c("rse"))
# 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
To test our hypothesis that pandemic anxiety (measured by the CPAS-11), social support (measured by the SSS), and self-esteem (measured by the RSE-10) would significantly predict general anxiety (measured by the GAD-7), we used a multiple linear regression to model the relationship between the variables. We confirmed that our data met the assumptions of a linear regression, and although there were some issues with the homogeneity of variance we continued with the analysis anyway.
Our model was statistically significant, Adj. R2 = .53, F(3,956) = 356.6, p < .001. The relationship between self-esteem (measured by the RSE-10) and general anxiety (measured by the GAD-7) was negative and has a large effect size (per Cohen, 1988). The relationships between our remaining predictors, pandemic anxiety (measured by the CPAS-11), and our outcome, general anxiety (measured by the GAD-7), were positive and had effect sizes that were small(per Cohen, 1988). The relationships between our remaining predictors, social support (measured by the SSS), and our outcome, general anxiety (measured by the GAD-7), were negative and had effect sizes that were small(per Cohen, 1988). Full output from the regression model is reported in Table 1.
| gad | ||||
|---|---|---|---|---|
| Predictors | Estimates | SE | CI | p |
| Intercept | 2.00 | 0.02 | 1.96 – 2.04 | <0.001 |
| Pandemic Anxiety (CPAS-11) | 0.16 | 0.02 | 0.12 – 0.20 | <0.001 |
| Social Support (SSS) | -0.12 | 0.02 | -0.16 – -0.07 | <0.001 |
| Self-Esteem (RSE-10) | -0.51 | 0.02 | -0.56 – -0.47 | <0.001 |
| Observations | 960 | |||
| R2 / R2 adjusted | 0.528 / 0.527 | |||
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.