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 aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)
##
## 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
library(ggbeeswarm) # for afex_plot
# 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="EAMMi2clean.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: I predict that there will be a significant effect of political affiliation on mindfulness, defined by the degrees of non-judgmental-present-moment awareness participants report on the EAMMi2 15-item mindfulness questionnaire.
# 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': 3182 obs. of 8 variables:
## $ ResponseId: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ politics : int 2 1 2 8 1 8 4 2 8 4 ...
## $ sex : int 2 1 1 2 1 2 2 2 2 2 ...
## $ moa : num 3.2 3.1 3.05 2.3 3.1 3.35 3.65 3.7 3.55 2.95 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ stress : num 3.3 3.6 3.3 3.2 3.5 2.9 3.2 3 2.9 3.2 ...
## $ mindful : num 2.4 1.8 2.2 2.2 3.2 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# Add new levels to the factor variable d$pol
levels(d$politics) <- c("1", "2", "3", "4", "5", "6", "7", "8")
# Recode the values in d$pol based on politics categories
d$pol[d$politics %in% c("1", "2", "3")] <- "liberal"
d$pol[d$politics %in% c("4", "8")] <- "moderate/apolitical" # Combine "moderate" and "apolitical" into one level
d$pol[d$politics %in% c("5", "6", "7")] <- "conservative"
# Convert d$pol back to a factor
d$pol <- as.factor(d$pol)
# Check the updated table
table(d$pol)
##
## conservative liberal moderate/apolitical
## 697 1380 1100
# make our categorical variables factors
#we'll actually use our ID variable for this analysis, so make sure it's coded as a factor
d$politics <- as.factor(d$politics)
d$row_id <- as.factor(d$row_id)
# we're going to recode our race/ethnicity variable into two groups: liberal and conservative
# table(d$politics)
#d$pol[d$politics == "1"] <- "liberal"
#d$pol[d$politics == "2"] <- "liberal"
#d$pol[d$politics == "3"] <- "liberal"
#d$pol[d$politics == "4"] <- "moderate"
#d$pol[d$politics == "5"] <- "conservative"
#d$pol[d$politics == "6"] <- "conservative"
#d$pol[d$politics == "7"] <- "conservative"
#d$pol[d$politics == "8"] <- NA
#table(d$pol)
#d$pol <- as.factor(d$pol)
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$mindful)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3173 3.71 0.84 3.73 3.71 0.79 1.13 6 4.87 -0.06 -0.13 0.01
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$mindful, group = d$pol)
##
## Descriptive statistics by group
## group: conservative
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 695 3.75 0.82 3.73 3.75 0.79 1.47 6 4.53 0.05 -0.18 0.03
## ------------------------------------------------------------
## group: liberal
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1377 3.65 0.84 3.67 3.65 0.79 1.13 6 4.87 -0.03 -0.09 0.02
## ------------------------------------------------------------
## group: moderate/apolitical
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1097 3.76 0.85 3.8 3.78 0.89 1.2 6 4.8 -0.15 -0.16 0.03
# also use histograms to examine your continuous variable
hist(d$mindful)
# and cross_cases() to examine your categorical variables
cross_cases(d, politics, pol)
|  pol | |||
|---|---|---|---|
|  conservative |  liberal |  moderate/apolitical | |
|  politics | |||
| Â Â Â 1Â | 235 | ||
| Â Â Â 2Â | 772 | ||
| Â Â Â 3Â | 373 | ||
| Â Â Â 4Â | 568 | ||
| Â Â Â 5Â | 308 | ||
| Â Â Â 6Â | 332 | ||
| Â Â Â 7Â | 57 | ||
| Â Â Â 8Â | 532 | ||
|    #Total cases | 697 | 1380 | 1100 |
DV should be normally distributed across levels of the IV All levels of the IVs should have equal number of cases and there should be no empty cells. Cells with low numbers decrease the power of the test (increase change of Type II error) Homogeneity of variance should be assured Outliers should be identified and removed Sampling distribution should be normal (if assumptions above are confirmed)
table(d$pol)
##
## conservative liberal moderate/apolitical
## 697 1380 1100
cross_cases(d, politics, pol)
|  pol | |||
|---|---|---|---|
|  conservative |  liberal |  moderate/apolitical | |
|  politics | |||
| Â Â Â 1Â | 235 | ||
| Â Â Â 2Â | 772 | ||
| Â Â Â 3Â | 373 | ||
| Â Â Â 4Â | 568 | ||
| Â Â Â 5Â | 308 | ||
| Â Â Â 6Â | 332 | ||
| Â Â Â 7Â | 57 | ||
| Â Â Â 8Â | 532 | ||
|    #Total cases | 697 | 1380 | 1100 |
# our number of small nb participants is going to hurt us for the two-way anova, but it should be okay for the one-way anova
# so we'll create a new dataframe for the two-way analysis and call it d2
# d2 <- subset(d, pol != "apolitical")
# d2$pol <- droplevels(d2$pol)
# to double-check any changes we made
# cross_cases(d2, politics, pol)
# use the leveneTest() command from the car package to test homogeneity of variance (we do not want significance)
# 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(mindful~pol, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.6545 0.5198
## 3166
# worked!
# 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(2972))
# to drop multiple outliers, remove the # at the beginning of the line and use this code:
# d <- subset(d, row_id!=c(561) & row_id!=c(855) & row_id!=c(2208))
# 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(mindful ~ pol, data = d) #for one-way
# reg_model2 <- lm(pss ~ gender_rc*poc, data = d2) #for two-way
# Cook's distance
plot(reg_model, 4)
# Residuals vs Leverage
plot(reg_model, 5)
My cell sizes are unbalanced. A small sample size of total conservative participants serves as a less accurate representation, limiting the variable’s power and increasing the Type II error rate. Of the 3166 participants, most identify as liberal (1380), thus being the dominant population over the conservatives (697). The remaining 1100 participants are from the moderate and apolitical levels.I combined these into one level for robustness fo ananlysis. Though they do not pertain to the research question, it is essential to include more than two levels when conducting an ANOVA. Outliers in the moderate/apolitical level were not powerful enough to remove.
The results of Levene’s Test indicated that there was no significant difference in variances among the groups (F(2, 3166) = 0.6545, p = 0.5198).
aov_model <- aov_ez(data = d,
id = "row_id",
between = c("pol"),
dv = "mindful",
anova_table = list(es = "pes"))
## Warning: Missing values for 13 ID(s), which were removed before analysis:
## 168, 216, 743, 1141, 1310, 1679, 1939, 2247, 2265, 2293, ... [showing first 10 only]
## Below the first few rows (in wide format) of the removed cases with missing data.
## row_id pol .
## # 168 168 moderate/apolitical NA
## # 216 216 liberal NA
## # 743 743 <NA> NA
## # 1141 1141 liberal NA
## # 1310 1310 conservative NA
## # 1679 1679 conservative NA
## Contrasts set to contr.sum for the following variables: pol
# aov_model2 <- aov_ez(data = d2,
# id = "X",
# between = c("gender_rc","poc"),
# dv = "pss",
# anova_table = list(es = "pes"))
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: mindful
## Effect df MSE F pes p.value
## 1 pol 2, 3166 0.71 6.96 *** .004 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model, x = "pol")
A significant effect of political affiliation on mindfulness was observed (F(1, 3166) = 6.96, p = .004), indicating that individuals’ political affiliation had a statistically significant impact on their level of mindfulness.
emmeans(aov_model, specs="pol", adjust="sidak")
## pol emmean SE df lower.CL upper.CL
## conservative 3.75 0.0319 3166 3.68 3.83
## liberal 3.65 0.0227 3166 3.59 3.70
## moderate/apolitical 3.76 0.0254 3166 3.70 3.82
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model, specs="pol", adjust="sidak"))
## contrast estimate SE df t.ratio p.value
## conservative - liberal 0.10840 0.0392 3166 2.768 0.0156
## conservative - (moderate/apolitical) -0.00658 0.0408 3166 -0.161 0.9858
## liberal - (moderate/apolitical) -0.11498 0.0341 3166 -3.376 0.0021
##
## P value adjustment: tukey method for comparing a family of 3 estimates
The aim of this study was to examine the impact of political affiliation on reported mindfulness in participants of the EAMMi2 data set. To test my hypothesis that there would be a significant effect of political affiliation on mindfulness, I used a one-way ANOVA for three levels of my politics variable (conservative, liberal, moderate/apolitical). Results indicate that there is a significant difference in mindfulness (measured with a 15-item questionnaire) scores between individuals with conservative political affiliation (M = 3.75, SE = 0.0319) and those with liberal political affiliation (M = 3.65, SE = 0.0227). Moderate/apolitical was M = 3.76, SE = 0.0254. The contrast analysis revealed that the conservative group had higher levels of mindfulness compared to the liberal group (estimate = 0.10840, p = 0.0156), though this finding is not significant. therefore, the null hypothesis cannot be ruled out.
Limitations to this study include unbalanced data: my sample consisted of 3166 participants, with a larger proportion identifying as liberal (n = 1380, 43.6%) compared to conservatives (n = 697, 21.6%), while moderate/apolitical participants comprised the remaining population (n = 1100, 34.7%). This significantly reduces the power of our test and increases the chances of a Type II error because of the liberal sample size dominance. A non-significant Levene’s test (p = 0.5198) assumes homogeneity of variance in my data.
Findings show a significant effect of political affiliation on mindfulness, (F(1, 3166) = 6.96, p > .001), ηp2 = .004 (Cohen, 1988). Despite the trivial contribution of political affiliation on mindfulness, this factor should still be considered when making observations.
## [1] "conservative" "liberal" "moderate/apolitical"
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.