#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
#install.packages("expss")
library(psych) # for the describe() command
library(ggplot2) # to visualize our results
library(expss) # for the cross_cases() command
library(car) # for the leveneTest() command
library(afex) # to run the ANOVA
library(ggbeeswarm) # to run plot results
library(emmeans) # for posthoc tests
# For HW, import the project 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)
# new code! this adds a column with a number for each row. It will make it easier if we need to drop outliers later
d$row_id <- 1:nrow(d)
Note: For your HW, you will choose to run EITHER a one-way ANOVA (a single IV with 3 or more levels) OR a two-way/factorial ANOVA (two IVs with 2 or 3 levels each). You will need to specify your hypothesis and customize your code based on the choice you make. We will run BOTH versions of the test in the lab for illustrative purposes.
We predict that there will be a significant difference in Narcissistic Personality Inventory by people’s level of Education between High school Diploma or Less, Completed Bachelor’s Degree, and Currently in Graduate Education.
# you only need to check the variables you're using in the current analysis
str(d)
## 'data.frame': 3070 obs. of 8 variables:
## $ ResponseID : chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ edu : chr "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
## $ income : chr "1 low" "1 low" "rather not say" "rather not say" ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ npi : num 0.6923 0.1538 0.0769 0.0769 0.7692 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables of interest "factors"
# because we'll use our newly created row ID variable for this analysis, so make sure it's coded as a factor, too.
d$edu <- as.factor(d$edu)
#d$row_id <- as.factor(d$row_id)
d <- subset(d, substr (edu, 1, 1) %in% c("1", "5", "6"))
d$edu <- as.factor(d$edu)
d$edu <- droplevels (d$edu)
# check that our categorical variables of interest are now factors
str(d)
## 'data.frame': 317 obs. of 8 variables:
## $ ResponseID : chr "R_2TGbiBXmAtxywsD" "R_2Quh0h3wxTjZjKP" "R_1gTNDGWsqikPuEX" "R_3QXexLyuOYNWmzg" ...
## $ edu : Factor w/ 3 levels "1 High school diploma or less, and NO COLLEGE",..: 2 2 2 3 2 2 2 2 2 2 ...
## $ income : chr "1 low" "1 low" "1 low" "1 low" ...
## $ belong : num 4.2 3.9 3 3.8 1.5 3.9 4.2 4.2 3.1 2.5 ...
## $ swb : num 4.17 3.67 5.33 2.33 5.67 ...
## $ moa_independence: num 3.67 4 3.67 3.5 1.5 ...
## $ npi : num 0.154 0.615 0 0.769 0.231 ...
## $ row_id : int 2 7 16 28 30 53 54 55 56 59 ...
# check our DV skew and kurtosis
describe(d$npi)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 317 0.27 0.3 0.15 0.23 0.23 0 1 1 0.96 -0.6 0.02
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$npi, group = d$edu)
##
## Descriptive statistics by group
## group: 1 High school diploma or less, and NO COLLEGE
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 53 0.33 0.34 0.15 0.3 0.23 0 1 1 0.59 -1.3 0.05
## ------------------------------------------------------------
## group: 5 Completed Bachelors Degree
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 133 0.26 0.28 0.15 0.22 0.11 0 1 1 1.13 -0.17 0.02
## ------------------------------------------------------------
## group: 6 Currently in graduate education
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 131 0.25 0.29 0.08 0.21 0.11 0 0.92 0.92 0.93 -0.72 0.03
# also use histograms to examine your continuous variable
hist(d$npi)
table(d$edu)
##
## 1 High school diploma or less, and NO COLLEGE
## 53
## 5 Completed Bachelors Degree
## 133
## 6 Currently in graduate education
## 131
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# use this commented out section below ONLY IF if you need to remove outliers
# to drop a single outlier, use this code:
#d <- subset(d, row_id!=c(1108)) # NOTE: Participant 1108 is not truly problematic in the Lab, but we are going to removed them for demonstration.
# to drop multiple outliers, use this code:
# d <- subset(d, row_id!=c(1108) & row_id!=c(602))
# use the lm() command to run the regression
# formula is y~x, where y is our DV, x is our IV
reg_model <- lm(npi~edu, data = d )
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
# IF you find outliers, go back up to line 114 or 117 and remove it/them, then re-run the reg_model code
# use the leveneTest() command from the car package to test homogeneity of variance
# uses the 'formula' setup: formula is y~x, where y is our DV and x is our IV
leveneTest(npi~edu, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.978 0.1401
## 314
Our cell sizes are unbalanced between the levels of education group levels. A small sample size for one of the levels of our variable (, n = 53) limits our power and increases our type II error rate.
Levene’s test was not significant for our three-level education type variable with the One-Way ANOVA.
[UPDATE this section in your HW.]
aov_model <- aov_ez(data = d ,
id = "ResponseID" ,
between = c("edu"),
dv = "npi",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: edu
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: npi
## Effect df MSE F pes p.value
## 1 edu 2, 314 0.09 1.34 .008 .263
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
ANOVA Effect Size [partial eta-squared] cutoffs from Cohen (1988): * η^2 < 0.01 indicates a trivial effect * η^2 >= 0.01 indicates a small effect * η^2 >= 0.06 indicates a medium effect * η^2 >= 0.14 indicates a large effect
afex_plot(aov_model, x = "edu" )
Remember: We ONLY run posthoc IF the ANOVA test is SIGNIFICANT! E.g., only run the posthoc tests on pet type if there is a main effect for pet type
emmeans(aov_model, specs="edu")
## edu emmean SE df lower.CL
## 1 High school diploma or less, and NO COLLEGE 0.327 0.0405 314 0.247
## 5 Completed Bachelors Degree 0.256 0.0256 314 0.206
## 6 Currently in graduate education 0.252 0.0257 314 0.201
## upper.CL
## 0.406
## 0.306
## 0.303
##
## Confidence level used: 0.95
pairs(emmeans(aov_model, specs="edu", adjust="tukey"))
## contrast
## 1 High school diploma or less, and NO COLLEGE - 5 Completed Bachelors Degree
## 1 High school diploma or less, and NO COLLEGE - 6 Currently in graduate education
## 5 Completed Bachelors Degree - 6 Currently in graduate education
## estimate SE df t.ratio p.value
## 0.07034 0.0479 314 1.469 0.3071
## 0.07465 0.0480 314 1.556 0.2665
## 0.00431 0.0363 314 0.119 0.9923
##
## P value adjustment: tukey method for comparing a family of 3 estimates
To test our hypothesis that there will be a significant difference in Narcissistic Personality Inventory scores based on peoples level of education (highs school diploma or less, completed bachelors degree, and currently in graduate education), we used a one-way ANOVA. Our data was unbalanced, with many more people who completed a bachelors degree (n = 133 ) or are currently in graduate education (n = 131) participating in our survey than those with a high school diploma or less (n = 53 ). This significantly reduces the power of our test and increases the chances of a Type II error. A non significant Levene’s test (p =.140 ) indicates that there is no evidence that our data fails the assumption of homogeneity of variance.
We found no significant effect of education level on Narcissistic Personlaity Inventory Scores, F(2, 314) = 1.34, p = .263 , ηp2 = .008 (trivial effect; Cohen, 1988). Posthoc tests using Tukey’s HSD adjustment were not conducted because the one-way ANOVA was not statistically significant. Therefore, our hypothesis was not supported.