#install.packages("broom")
#install.packages("ggplot2")
library(psych) # for the describe() command
## Warning: package 'psych' was built under R version 4.4.3
library(broom) # for the augment() command
## Warning: package 'broom' was built under R version 4.4.3
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
# 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)
We hypothesize that people’s reported levels of General Anxiety Disorder will significantly predict their level of pandemic anxiety, and that the relationship will be positive. This means that as people report higher levels of anxiety, they will report higher levels of pandemic anxiety.
My independent variable is: levels of anxiety My dependent variable is: pandemic anxiety
# 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 to be sure that everything is correct
str(d)
## 'data.frame': 979 obs. of 7 variables:
## $ X : int 321 401 520 1390 1422 1849 2247 2526 2609 2689 ...
## $ age : chr "1 under 18" "4 between 36 and 45" "1 under 18" "5 over 45" ...
## $ education: chr "1 equivalent to not completing high school" "6 graduate degree or higher" "1 equivalent to not completing high school" "5 undergraduate degree" ...
## $ phq : num 1.89 2.44 1.56 1.22 4 ...
## $ gad : num 1 2.14 1.14 1 1.57 ...
## $ covid_neg: int 0 0 0 0 0 0 0 0 0 0 ...
## $ pas_covid: num 2.33 4 3 2.89 2.67 ...
# 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
## X 1 979 4698.20 2572.33 4838.00 4749.88 3309.16 20 8860 8840
## age* 2 979 2.12 1.66 1.00 1.91 0.00 1 5 4
## education* 3 979 3.04 1.98 2.00 2.84 1.48 1 7 6
## phq 4 979 2.05 0.86 1.89 1.96 0.99 1 4 3
## gad 5 979 1.99 0.90 1.71 1.90 0.85 1 4 3
## covid_neg 6 979 1.04 1.76 0.00 0.68 0.00 0 8 8
## pas_covid 7 979 3.23 0.69 3.22 3.25 0.66 1 5 4
## skew kurtosis se
## X -0.13 -1.21 82.21
## age* 0.94 -0.97 0.05
## education* 0.60 -1.05 0.06
## phq 0.68 -0.58 0.03
## gad 0.75 -0.62 0.03
## covid_neg 1.50 1.11 0.06
## pas_covid -0.20 0.11 0.02
# next, use histograms to examine your continuous variables
hist(d$gad)
hist(d$pas_covid)
# 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$gad, d$pas_covid)
# to calculate standardized coefficients for the regression, we have to standardize our IV
d$gad_std <- scale(d$gad, 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(pas_covid ~ gad_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 = gad_std, y = pas_covid)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
geom_segment(aes(xend = gad_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 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)
Interpretation: Our Residual vs Fitted plot suggests there is some non-lineraity between our independent and dependent variables, but we are okay to proceed with the regression.
Our Residual vs Fitted plot shows a few plots being numbered, suggesting that these should be paid attention to. The red line is a smoothed curve that passes through the actual residuals and it is relatively flat. This is good!
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)
Interpretation: Our data does not have any severe outliers.
For your HW: You need to generate the plot, assess Cook’s distance in your dataset and identify any potential cases/participants that are prominent outliers using the cutoff for a high Cook’s distance score of .50. You will summarize this in the “Issues with my Data” section below.
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 plot to detect outliers. All cases were below the recommended cutoff for Cook’s distance of 0.5, so no outliers were detected.
summary(reg_model)
##
## Call:
## lm(formula = pas_covid ~ gad_std, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.54991 -0.38601 0.01912 0.41982 1.79469
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.23073 0.02036 158.66 <2e-16 ***
## gad_std 0.26192 0.02037 12.86 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6371 on 977 degrees of freedom
## Multiple R-squared: 0.1447, Adjusted R-squared: 0.1438
## F-statistic: 165.3 on 1 and 977 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 Trivial: Less than 0.10 Small: 0.10–0.29 Medium: 0.30–0.49 Large: 0.50 or greater
To test our hypothesis that levels of General Anxiety Disorder will significantly predict pandemic anxiety, and that the relationship will 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 assignment.
As predicted, we found that anxiety significantly predicted pandemic anxiety, Adj. R2 = .144, F(1, 977) = 165.30, p < .001 . The relationship between anxiety and pandemic anxiety was positive, ß = .26, t(977) = 12.86, p < .001 (refer to Figure 1). According to Cohen (1988), this constitutes a large effect size (ß >.30).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.