#install.packages("broom")
#install.packages("ggplot2")
library(psych) # for the describe() command
library(broom) # for the augment() command
## Warning: package 'broom' was built under R version 4.4.2
library(ggplot2) # to visualize our results
## Warning: package 'ggplot2' was built under R version 4.4.2
##
## 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 higher extroversion will significantly predict how positively COVID-19 is viewed, and that the relationship will be positive.
My independent variable is: Extroversion My dependent variable is: COVID Positive
# 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': 1337 obs. of 7 variables:
## $ X : int 20 30 31 48 49 57 58 69 79 81 ...
## $ age : chr "1 under 18" "1 under 18" "4 between 36 and 45" "4 between 36 and 45" ...
## $ mhealth : chr "anxiety disorder" "none or NA" "none or NA" "depression" ...
## $ covid_pos: int 0 0 0 0 0 0 0 0 0 0 ...
## $ covid_neg: int 0 0 0 0 0 0 0 0 0 0 ...
## $ big5_open: num 5.33 5 6 4.33 6.67 ...
## $ big5_ext : num 1.67 6 5 4.33 5.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 1337 4865.73 2578.34 5195.00 4957.45 3236.52 20 8860 8840
## age* 2 1337 2.10 1.64 1.00 1.87 0.00 1 5 4
## mhealth* 3 1337 4.58 1.44 5.00 4.82 0.00 1 8 7
## covid_pos 4 1337 2.05 3.38 0.00 1.32 0.00 0 15 15
## covid_neg 5 1337 1.21 1.87 0.00 0.85 0.00 0 8 8
## big5_open 6 1337 5.21 1.13 5.33 5.29 0.99 1 7 6
## big5_ext 7 1337 4.37 1.45 4.33 4.41 1.48 1 7 6
## skew kurtosis se
## X -0.24 -1.19 70.51
## age* 0.97 -0.89 0.04
## mhealth* -1.40 2.13 0.04
## covid_pos 1.58 1.47 0.09
## covid_neg 1.29 0.44 0.05
## big5_open -0.75 0.54 0.03
## big5_ext -0.24 -0.78 0.04
# next, use histograms to examine your continuous variables
hist(d$big5_ext)
hist(d$covid_pos)
# last, use scatterplots to examine your continuous variables together
# Remember to put INDEPENDENT FIRST, so it goes on the x-axis
plot(d$big5_ext,d$covid_pos)
# to calculate standardized coefficients for the regression, we have to standardize our IV
d$big5_ext_std <- scale(d$big5_ext, center=T, scale=T)
# use the lm() command to run the regression
# dependent/outcome variable on the left of the ~, independent/predictor variable on the right.
reg_model <- lm(covid_pos ~ big5_ext_std, data = d)
# NO PEEKING AT YOUR MODEL RESULTS YET!
# do not edit this line of code
model.diag.metrics <- augment(reg_model)
# only replace the variables in 3 places in this line of code
ggplot(model.diag.metrics, aes(x = d$big5_ext_std, y = d$covid_pos)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
geom_segment(aes(xend = d$big5_ext_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 looks like one of the good plots given I have an even scatter with no parabolas or curves and my line is mostly horizontal at zero.
For your HW: You need to generate this plot and then talk about how your plot compares to the ‘good’ / ‘bad, problematic’ plots linked to above in the “Issues with my Data” section below. Is it closer to the ‘good’ plots or one of the ‘bad’ plots? This is going to be a judgement call, so just do your best!
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 the 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 .5. For our data, some points do exert more influence than others, but none of them are close to the cutoff.
[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 .5. You will summarize this in the “Issues with my Data” section below.
Before interpreting our results, we assessed whether our data met the assumptions for simple linear regression. The Residuals vs. Fitted plot indicated minor non-linearity, but not enough to violate the assumption of linearity. Additionally, we examined Cook’s distance to identify potential outliers. All cases fell well below the recommended cutoff of 0.5 for Cook’s distance, suggesting that there were no influential outliers in our dataset.
summary(reg_model)
##
## Call:
## lm(formula = covid_pos ~ big5_ext_std, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.030 -2.063 -1.676 1.357 13.421
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.05161 0.09176 22.360 < 2e-16 ***
## big5_ext_std -0.42059 0.09179 -4.582 5.03e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.355 on 1335 degrees of freedom
## Multiple R-squared: 0.01548, Adjusted R-squared: 0.01475
## F-statistic: 21 on 1 and 1335 DF, p-value: 5.033e-06
# note for 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.
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 extroversion will significantly predict how positively COVID is viewed, 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.
We found that extroversion does not predict how positively COVID is viewed, Adj. R² = .015, F(1, 1335) = 21.0, p < .001. The relationship between extroversion and positive COVID attitude was negative, ß = -0.42, t(1335) = -4.58, p < .001. According to Cohen (1988), this constitutes a small effect size (< .10).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.