#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
# 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="projectdata.csv", header=T)
We hypothesize that individuals’ reported level of general self-efficacy will significantly predict their level of life satisfaction, and that the relationship will be positive. This means that as individuals report higher levels of general self-efficacy, they will also report higher levels of life satisfaction.
My independent variable (the one doing the predicting) is: self efficacy My dependent variable (the one being predicted) is: life satisfaction
# 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': 3146 obs. of 7 variables:
## $ ResponseID: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ income : chr "1 low" "1 low" "rather not say" "rather not say" ...
## $ edu : chr "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
## $ idea : num 3.75 3.88 3.75 3.75 3.5 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ efficacy : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
# 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
## ResponseID* 1 3146 1573.50 908.32 1573.50 1573.50 1166.06 1.0 3146.0 3145.0
## income* 2 3146 2.43 1.16 2.00 2.42 1.48 1.0 4.0 3.0
## edu* 3 3146 2.50 1.24 2.00 2.17 0.00 1.0 7.0 6.0
## idea 4 3146 3.57 0.38 3.62 3.62 0.37 1.0 4.0 3.0
## swb 5 3146 4.47 1.32 4.67 4.53 1.48 1.0 7.0 6.0
## efficacy 6 3146 3.13 0.45 3.10 3.13 0.44 1.1 4.0 2.9
## stress 7 3146 3.05 0.60 3.00 3.05 0.59 1.3 4.7 3.4
## skew kurtosis se
## ResponseID* 0.00 -1.20 16.19
## income* 0.15 -1.44 0.02
## edu* 2.21 3.80 0.02
## idea -1.52 4.32 0.01
## swb -0.36 -0.45 0.02
## efficacy -0.24 0.45 0.01
## stress 0.03 -0.16 0.01
# next, use histograms to examine your continuous variables
hist(d$efficacy)
hist(d$swb)
# 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$efficacy, d$swb)
# to calculate standardized coefficients for the regression, we have to standardize our IV
d$efficacy_std <- scale(d$efficacy, 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(swb ~ efficacy_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 = efficacy_std, y = swb)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
geom_segment(aes(xend = efficacy_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 per session.
## 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.
plot(reg_model, 1) #Residual vs Fitted plot
Interpretation: Our Residuals vs Fitted plot suggests there is some minor non-linearity in the relationship between general self-efficacy (independent variable) and life satisfaction (dependent variable), but the pattern is not severe, so we can proceed with the regression analysis.
This Residuals vs Fitted plot appears closer to a “good” plot than a problematic one. The residuals are generally scattered around zero without a strong or clear pattern which supports the assumption of linearity. Although there is a slight curve in the red line suggesting minor non-linearity and some uneven spread at higher values. The plot does not show major violations of regression assumptions, so it is reasonable to proceed with the analysis.
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 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.
# Cook's distance
plot(reg_model, 4)
Interpretation: There do not appear to be any severe outliers. A few points show relatively higher Cook’s distance values compared to others but they are still quite small overall and do not stand out as influential points.
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 whether our variables self-efficacy and life satisfaction met the assumptions for a simple linear regression. The Residuals vs Fitted plot suggested there is some minor non-linearity in the relationship, but not enough to violate the assumption of linearity. We also examined the Cook’s distance plot to identify any influential outliers. All cases fell well below the recommended cutoff of 0.5, indicating that no significant outliers were present in the data.
summary(reg_model)
##
## Call:
## lm(formula = swb ~ efficacy_std, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.3369 -0.8253 0.1259 0.9189 3.3816
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.47452 0.02161 207.04 <2e-16 ***
## efficacy_std 0.52597 0.02162 24.33 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.212 on 3144 degrees of freedom
## Multiple R-squared: 0.1585, Adjusted R-squared: 0.1582
## F-statistic: 592.1 on 1 and 3144 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.
To test our hypothesis that general self-efficacy would significantly predict life satisfaction, and that the relationship would be positive, we used a simple linear regression to model the relationship between these 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 a Cook’s distance plot.
As predicted, we found that general self-efficacy significantly predicted life satisfaction, F(1, 3144) = 592.10, p < .001, Adj. R2 = 0.16. Additionally, the relationship between general self-efficacy and life satisfaction was positive, ß = .53, t(3144) = 24.33, 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.