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
##
## Use magrittr pipe '%>%' to chain several operations:
## mtcars %>%
## let(mpg_hp = mpg/hp) %>%
## take(mean(mpg_hp), by = am)
##
##
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
## To return to the console output, use 'expss_output_default()'.
##
## 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
## 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
library(emmeans) # for posthoc tests
## 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="/Users/ethanshinaver/Library/CloudStorage/OneDrive-Personal/Social Psych Lab/Research/final paper/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 (at least 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.
One-Way: There will be a significant difference in Mindfulness by people’s level of Marital Status of their parents (currently married, currently divorced, never married and together, never married each other and are not together.)
# you only need to check the variables you're using in the current analysis
str(d)
## 'data.frame': 3147 obs. of 8 variables:
## $ ResponseID: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ 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" ...
## $ gender : chr "f" "m" "m" "f" ...
## $ socmeduse : int 47 23 34 35 37 13 37 43 37 29 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ mindful : num 2.4 1.8 2.2 2.2 3.2 ...
## $ 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$mindful <- as.factor(d$mindful)
d$marriage5 <- as.factor(d$marriage5)
d$row_id <- as.factor(d$row_id)
# we're going to recode our race variable into two groups: poc and white
# in doing so, we are creating a new variable "poc" that has 2 levels
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame': 3147 obs. of 8 variables:
## $ ResponseID: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ marriage5 : Factor w/ 4 levels "are currently divorced from one another",..: 1 2 2 2 2 2 2 2 2 3 ...
## $ gender : chr "f" "m" "m" "f" ...
## $ socmeduse : int 47 23 34 35 37 13 37 43 37 29 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ mindful : Factor w/ 73 levels "1.133333333",..: 19 10 16 16 31 34 45 28 34 56 ...
## $ npi : num 0.6923 0.1538 0.0769 0.0769 0.7692 ...
## $ row_id : Factor w/ 3147 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$mindful)
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1* 1 3147 38.64 12.64 39 38.73 11.86 1 73 72 -0.06 -0.15
## se
## X1* 0.23
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$mindful, group = d$marriage5 )
## Warning in `[[<-.factor`(`*tmp*`, i, value = 19): invalid factor level, NA
## generated
##
## Descriptive statistics by group
## group: are currently divorced from one another
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 732 38.12 12.58 38 37.99 13.34 1 71 70 0.06 -0.19 0.47
## ------------------------------------------------------------
## group: are currently married to one another
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1* 1 2121 38.58 12.64 39 38.68 11.86 1 73 72 -0.06 -0.16
## se
## X1* 0.27
## ------------------------------------------------------------
## group: never married each other and are not together
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 246 39.91 12.34 41 40.61 11.86 3 73 70 -0.5 0.38 0.79
## ------------------------------------------------------------
## group: never married each other but are currently together
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 47 43.06 13.74 45 42.95 16.31 14 68 54 -0.02 -0.81 2
# also use histograms to examine your continuous variable
d$mindful <- as.numeric(as.character(d$mindful))
hist(d$mindful)
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# One-Way
table(d$marriage5)
##
## are currently divorced from one another
## 733
## are currently married to one another
## 2121
## never married each other and are not together
## 246
## never married each other but are currently together
## 47
## If cross_cases() doesn't work for you, then use xtabs() instead. Fill in the code below and remove the "#" to run. Then hashtag out the cross_cases() line above.
#xtabs(~ V1 + V2, data=d)
# 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 (dont want levene test to be significant)
# One-Way
leveneTest(mindful~marriage5, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 0.5174 0.6703
## 3143
# 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.
# One-Way
reg_model <- lm(mindful~marriage5, data = d)
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
Our cell sizes are somewhat unbalanced between the Marital Status group levels. A small sample size for one of the levels of our variable (n=47) slightly limits our power and may increase our Type II error rate.
Cooks Distance found three influential cases row_ids 1285, 1579, and 2130). These values are significantly high but still within the range of the mindfulness scale. Given the large sample size, these were kept in analysis.
# One-Way
aov_model <- aov_ez(data = d,
id = "row_id",
between = c("marriage5"),
dv = "mindful",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: marriage5
# One-Way
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: mindful
## Effect df MSE F pes p.value
## 1 marriage5 3, 3143 0.71 3.23 * .003 .022
## ---
## 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 = "marriage5")
# NOTE: for the Two-Way, you will need to decide which plot version makes the MOST SENSE based on your data / rationale when you make the nice Figure 2 at the end.
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="marriage5", adjust="sidak")
## marriage5 emmean SE df
## are currently divorced from one another 3.67 0.0311 3143
## are currently married to one another 3.71 0.0183 3143
## never married each other and are not together 3.79 0.0537 3143
## never married each other but are currently together 4.00 0.1230 3143
## lower.CL upper.CL
## 3.60 3.75
## 3.66 3.75
## 3.66 3.93
## 3.70 4.31
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="marriage5", adjust="sidak"))
## contrast
## are currently divorced from one another - are currently married to one another
## are currently divorced from one another - never married each other and are not together
## are currently divorced from one another - never married each other but are currently together
## are currently married to one another - never married each other and are not together
## are currently married to one another - never married each other but are currently together
## never married each other and are not together - never married each other but are currently together
## estimate SE df t.ratio p.value
## -0.0326 0.0361 3143 -0.905 0.8024
## -0.1213 0.0620 3143 -1.955 0.2056
## -0.3315 0.1270 3143 -2.617 0.0442
## -0.0886 0.0567 3143 -1.563 0.4000
## -0.2989 0.1240 3143 -2.407 0.0759
## -0.2102 0.1340 3143 -1.568 0.3969
##
## P value adjustment: tukey method for comparing a family of 4 estimates
To test our hypothesis that there will be a significant difference in people’s level of mindfulness based on the parental marital status (currently married, currently divorced, never married and together, never married each other and are not together.) we used a one-way ANOVA. Our data was somewhat unbalanced, with the “never married but currently together” group being relatively small (n = 47) compared to other groups “currently divorced” (n = 733), “currently married” (n = 2121), “never married and not together” (n = 246). This may slightly reduce the power of our test and increase the chance of a Type II error. The Cooks distance flagged three potentially influential points (row_ids 1285, 1579, 2130) but their mindfulness values were within the valid range, so we kept them. A non significant Levene’s test (p = 0.67) also indicates that our data met th assumption of homogeneity of variance.
We found a significant effect of parental marital status type, F(3, 3143) = 3.23, p < .05, ηp2 = .003 (small effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants whose parents were never married but are currently together (M = 4.00, SE = .12) reported higher levels of mindfulness than those whose parents are currently divorced (M = 3.67, SE = .03) (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.