#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="Data/projectdata.csv", header=T)
School Attitude will significantly predict Self-esteem, and the relationship will be positive. This means that as people report higher levels of School Attitude, they will also report higher levels of Self-esteem My independent variable (the one doing the predicting) is: School Attitude My dependent variable (the one being predicted) is: Self-esteem
# 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': 140 obs. of 7 variables:
## $ X : int 8120 8301 8308 8389 8536 8578 8660 8680 8738 8106 ...
## $ employment : chr "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" ...
## $ sleep_hours: chr "4 8-10 hours" "3 7-8 hours" "3 7-8 hours" "1 < 5 hours" ...
## $ rse : num 2.1 2.9 2.4 1.2 1.2 1.9 2.7 2.9 3.3 3.5 ...
## $ pss : num 3 4 3 5 3.5 2.75 3.5 2.5 2.5 3.75 ...
## $ mfq_26 : num 3.55 4.5 3.4 4.6 3.8 3.6 4.45 4.5 4.75 5.1 ...
## $ school_att : num 3.62 3.2 4 2.6 3.89 ...
# 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
## X 1 140 8356.51 304.78 8370.50 8358.27 398.82 7847.00 8860.00
## employment* 2 140 1.06 0.36 1.00 1.00 0.00 1.00 4.00
## sleep_hours* 3 140 2.66 0.96 3.00 2.68 1.48 1.00 5.00
## rse 4 140 2.35 0.65 2.35 2.33 0.67 1.00 4.00
## pss 5 140 3.29 0.94 3.50 3.29 1.11 1.00 5.00
## mfq_26 6 140 4.19 0.62 4.20 4.18 0.59 2.75 5.75
## school_att 7 140 3.95 1.05 4.00 3.93 1.20 1.40 6.33
## range skew kurtosis se
## X 1013.00 -0.05 -1.35 25.76
## employment* 3.00 6.13 39.25 0.03
## sleep_hours* 4.00 0.07 -0.49 0.08
## rse 3.00 0.24 -0.48 0.05
## pss 4.00 -0.10 -0.82 0.08
## mfq_26 3.00 0.07 -0.41 0.05
## school_att 4.93 0.12 -0.56 0.09
# next, use histograms to examine your continuous variables
hist(d$school_att)
hist(d$rse)
# 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$school_att, d$rse)
# to calculate standardized coefficients for the regression, we have to standardize our IV
d$school_att_std <- scale(d$school_att, 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(rse ~ school_att_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 = school_att_std, y = rse)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
geom_segment(aes(xend = school_att_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 some minor non-linearity between our independent and dependent variables, but we are okay to proceed with the regression.
[Remember to revise the above interpretation in you HW assignment. Then delete this reminder and the bolded text below.]
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.
[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.
[Remember to revise the above interpretation in you HW assignment. Then delete this reminder and the bolded text below.]
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.
[Remember to revise the above paragraph in you HW assignment. Then delete this reminder.]
summary(reg_model)
##
## Call:
## lm(formula = rse ~ school_att_std, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.17088 -0.35144 0.05732 0.34252 1.44293
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.35143 0.04548 51.707 < 2e-16 ***
## school_att_std 0.36200 0.04564 7.932 6.62e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5381 on 138 degrees of freedom
## Multiple R-squared: 0.3131, Adjusted R-squared: 0.3082
## F-statistic: 62.91 on 1 and 138 DF, p-value: 6.622e-13
# 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 School Attitude would significantly predict Self-esteem, 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 School Attitude significantly predicted Self-esteem, F(1, 138) = 62.91 , p < .001 , Adj. R2 = 0.31. Additionally, the relationship between School Attitude and Self-esteem was positive, ß = 0.36, t(138) = 7.93, 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.