#install.packages("broom")
#install.packages("ggplot2")
library(psych) # for the describe() command
library(broom) # for the augment() command
library(ggplot2) # to visualize our results
## Warning: package 'ggplot2' was built under R version 4.4.3
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
d <- read.csv(file="Data/projectdata.csv", header=T)
We hypothesize that people’s reported level of depression will significantly predict their level of anxiety, and that the relationship will be positive. This means that as people report higher levels of depression, they will also report higher levels of anxiety.
My independent variable (the one doing the predicting) is: depression (PHQ-9) My dependent variable (the one being predicted) is: anxiety (GAD-7)
# you only need to check the variables you're using in the current analysis
str(d$phq)
## num [1:256] 2 1.78 1.11 1.33 1.89 ...
str(d$gad)
## num [1:256] 4 1.43 1 1 3.43 ...
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$phq)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 256 2.69 0.85 2.78 2.7 0.99 1 4 3 -0.09 -1.06 0.05
describe(d$gad)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 256 2.65 0.92 2.71 2.67 1.06 1 4 3 -0.18 -1.15 0.06
# next, use histograms to examine your continuous variables
hist(d$phq,
main = "Histogram of Patient Health Questionnaire-9",
xlab = "Patient Health Questionnaire-9")
hist(d$gad,
main = "Histogram of Generalized Anxiety Disorder-7",
xlab = "Generalized Anxiety Disorder-7")
# last, use scatterplots to examine your continuous variables together
# Remember to put INDEPENDENT VARIABLE FIRST, so that it goes on the x-axis
plot(d$phq, d$gad,
main = "Scatterplot of Depression and Anxiety",
xlab = "Patient Health Questionnaire-9",
ylab = "Generalized Anxiety Disorder-7")
# to calculate standardized coefficients for the regression, we have to standardize our IV
d$phq_std <- scale(d$phq, center=T, scale=T)
# use the lm() command to run the regression
# dependent/outcome variable on the left of the ~, standardized independent/predictor variable on the right.
reg_model <- lm(gad ~ phq_std, data = d)
# NO PEEKING AT YOUR MODEL RESULTS YET!
# Create Plots
model.diag.metrics <- augment(reg_model)
# View Raw Residuals Plot
# NOTE: only replace the variables in 3 places in this line of code
ggplot(model.diag.metrics, aes(x = phq_std, y = gad)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
geom_segment(aes(xend = phq_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'
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. You can see that for this lab, the plot shows some non-linearity because there are more data points below the regression line than there 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 with non-normality – there is a range of acceptability that we can work in before 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. Looking at these examples, 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. Our plot for the lab 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 a subjective decision. 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 participants (“cases”, indicated by row number) who have the most influence on the regression line (and so they might be outliers). We’ll cover more about outliers in the next section.
[NOTE: All of the above text is informational. You do NOT need to edit it for the HW.]
plot(reg_model, 1) #Residual vs Fitted plot
Interpretation: Our Residual vs Fitted plot suggests there is a reasonably linear relationship between depression and anxiety, with the red line staying close to zero. This is much closer to the ‘good’ plots than the ‘bad, problematic’ plots, so we are okay to proceed with the regression.
The plot below addresses 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 Cook’s distance plot is a visualization of a score called (you guessed it) Cook’s distance, calculated for each case (aka participant) in the dataframe. Cook’s distance tells us how much the regression would change if that data 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 .50. For our lab data, some points do exert more influence than others, but none of them are close to the cutoff. Remember, the plot will always identify the 3 most extreme values; it is your job to identify if any of those values are beyond the cutoff value.
[NOTE: All of the above text is informational. You do NOT need to edit it for the HW.]
# Cook's distance
plot(reg_model, 4)
Our data does not have any severe outliers. All three of the most extreme cases identified in the plot fall well below the cutoff of 0.50.
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 a reasonably linear relationship between depression and anxiety, and the assumption of linearity was met. We also checked Cook’s distance plot to detect outliers. All cases were below the recommended cutoff for Cook’s distance of 0.5, so no outliers were detected.
[Fill in the bracketed sections above after running the code, then delete this reminder.]
summary(reg_model)
##
## Call:
## lm(formula = gad ~ phq_std, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.72406 -0.36230 0.02459 0.28589 1.92405
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.64955 0.03644 72.71 <2e-16 ***
## phq_std 0.70674 0.03651 19.36 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.583 on 254 degrees of freedom
## Multiple R-squared: 0.596, Adjusted R-squared: 0.5944
## F-statistic: 374.7 on 1 and 254 DF, p-value: < 2.2e-16
# NOTE: For the write-up section below, to type lowercase Beta (ß) you need to hold down Alt key and type 225 on numeric keypad. If that doesn't work (upon releasing the Alt key), you should be able to copy/paste it from somewhere else in the write-up.
Effect size, based on Regression ß (Beta Estimate) value in our output:
To test our hypothesis that depression significantly predicts anxiety, and that the relationship would be positive, we used a simple linear regression to model the relationship between those 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 plot. (Note: We are skipping the assumptions of normality and homogeneity of variance for this analysis.)
As predicted, we found that depression significantly predicted anxiety, Adj. R2 = .594, F(1, 254) = 374.70, p < .001. Additionally, the relationship between depression and anxiety was positive, ß = 0.71, t(254) = 19.36, p < .001 (refer to Figure 1). According to Cohen (1988), this constitutes a large effect size (ß > 0.50)
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.