#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
library(psych) # for the describe() command
## Warning: package 'psych' was built under R version 4.4.3
library(ggplot2) # to visualize our results
## Warning: package 'ggplot2' was built under R version 4.4.3
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
library(expss) # for the cross_cases() command
## Warning: package 'expss' was built under R version 4.4.3
## Loading required package: maditr
## Warning: package 'maditr' was built under R version 4.4.3
##
## To drop variable use NULL: let(mtcars, am = NULL) %>% head()
##
## Attaching package: 'expss'
## The following object is masked from 'package:ggplot2':
##
## vars
library(car) # for the leveneTest() command
## Warning: package 'car' was built under R version 4.4.3
## 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
## Warning: package 'afex' was built under R version 4.4.3
## 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(ggbeeswarm) # to run plot results
## Warning: package 'ggbeeswarm' was built under R version 4.4.3
library(emmeans) # for posthoc tests
## Warning: package 'emmeans' was built under R version 4.4.3
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
# 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 at least 3 levels) OR a two-way ANOVA (two IVs, each with 2 levels). You will need to specify your hypothesis and customize your code based on the choice you make (i.e., delete code that is not relevant). We will run BOTH versions in the lab for illustrative purposes.
One-Way Hypothesis:There will be a significant difference in Mental Flexibility by people’s level of relationship status between: single, never married; single, divorced or widowed;, and in a relationship or married and cohabiting;.
IV = Relationship status DV = Mental Flexibility
# you only need to check the variables you're using in the current analysis
# even if 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': 328 obs. of 8 variables:
## $ X : int 1 401 469 1390 2689 2752 2835 3935 4050 4058 ...
## $ relationship_status: chr "In a relationship/married and cohabiting" "Single, divorced or widowed" "In a relationship/married and cohabiting" "In a relationship/married and cohabiting" ...
## $ income : chr "3 high" "3 high" "2 middle" "3 high" ...
## $ iou : num 3.19 2.81 2.59 1.48 3.26 ...
## $ mfq_26 : num 4.2 4.7 4.6 2.95 4.15 3.6 5.2 3.15 4.55 3.7 ...
## $ gad : num 1.86 2.14 1.71 1 4 ...
## $ edeq12 : num 1.58 3.08 1.83 1.5 1.5 ...
## $ 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$relationship_status <- as.factor(d$relationship_status)
d$mfq_26 <- as.factor(d$mfq_26)
# We're going to recode our race variable into two groups for the Two-Way ANOVA: poc and white
# in doing so, we are creating a new variable "poc" that has 2 levels
table(d$relationship_status)
##
## In a relationship/married and cohabiting
## 263
## In a relationship/married but living apart
## 14
## Prefer not to say
## 6
## Single, divorced or widowed
## 34
## Single, never married
## 11
## NOTE: This is a FOUR STEP process!
d<- subset(d,relationship_status != "Prefer not to say") # use subset() to remove all participants from the additional level
table(d$relationship_status, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## In a relationship/married and cohabiting
## 263
## In a relationship/married but living apart
## 14
## Prefer not to say
## 0
## Single, divorced or widowed
## 34
## Single, never married
## 11
## <NA>
## 0
d$relationship_status <- droplevels(d$relationship_status) # use droplevels() to drop the empty factor
table(d$relationship_status, useNA = "always") # verify that now the entire factor level is removed
##
## In a relationship/married and cohabiting
## 263
## In a relationship/married but living apart
## 14
## Single, divorced or widowed
## 34
## Single, never married
## 11
## <NA>
## 0
d<- subset(d,relationship_status != "In a relationship/married but living apart") # use subset() to remove all participants from the additional level
table(d$relationship_status, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## In a relationship/married and cohabiting
## 263
## In a relationship/married but living apart
## 0
## Single, divorced or widowed
## 34
## Single, never married
## 11
## <NA>
## 0
d$relationship_status <- droplevels(d$relationship_status) # use droplevels() to drop the empty factor
table(d$relationship_status, useNA = "always") # verify that now the entire factor level is removed
##
## In a relationship/married and cohabiting
## 263
## Single, divorced or widowed
## 34
## Single, never married
## 11
## <NA>
## 0
# For your HW, you can choose to combine levels like we did here, OR you can simply choose which existing levels you want to compare/test -- to do this option, you'll need to copy/paste the "drop levels" code from the t-test lab/HW and delete the recoding code line above.
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame': 308 obs. of 8 variables:
## $ X : int 1 401 469 1390 2689 2752 2835 3935 4050 4058 ...
## $ relationship_status: Factor w/ 3 levels "In a relationship/married and cohabiting",..: 1 2 1 1 3 1 2 1 1 1 ...
## $ income : chr "3 high" "3 high" "2 middle" "3 high" ...
## $ iou : num 3.19 2.81 2.59 1.48 3.26 ...
## $ mfq_26 : Factor w/ 58 levels "2.4","2.9","2.95",..: 26 36 34 3 25 14 46 6 33 16 ...
## $ gad : num 1.86 2.14 1.71 1 4 ...
## $ edeq12 : num 1.58 3.08 1.83 1.5 1.5 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$mfq_26)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 308 32.83 11.81 34 33.35 11.86 1 58 57 -0.41 -0.17 0.67
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$mfq_26, group = d$relationship_status)
## Warning in `[[<-.factor`(`*tmp*`, i, value = 26): invalid factor level, NA
## generated
##
## 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 262 32.67 11.4 34 33.2 10.38 2 58 56 -0.41 -0.16 0.7
## ------------------------------------------------------------
## group: Single, divorced or widowed
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 34 35.03 12.05 36.5 35.57 11.86 1 54 53 -0.55 0.02 2.07
## ------------------------------------------------------------
## group: Single, never married
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 11 30.36 19.42 34 30.44 26.69 5 55 50 -0.07 -1.71 5.86
# also use histograms to examine your continuous variable
hist(as.numeric(as.character(d$mfq_26)))
# One-Way
table(d$relationship_status)
##
## In a relationship/married and cohabiting
## 263
## Single, divorced or widowed
## 34
## Single, never married
## 11
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# 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
# One-Way
d$mfq_26 <- as.numeric(as.character(d$mfq_26))
leveneTest(mfq_26~relationship_status, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 5.2548 0.005706 **
## 305
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 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.
reg_model <- lm(mfq_26~relationship_status, data = d) #for One-Way
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
## Issues with My Data
Our cell sizes are somewhat unbalanced between the relationship status group levels. 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 was significant for our three-level relationship status variable with the One-Way ANOVA. We are acknowledging this and continuing with the analysis anyway for class purposes.
# Run an ANOVA
```r
# One-Way
d$mfq_26 <- as.numeric(as.character(d$mfq_26))
aov_model <- aov_ez(data = d,
id = "X",
between = c("relationship_status"),
dv = "mfq_26",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: relationship_status
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: mfq_26
## Effect df MSE F pes p.value
## 1 relationship_status 2, 305 0.37 0.70 .005 .498
## ---
## 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
# One-Way
afex_plot(aov_model, x = "relationship_status")
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="relationship_status", adjust="sidak")
## relationship_status emmean SE df lower.CL upper.CL
## In a relationship/married and cohabiting 4.53 0.0375 305 4.44 4.62
## Single, divorced or widowed 4.64 0.1040 305 4.39 4.89
## Single, never married 4.42 0.1830 305 3.98 4.86
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model, specs="relationship_status", adjust="sidak"))
## contrast
## (In a relationship/married and cohabiting) - Single, divorced or widowed
## (In a relationship/married and cohabiting) - Single, never married
## Single, divorced or widowed - Single, never married
## estimate SE df t.ratio p.value
## -0.108 0.111 305 -0.979 0.5909
## 0.111 0.187 305 0.596 0.8224
## 0.220 0.211 305 1.043 0.5504
##
## P value adjustment: tukey method for comparing a family of 3 estimates
To test our hypothesis that there will be a significant difference in people’s level of felt mental flexibility based on the relationship status type (single, never married; single, divorced or widowed; and in a relationship or married and cohabiting;), we used a one-way ANOVA. A significant Levene’s test (p = .006) 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 relationship status, F(2, 305) = .70, p = .498, ηp2 = .005 (trivial effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants who are in a relationship/married and cohabiting (M = 4.53, SE = 0.04) reported more mental flexibility than those who are single, never married (M = 4.42, SE = 0.2) but less mental flexibility than those who are Single, divorced or widowed (M = 4.64, SE = 0.1); participants who Single, divorced or widowed reported the highest amount of mental flexibility overall (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.