library(psych) # for the describe() command
library(ggplot2) # to visualize our results
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To modify variables or add new variables:
## let(mtcars, new_var = 42, new_var2 = new_var*hp) %>% head()
##
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
##
## sort_by
##
## Attaching package: 'expss'
## The following object is masked from 'package:ggplot2':
##
## vars
library(car) # for the leveneTest() command
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:expss':
##
## recode
## The following object is masked from 'package:psych':
##
## logit
library(afex) # to run the ANOVA and plot results
## Loading required package: lme4
## Loading required package: Matrix
##
## Attaching package: 'lme4'
## The following object is masked from 'package:expss':
##
## dummy
## ************
## Welcome to afex. For support visit: http://afex.singmann.science/
## - Functions for ANOVAs: aov_car(), aov_ez(), and aov_4()
## - Methods for calculating p-values with mixed(): 'S', 'KR', 'LRT', and 'PB'
## - 'afex_aov' and 'mixed' objects can be passed to emmeans() for follow-up tests
## - Get and set global package options with: afex_options()
## - Set sum-to-zero contrasts globally: set_sum_contrasts()
## - For example analyses see: browseVignettes("afex")
## ************
##
## Attaching package: 'afex'
## The following object is masked from 'package:lme4':
##
## lmer
library(emmeans) # for posthoc tests
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
# import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
# use ARC data
d <- read.csv(file="Data/mydata.csv", header=T)
# new code! this adds a column with a number for each row. it makes it easier when we drop outliers later
d$row_id <- 1:nrow(d)
One-Way: We predict that there will be a significant effect of relationship status on self-esteem, as measured by the Rosenberg Self-Esteem Inventory (rse).
# 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': 1337 obs. of 7 variables:
## $ sexual_orientation : chr "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" ...
## $ relationship_status: chr "In a relationship/married and cohabiting" "Prefer not to say" "Prefer not to say" "In a relationship/married and cohabiting" ...
## $ big5_open : num 5.33 5.33 5 6 5 ...
## $ pswq : num 4.94 3.36 1.86 3.94 2.62 ...
## $ mfq_26 : num 4.2 3.35 4.65 4.65 4.5 4.3 5.25 5 4.7 4.05 ...
## $ rse : num 2.3 1.6 3.9 1.7 3.9 2.4 1.8 1.3 3.5 2.6 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
#d$X <- as.factor(d$X)
#we'll actually use our ID variable for this analysis, so make sure it's coded as a factor
d$sexual_orientation <- as.factor(d$sexual_orientation)
d$relationship_status <- as.factor(d$relationship_status)
d$row_id <- as.factor(d$row_id)
# you can use the describe() command on an entire dataframe (d) or just on a single variable
# Check the class of d$rse
class(d$rse)
## [1] "numeric"
# Convert factor to numeric
d$rse <- as.numeric(as.character(d$rse))
# Now try to describe it
describe(d$rse)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1337 2.61 0.72 2.7 2.62 0.74 1 4 3 -0.17 -0.72 0.02
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$rse, group = d$relationship_status)
##
## Descriptive statistics by group
## group: In a relationship/married and cohabiting
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 282 3.1 0.5 3.1 3.11 0.59 1.4 4 2.6 -0.39 -0.13 0.03
## ------------------------------------------------------------
## group: In a relationship/married but living apart
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 106 2.39 0.76 2.5 2.38 0.89 1 4 3 0.06 -0.97 0.07
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 104 2.45 0.7 2.4 2.44 0.82 1.1 3.9 2.8 0.1 -0.8 0.07
## ------------------------------------------------------------
## group: Single, divorced or widowed
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 44 2.94 0.54 2.9 2.94 0.52 1.2 4 2.8 -0.41 0.86 0.08
## ------------------------------------------------------------
## group: Single, never married
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 801 2.47 0.71 2.4 2.47 0.74 1 4 3 0.03 -0.7 0.03
# also use histograms to examine your continuous variable
hist(d$rse)
# and cross_cases() to examine your categorical variables
cross_cases(d, relationship_status)
|  #Total | |
|---|---|
|  relationship_status | |
|    In a relationship/married and cohabiting | 282 |
|    In a relationship/married but living apart | 106 |
|    Prefer not to say | 104 |
|    Single, divorced or widowed | 44 |
|    Single, never married | 801 |
|    #Total cases | 1337 |
table(d$relationship_status)
##
## In a relationship/married and cohabiting
## 282
## In a relationship/married but living apart
## 106
## Prefer not to say
## 104
## Single, divorced or widowed
## 44
## Single, never married
## 801
# use the leveneTest() command from the car package to test homogeneity of variance
# uses the 'formula' setup: formula is y~x1*x2, where y is our DV and x1 is our first IV and x2 is our second IV
leveneTest(rse~relationship_status, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 4 15.654 1.596e-12 ***
## 1332
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# use this commented out section only if you need to remove outliers
# to drop a single outlier, remove the # at the beginning of the line and use this code:
#d <- subset(d, row_id!=c(1108))
# to drop multiple outliers, remove the # at the beginning of the line and 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~x1*x2 + c, where y is our DV, x1 is our first IV, x2 is our second IV, and c is our covariate
reg_model <- lm(rse ~ relationship_status, data = d) #for one-way
# Cook's distance
plot(reg_model, 4)
# Residuals vs Leverage
plot(reg_model, 5)
Our cell sizes are very unbalanced. A small sample size for one of the levels of our variable limits our power and increases our Type II error rate.
Levene’s test is significant for our relationship status variable (p<0.05). We are ignoring this and continuing with the analysis anyway, but in the real world this is something we would have to correct for.
aov_model <- aov_ez(data = d,
id = "row_id",
between = c("relationship_status"),
dv = "rse",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: relationship_status
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: rse
## Effect df MSE F pes p.value
## 1 relationship_status 4, 1332 0.45 52.54 *** .136 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model, x = "relationship_status")
Only run posthocs if the test is significant! E.g., only run the posthoc tests on relationship status if there is a main effect for relationship status.
emmeans(aov_model, specs="relationship_status", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## relationship_status emmean SE df lower.CL
## In a relationship/married and cohabiting 3.10 0.0399 1332 2.99
## In a relationship/married but living apart 2.39 0.0650 1332 2.23
## Prefer not to say 2.45 0.0657 1332 2.28
## Single, divorced or widowed 2.94 0.1009 1332 2.68
## Single, never married 2.47 0.0237 1332 2.41
## upper.CL
## 3.20
## 2.56
## 2.61
## 3.20
## 2.53
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 5 estimates
pairs(emmeans(aov_model, specs="relationship_status", adjust="tukey"))
## contrast
## (In a relationship/married and cohabiting) - (In a relationship/married but living apart)
## (In a relationship/married and cohabiting) - Prefer not to say
## (In a relationship/married and cohabiting) - Single, divorced or widowed
## (In a relationship/married and cohabiting) - Single, never married
## (In a relationship/married but living apart) - Prefer not to say
## (In a relationship/married but living apart) - Single, divorced or widowed
## (In a relationship/married but living apart) - Single, never married
## Prefer not to say - Single, divorced or widowed
## Prefer not to say - Single, never married
## Single, divorced or widowed - Single, never married
## estimate SE df t.ratio p.value
## 0.7016 0.0763 1332 9.198 <.0001
## 0.6498 0.0768 1332 8.460 <.0001
## 0.1587 0.1085 1332 1.462 0.5874
## 0.6236 0.0464 1332 13.451 <.0001
## -0.0518 0.0924 1332 -0.560 0.9806
## -0.5430 0.1201 1332 -4.522 0.0001
## -0.0780 0.0692 1332 -1.127 0.7921
## -0.4912 0.1204 1332 -4.079 0.0005
## -0.0262 0.0698 1332 -0.376 0.9958
## 0.4650 0.1037 1332 4.485 0.0001
##
## P value adjustment: tukey method for comparing a family of 5 estimates
To test our hypothesis that there would be a significant effect of relationship status on self-esteem, we used a one-way ANOVA. Our data was unbalanced, with many more single, never married participating in our survey (n = 800), and small group of single, divorced or widowed participants (n = 44). This significantly reduces the power of our test and increases the chances of a Type II error. There was no outlier based on the Cook’s distance and Residuals vs Leverage plots. A significant Levene’s test (p = 1.551e-12) also indicates that our data violates the assumption of homogeneity of variance. This suggests that there is an increased chance of Type I error. We continued with our analysis for the purpose of this class.
We found a significant effect of gender, F(4,1331) = 52.55, p < .001, ηp2 = .136 (large effect size; Cohen, 1988). Posthoc tests using sidak’s test revealed that participants who are in a relationship/married and cohabiting reported higher self-esteem than other groups, followed by single/divorced or widowed, single/never married, prefer not to say, and participants who are in a relationship/married but living apart reported the lowest self-esteem. (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.