#install.packages("sjPlot")
library(psych) # for the describe() command
library(car) # for the vif() command
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
library(sjPlot) # to visualize our results
# 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 Independence and Inventory of Dimensions of Emerging Adulthood will significantly predict Stress. The relationship between Inventory of Dimensions of Emerging Adulthood and Stress will be positive, and the relationship between Independence and Stress will be negative.
# 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 and be sure that everything is correct
str(d)
## 'data.frame': 2092 obs. of 7 variables:
## $ ResponseID : chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ income : chr "1 low" "1 low" "rather not say" "rather not say" ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ efficacy : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ idea : num 3.75 3.88 3.75 3.75 3.5 ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
# Place only continuous variables of interest in new dataframe, and name it "cont"
cont <- na.omit(subset(d, select=c(moa_independence, idea, stress )))
cont$row_id <- 1:nrow(cont)
# Standardize all IVs
cont$moa_independence <- scale(cont$moa_independence, center=T, scale=T)
cont$idea <- scale(cont$idea, center=T, scale=T)
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(cont)
## vars n mean sd median trimmed mad min max
## moa_independence 1 2092 0.00 1.00 0.27 0.15 1.06 -5.44 0.99
## idea 2 2092 0.00 1.00 0.15 0.13 0.97 -6.71 1.13
## stress 3 2092 3.07 0.60 3.10 3.07 0.59 1.30 4.60
## row_id 4 2092 1046.50 604.05 1046.50 1046.50 775.40 1.00 2092.00
## range skew kurtosis se
## moa_independence 6.43 -1.49 2.75 0.02
## idea 7.84 -1.48 4.00 0.02
## stress 3.30 -0.02 -0.15 0.01
## row_id 2091.00 0.00 -1.20 13.21
# also use histograms to examine your continuous variables (all IVs and DV)
hist(cont$moa_independence)
hist(cont$idea)
hist(cont$stress)
# last, use scatterplots to examine each pairing of your continuous variables together
plot(cont$moa_independence, cont$stress) # PUT YOUR DV 2ND (Y-AXIS)
plot(cont$idea, cont$stress) # PUT YOUR DV 2ND (Y-AXIS)
plot(cont$moa_independence, cont$idea) # Check relationship between IVs, order does not matter
corr_output_m <- corr.test(cont)
corr_output_m
## Call:corr.test(x = cont)
## Correlation matrix
## moa_independence idea stress row_id
## moa_independence 1.00 0.13 -0.02 -0.01
## idea 0.13 1.00 0.18 -0.03
## stress -0.02 0.18 1.00 0.00
## row_id -0.01 -0.03 0.00 1.00
## Sample Size
## [1] 2092
## Probability values (Entries above the diagonal are adjusted for multiple tests.)
## moa_independence idea stress row_id
## moa_independence 0.00 0.00 1.00 1.00
## idea 0.00 0.00 0.00 0.58
## stress 0.44 0.00 0.00 1.00
## row_id 0.68 0.14 0.98 0.00
##
## To see confidence intervals of the correlations, print with the short=FALSE option
# CHECK FOR ANY CORRELATIONS AMONG YOUR IVs ABOVE .70 --> BAD (aka multicollinearity)
# ONLY use the commented out section below IF if you need to remove outliers AFTER examining the Cook's distance and a Residuals vs Leverage plots in your HW -- remember we practiced this in the ANOVA lab
cont <- subset(cont, row_id!=c(1681) & row_id!=c(392))
# use the lm() command to run the regression. Put DV on the left, IVs on the right separated by "+"
reg_model <- lm(moa_independence ~ idea + stress , data = cont )
Assumptions we’ve discussed previously:
New assumptions:
needed <- 80 + 8*2
nrow(cont) >= needed
## [1] TRUE
NOTE: For your homework, if you don’t have the required number of cases reach out to me and we can figure out the best way to proceed!
# Variance Inflation Factor = VIF
vif(reg_model)
## idea stress
## 1.034219 1.034219
NOTE: For your homework, you will need to discuss multicollinearity and any high values in “Issues with My Data”, but you don’t have to drop any variables.
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. However, a bit of deviation is okay – just like with skewness and kurtosis, there’s a range that we can work in before non-normality becomes a critical issue. For some examples of good Residuals vs Fitted plot and ones that show serious errors, check out this page.
plot(reg_model, 1)
NOTE: For your homework, you’ll simply need to generate this plot and talk about whether the assumption was met in “Issues with My Data”. This is going to be a judgement call, and that’s okay!
The plots below both address 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 first plot, Cook’s distance, is a visualization of a score called (you guessed it) Cook’s distance, calculated for each case (aka row or participant) in the dataframe. Cook’s distance tells us how much the regression would change if the point was removed. The second plot also includes the residuals in the examination of leverage. The standardized residuals are on the y-axis and leverage is on the x-axis; this shows us which points have high residuals (are far from the regression line) and high leverage. Points that have large residuals and high leverage are especially worrisome, because they are far from the regression line but are also exerting a large influence on it.
# Cook's distance
plot(reg_model, 4)
# Residuals vs Leverage
plot(reg_model, 5)
NOTE: For your homework, you’ll simply need to generate these plots, assess Cook’s distance in your dataset, and then identify and remove any potential cases that are prominent outliers (like we did in the ANOVA lab). You will make a note of this in the “Issues with My Data” write-up.
This plot is a bit new. It’s called a Q-Q plot and shows the standardized residuals plotted against a normal distribution. If our variables are perfectly normal, the points will fit on the dashed line perfectly. This page shows how different types of non-normality appear on a Q-Q plot.
It’s normal for Q-Q plots show a bit of deviation at the ends. This page shows some examples that help us put our Q-Q plot into context.
plot(reg_model, 2)
NOTE: For your homework, you’ll simply need to generate this plot and think about how your plot compares to the normal/non-normal plots pictured in the links above. Does it seem like the points lie mostly along the straight diagonal line with either no or some minor deviations along each of the tails? If so, your residuals are likely normal enough to meet the assumption. You will talk about this in the write-up below.
Before interpreting our results, we assessed our variables to see if they met the assumptions for a multiple linear regression. We determined we had an out of range kurtosis for our inventory of the dimensions of emerging adulthood. We detected slight issues with linearity in a Residuals vs Fitted plot. We did detect two outliers and removed 392 and 1681 (by visually analyzing Cook’s Distance and Residuals vs Leverage plots) or any serious issues with the normality of our residuals (by visually analyzing a Q-Q plot), and there were not any issues of multicollinearity among our two independent variables.
summary(reg_model)
##
## Call:
## lm(formula = moa_independence ~ idea + stress, data = cont)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.5864 -0.4755 0.2406 0.7386 1.6910
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.21999 0.11499 1.913 0.0559 .
## idea 0.15180 0.02250 6.747 1.95e-11 ***
## stress -0.07238 0.03684 -1.965 0.0496 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9896 on 2087 degrees of freedom
## Multiple R-squared: 0.0216, Adjusted R-squared: 0.02067
## F-statistic: 23.04 on 2 and 2087 DF, p-value: 1.266e-10
# Note for section below: to type lowercase Beta below (ß) you need to hold down Alt key and type 225 on numeric keypad. If that doesn't work you should be able to copy/paste it from somewhere else
Effect size, based on Regression ß (Beta Estimate) value in our output
To test our hypothesis that independence and inventory of dimensions of emerging adulthood would significantly predict stress, we used a multiple linear regression to model the associations between these variables. We confirmed that our data met the assumptions of a linear regression, aside from there being slight issues with linearity.
Our hypothesis was supported. The model was statistically significant, Adj. R2 = .02, F(2, 23.04) = , p < .001 . Our results indicate that inventory of inventory of dimension of emerging adulthood positively predicted stress and had a large effect size (ß > .50; per Cohen, 1988), while independence negatively predicted stress and had a trivial effect size (ß < .10). Full output from the regression model is reported in Table 1. This means that people’s stress increases by 0.5 units for every one unit increase in their inventory of dimensions of emerging adulthood, while it decreases by 0.05 units for every one unit increase in their independence.
| Stress | ||||
|---|---|---|---|---|
| Predictors | Estimates | SE | CI | p |
| Intercept | 0.22 | 0.11 | -0.01 – 0.45 | 0.056 |
| Independence | 0.15 | 0.02 | 0.11 – 0.20 | <0.001 |
| Inventory of Dimension of Emerging Adulthood | -0.07 | 0.04 | -0.14 – -0.00 | 0.050 |
| Observations | 2090 | |||
| R2 / R2 adjusted | 0.022 / 0.021 | |||
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.