#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 people’s reported level of narcissism will significantly predict their level of exploitativeness, and that the relationship will be positive. This means that as people report higher levels of narcissism, they will also report higher levels of exploitativeness.
My independent variable (the one doing the predicting) is narcissism. My dependent variable (the one being predicted) is exploitativeness.
# 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': 3150 obs. of 7 variables:
## $ ResponseID: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ gender : chr "f" "m" "m" "f" ...
## $ marriage5 : chr "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
## $ efficacy : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
## $ npi : num 0.6923 0.1538 0.0769 0.0769 0.7692 ...
## $ exploit : num 2 3.67 4.33 1.67 4 ...
# 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 3150 1575.50 909.47 1575.50 1575.50 1167.55 1.0 3150 3149.0
## gender* 2 3150 1.28 0.49 1.00 1.21 0.00 1.0 3 2.0
## marriage5* 3 3150 1.87 0.60 2.00 1.83 0.00 1.0 4 3.0
## efficacy 4 3150 3.13 0.45 3.10 3.13 0.44 1.1 4 2.9
## belong 5 3150 3.23 0.61 3.30 3.25 0.59 1.3 5 3.7
## npi 6 3150 0.28 0.31 0.15 0.24 0.23 0.0 1 1.0
## exploit 7 3150 2.39 1.37 2.00 2.21 1.48 1.0 7 6.0
## skew kurtosis se
## ResponseID* 0.00 -1.20 16.20
## gender* 1.39 0.86 0.01
## marriage5* 0.47 1.48 0.01
## efficacy -0.26 0.51 0.01
## belong -0.26 -0.12 0.01
## npi 0.94 -0.69 0.01
## exploit 0.95 0.36 0.02
# next, use histograms to examine your continuous variables
hist(d$npi)
hist(d$exploit)
# 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$npi, d$exploit)
# to calculate standardized coefficients for the regression, we have to standardize our IV
d$npi_std <- scale(d$npi, 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(exploit ~ npi_std, data = d)
# NO PEEKING AT YOUR MODEL RESULTS YET!
NOTE: We will NOT be evaluating whether our data meets this last assumption in this lab/homework.
# 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 = npi_std, y = exploit)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
geom_segment(aes(xend = npi_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.
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.
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: Our data does not contain any severe outliers.
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 minor non-linearity, but not enough to violate the assumption of linearity. We also checked Cook’s distance plot to detect outliers. All data were below the recommended cutoff for Cook’s distance of 0.5, so no outliers were detected.
summary(reg_model)
##
## Call:
## lm(formula = exploit ~ npi_std, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.5463 -1.0511 -0.2607 0.8251 4.9393
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.38635 0.02282 104.59 <2e-16 ***
## npi_std 0.49537 0.02282 21.71 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.281 on 3148 degrees of freedom
## Multiple R-squared: 0.1302, Adjusted R-squared: 0.1299
## F-statistic: 471.2 on 1 and 3148 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 reported levels of narcissism would significantly predict a person’s reported level of exploitativeness, 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 narcissism significantly predicted exploitativeness, F(1, 3148) = 471.2, p < 0.001 , Adj. R2 = 0.1299. Additionally, the relationship between narcissism and exploitativeness was positive, ß = 0.49, t(3148) = 21.71, p < 0.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.