#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)
We hypothesize that children’s reported levels of worry, as measured by the Penn State Worry Questionnaire (Child), will significantly predict their levels of anxiety, as measured by the Patient Health Questionnaire (PHQ). We expect the relationship to be positive, meaning that as children report higher levels of worry, they will also report higher levels of anxiety.
My independent variable (the one doing the predicting) is: levels of worry My dependent variable (the one being predicted) is: 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': 1165 obs. of 7 variables:
## $ X : int 1 321 401 469 520 1390 1849 2183 2247 2482 ...
## $ gender: chr "female" "male" "female" "female" ...
## $ health: chr "something else or not applicable" "something else or not applicable" "high blood pressure" "diabetes" ...
## $ pswq : num 4.94 1.71 2.44 2.5 2.71 ...
## $ pswq.1: num 4.94 1.71 2.44 2.5 2.71 ...
## $ phq : num 1.33 1.89 2.44 1.22 1.56 ...
## $ gad : num 1.86 1 2.14 1.71 1.14 ...
# 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 1165 4665.88 2575.36 4757.00 4714.85 3328.44 1 8867.00 8866.00
## gender* 2 1165 1.36 0.78 1.00 1.18 0.00 1 4.00 3.00
## health* 3 1165 8.61 1.37 9.00 8.92 0.00 1 11.00 10.00
## pswq 4 1165 2.75 0.80 2.79 2.75 0.95 1 4.94 3.94
## pswq.1 5 1165 2.75 0.80 2.79 2.75 0.95 1 4.94 3.94
## phq 6 1165 2.07 0.86 1.89 1.99 0.99 1 4.00 3.00
## gad 7 1165 2.04 0.91 1.71 1.94 0.85 1 4.00 3.00
## skew kurtosis se
## X -0.13 -1.22 75.45
## gender* 1.86 1.84 0.02
## health* -2.27 5.73 0.04
## pswq 0.00 -0.80 0.02
## pswq.1 0.00 -0.80 0.02
## phq 0.65 -0.65 0.03
## gad 0.69 -0.70 0.03
# next, use histograms to examine your continuous variables
hist(d$pswq.1)
hist(d$phq)
# 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$pswq.1, d$phq)
# to calculate standardized coefficients for the regression, we have to standardize our IV
d$pswq.1_std <- scale(d$pswq.1, 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(phq ~ pswq.1_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 = pswq.1_std, y = phq)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
geom_segment(aes(xend = pswq.1_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 .minor non-linearity between our independent and dependent variables, but we are okay to proceed with the regression
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.
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 the Residuals vs Fitted plot suggested a curved pattern in the residuals, indicating potential non-linearity in the relationship between the predictor and outcome variables. Additionally, the residuals showed a fan-shaped spread, suggesting heteroscedasticity, or unequal variance across the range of fitted values. While these issues may not completely invalidate the model, they do indicate that the assumptions of linearity and homoscedasticity are not perfectly met. This suggests we should interpret our results with some caution and consider that a different model or transformation may better fit the data.We also checked Cook’s Distance to detect potential outliers. All cases were well below the recommended cutoff of 0.5, so no influential outliers were detected.
summary(reg_model)
##
## Call:
## lm(formula = phq ~ pswq.1_std, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.8353 -0.5432 -0.1302 0.5351 2.1624
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.06829 0.02243 92.20 <2e-16 ***
## pswq.1_std 0.39424 0.02244 17.57 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7657 on 1163 degrees of freedom
## Multiple R-squared: 0.2097, Adjusted R-squared: 0.209
## F-statistic: 308.6 on 1 and 1163 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 Penn State Worry Questionnaire (Child) scores will significantly predict Patient Health Questionnaire (PHQ) scores, and that the relationship will be positive, we used a simple linear regression to model the relationship between these variables. We confirmed that our data met the assumptions of linear regression by examining a Residuals vs Fitted plot, which showed some minor non-linearity but nothing severe enough to violate the assumption of linearity. We also checked for influential outliers using a Cook’s distance plot. All values were well below the recommended cutoff of 0.5, indicating no influential outliers were present. Note: we are skipping the assumptions of normality and homogeneity of variance for this assignment.
As predicted, we found that … significantly …, Adj. R2 = 16 , F(1, 3164) = , p < .001. The relationship between social exclusion and loneliness was positive, ß = .52, t(3164) = 24.17, p < .001 (refer tto Figure 1). According to Cohen (1988), this constitutes a large effect size (ß > .50).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.