#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="multi_regression_labdata.csv", header=T)
# YOU WILL DELETE THESE LINES IN YOUR HW
set.seed(110)
d$fake <- (rnorm(n=nrow(d))-d$satis)*d$rel_stability
d$fake <- d$fake+(d$fake*rnorm(n=nrow(d)))
We hypothesize that partner attractiveness, relationship satisfaction, and fake data (a fake variable generated for this lab) will significantly predict relationship stability.
# 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': 3166 obs. of 7 variables:
## $ ResponseId : chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ gender : chr "f" "m" "m" "f" ...
## $ race_rc : chr "white" "white" "white" "other" ...
## $ satis : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ part_attract : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
## $ rel_stability: num 4.33 4.17 1.83 5.17 3.67 ...
## $ fake : num -30.78 4.09 -3.81 -19.12 -16.99 ...
# Place only variables of interest in new dataframe
cont <- na.omit(subset(d, select=c(rel_stability, part_attract, satis, fake)))
# Standardize all IVs
cont$part_attract <- scale(cont$part_attract, center=T, scale=T)
cont$satis <- scale(cont$satis, center=T, scale=T)
cont$fake <- scale(cont$fake, 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 range skew
## rel_stability 1 3166 4.47 1.32 4.67 4.53 1.48 1.00 7.00 6.00 -0.36
## part_attract 2 3166 0.00 1.00 0.11 0.03 0.98 -3.19 2.92 6.11 -0.26
## satis 3 3166 0.00 1.00 -0.06 0.01 1.00 -4.54 1.96 6.50 -0.24
## fake 4 3166 0.00 1.00 0.15 0.07 0.82 -5.18 3.84 9.02 -0.89
## kurtosis se
## rel_stability -0.45 0.02
## part_attract -0.12 0.02
## satis 0.44 0.02
## fake 2.17 0.02
# also use histograms to examine your continuous variables
hist(cont$part_attract)
hist(cont$satis)
hist(cont$fake)
hist(cont$rel_stability)
# last, use scatterplots to examine your continuous variables together
plot(cont$part_attract, cont$rel_stability) # PUT YOUR DV 2ND (Y-AXIS)
plot(cont$satis, cont$rel_stability) # PUT YOUR DV 2ND (Y-AXIS)
plot(cont$fake, cont$rel_stability) # PUT YOUR DV 2ND (Y-AXIS)
plot(cont$part_attract, cont$satis)
plot(cont$part_attract, cont$fake)
plot(cont$satis, cont$fake)
corr_output_m <- corr.test(cont)
corr_output_m
## Call:corr.test(x = cont)
## Correlation matrix
## rel_stability part_attract satis fake
## rel_stability 1.00 -0.14 0.39 -0.26
## part_attract -0.14 1.00 -0.26 0.06
## satis 0.39 -0.26 1.00 -0.19
## fake -0.26 0.06 -0.19 1.00
## Sample Size
## [1] 3166
## Probability values (Entries above the diagonal are adjusted for multiple tests.)
## rel_stability part_attract satis fake
## rel_stability 0 0 0 0
## part_attract 0 0 0 0
## satis 0 0 0 0
## fake 0 0 0 0
##
## To see confidence intervals of the correlations, print with the short=FALSE option
# CHECK FOR CORRELATIONS AMONG IVs ABOVE .70 --> BAD (aka multicollinearity)
# use this commented out section below ONLY 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$row_id <- 1:nrow(cont)
#d <- subset(d, row_id!=c(1970))
# use the lm() command to run the regression
# DV on the left, IVs on the right separated by "+"
reg_model <- lm(rel_stability ~ part_attract + satis + fake, data = cont)
Assumptions we’ve discussed previously:
New assumptions:
needed <- 80 + 8*3
nrow(cont) >= needed
## [1] TRUE
NOTE: For your homework, if you don’t have the required number of cases you’ll need to drop one of your independent variables. Reach out to me and we can figure out the best way to proceed!
# Variance Inflation Factor = VIF
vif(reg_model)
## part_attract satis fake
## 1.073156 1.107768 1.036399
NOTE: For your homework, you will need to discuss multicollinearity and any high values, 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 your assumptions are met. This is going to be a judgement call, and that’s okay! In practice, you’ll always be making these judgement calls as part of a team, so this assignment is just about getting experience with it, not making the perfect call.
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 any potential cases that are prominent outliers. Since we have some cutoffs, that makes this process is a bit less subjective than some of the other assessments we’ve done here, which is nice change!
This plot shows us the standardized residuals across the range of the regression line. Because the residuals are standarized, large residuals (whether positive or negative) are at the top of the plot, while small residuals (whether positive or negative) are at the bottom of the plot. If the assumption of homogeneity of variance (also called homoscedasticity) is met, the red line should be mostly flat, horizontal. If it deviates from the mean line, that means that the variance is smaller or larger at that point of the regression line.
You can check out this page for some other examples of this type of plot.
plot(reg_model, 3)
### LAB ONLY - DELETE CODE BELOW FOR HW
## We can use the residual plot below to see the variance issues with our ‘fake’ variable.
library(broom)
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
model.diag.metrics <- augment(reg_model)
ggplot(model.diag.metrics, aes(x = rel_stability, y = fake)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
geom_segment(aes(xend = rel_stability, 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'
NOTE: For your homework, you’ll simply need to generate the Scale-Location plot and talk about how your plot compares to the ones pictured in the link above. Is it closer to the ‘good’ plots or one of the ‘bad’ plots? Again, this is a judgement call! It’s okay if feel uncertain, and you won’t be penalized for that.
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 talk about how your plot compares to the ones pictured. Does it seem like any skew or kurtosis (i.e., thin/thick tails) is indicated by your plot ? Is it closer to the ‘good’ or ‘bad’ plots from the second link above?
Before interpreting our results, we assessed our variables to see if they met the assumptions for a multiple linear regression. We analyzed a Scale-Location plot and detected slight issues with homogeneity of variance, as well as slight issues with linearity in a Residuals vs Fitted plot. However, we did not detect any outliers (visually analyzing Cook’s Distance and Residuals vs Leverage plots) or any serious issues with the normality of our residuals (visually analyzing a Q-Q plot), nor were there any issues of multicollinearity among our three independent variables.
summary(reg_model)
##
## Call:
## lm(formula = rel_stability ~ part_attract + satis + fake, data = cont)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.0387 -0.8088 0.0793 0.8873 3.4071
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.47447 0.02112 211.867 <2e-16 ***
## part_attract -0.05463 0.02188 -2.497 0.0126 *
## satis 0.46049 0.02223 20.713 <2e-16 ***
## fake -0.25210 0.02150 -11.724 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.188 on 3162 degrees of freedom
## Multiple R-squared: 0.1927, Adjusted R-squared: 0.192
## F-statistic: 251.6 on 3 and 3162 DF, p-value: < 2.2e-16
# you do not have to run the below code for the HW
#reg_model_test <- lm(rel_stability ~ satis, data = cont)
#summary(reg_model_test)
# 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 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 partner attractiveness, relationship satisfaction, and a fake variable (created for this assignment) would significantly predict relationship stability, 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, and although there were slight issues with homogeneity of variance and linearity we still continued on with the analysis.
Our model was statistically significant, Adj. R2 = .19 , F(3,3,162) = 251.6, <.001 . The relationship between relationship satisfaction and relationship stability was positive and has a medium effect size (per Cohen, 1988), while the relationships between our remaining predictors (partner attractiveness and our fake variable) and relationship stability were negative and had effect sizes that were small (fake variable) or trivial (partner attractiveness). Full output from the regression model is reported in Table 1.
Relationship Stability | ||||
---|---|---|---|---|
Predictors | Estimates | SE | CI | p |
Intercept | 4.47 | 0.02 | 4.43 – 4.52 | <0.001 |
Partner Attractiveness | -0.05 | 0.02 | -0.10 – -0.01 | 0.013 |
Satisfaction | 0.46 | 0.02 | 0.42 – 0.50 | <0.001 |
Fake | -0.25 | 0.02 | -0.29 – -0.21 | <0.001 |
Observations | 3166 | |||
R2 / R2 adjusted | 0.193 / 0.192 |
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.