1 Loading Libraries

#install.packages("broom")
#install.packages("ggplot2")

library(psych) # for the describe() command
library(broom) # for the augment() command
library(ggplot2) # to visualize our results
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha

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 negative effects of Covid will significantly predict anxiety, and the relationship will be positive. This means that as people report higher levels of negative effects of Covid, they will also report higher levels of anxiety.

My independent variable (the one doing the predicting) is: negative effects of Covid My dependent variable (the one being predicted) is: anxiety

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 to be sure that everything is correct

str(d)
## 'data.frame':    1280 obs. of  7 variables:
##  $ X          : int  1 321 401 469 520 1390 1422 1849 2183 2247 ...
##  $ sleep_hours: chr  "3 7-8 hours" "4 8-10 hours" "2 5-6 hours" "3 7-8 hours" ...
##  $ mhealth    : chr  "none or NA" "none or NA" "obsessive compulsive disorder" "depression" ...
##  $ covid_neg  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ gad        : num  1.86 1 2.14 1.71 1.14 ...
##  $ pss        : num  3.25 2.25 2.25 2.25 2.75 2.75 4.75 3.25 3.5 2.25 ...
##  $ support    : num  2.5 2.5 3.83 4.67 2.83 ...
# 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 1280 4701.89 2602.47 4817.00 4756.43 3392.19   1 8867  8866
## sleep_hours*    2 1280    2.90    0.97    3.00    2.91    1.48   1    5     4
## mhealth*        3 1280    4.65    1.41    5.00    4.88    0.00   1    8     7
## covid_neg       4 1280    1.08    1.77    0.00    0.72    0.00   0    8     8
## gad             5 1280    2.04    0.91    1.71    1.95    0.85   1    4     3
## pss             6 1280    2.93    0.95    3.00    2.92    1.11   1    5     4
## support         7 1280    3.57    0.95    3.67    3.62    0.99   1    5     4
##               skew kurtosis    se
## X            -0.13    -1.24 72.74
## sleep_hours*  0.02    -0.45  0.03
## mhealth*     -1.37     2.52  0.04
## covid_neg     1.44     0.91  0.05
## gad           0.68    -0.72  0.03
## pss           0.09    -0.75  0.03
## support      -0.43    -0.54  0.03
# next, use histograms to examine your continuous variables
hist(d$covid_neg)

hist(d$gad)

# 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$covid_neg, d$gad)

5 Run a Simple Regression

# to calculate standardized coefficients for the regression, we have to standardize our IV
d$covid_neg_std <- scale(d$covid_neg, 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 ~ covid_neg_std, data = d)

# NO PEEKING AT YOUR MODEL RESULTS YET!

6 Check Your Assumptions

6.1 Simple Regression Assumptions

  • Should have two measurements for each participant
  • Variables should be continuous and normally distributed
  • Relationship between the variables should be linear
  • Outliers should be identified and removed
  • Residuals should be approx. normal and have constant variance NOTE: We will NOT be evaluating whether our data meets this last assumption in this lab/homework.

6.2 Create plots and view residuals

# 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 = covid_neg_std, y = gad)) +
  geom_point() +
  stat_smooth(method = lm, se = FALSE) +
  geom_segment(aes(xend = covid_neg_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'

6.3 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. 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 some very minor non-linearity between our independent and dependent variables, but we are okay to proceed with the regression. This plot is similar to the ‘good’ regression plot since the horizontal line is extremely close to 0 and only strays slightly above and slightly below in two areas. My regression plot is not similar to the ‘problematic’ regression plot because it doe snot display a parabola or a U or V shape line.

6.4 Check for outliers

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.

6.5 Issues with My Data

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

7 View Statistical Test Output

summary(reg_model)
## 
## Call:
## lm(formula = gad ~ covid_neg_std, data = d)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1813 -0.6940 -0.2655  0.5917  2.1631 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     2.0440     0.0236   86.63   <2e-16 ***
## covid_neg_std   0.3407     0.0236   14.43   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8442 on 1278 degrees of freedom
## Multiple R-squared:  0.1402, Adjusted R-squared:  0.1395 
## F-statistic: 208.3 on 1 and 1278 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.

7.1 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)

8 Write Up Results

To test our hypothesis that negative effects of Covid would significantly predict 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 negative effects of Covid significantly predicted anxiety, F(1, 1278) = 208.3 , p <.001 , Adj. R2 = 0.14 . Additionally, the relationship between negative effects of Covid and anxiety was positive, ß = .34, t(1278) = 14.43 , p <.001 (refer to Figure 1). According to Cohen (1988), this constitutes a medium effect size (0.30 < ß < 0.49).

References

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