#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
#install.packages("psych")
#install.packages("ggplot2")
#install.packages("expss")
#install.packages("car")
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 select rows from data: rows(mtcars, am==0)
##
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
## 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="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 is a significant difference in Patient Health Questionnaire (PHQ) scores across different self-reported health conditions (e.g., No Condition, Chronic Condition, Acute Condition).
# 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': 1165 obs. of 8 variables:
## $ X : int 1 321 401 469 520 1390 1849 2183 2247 2482 ...
## $ gender: chr "female" "male" "female" "female" ...
## $ health: chr "something else or not applicable" "something else or not applicable" "high blood pressure" "diabetes" ...
## $ pswq : num 4.94 1.71 2.44 2.5 2.71 ...
## $ pswq.1: num 4.94 1.71 2.44 2.5 2.71 ...
## $ phq : num 1.33 1.89 2.44 1.22 1.56 ...
## $ gad : num 1.86 1 2.14 1.71 1.14 ...
## $ 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$gender <- as.factor(d$gender)
d$health <- as.factor(d$health)
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
table(d$health)
##
## cancer diabetes
## 1 4
## heart disease high blood pressure
## 2 22
## lung disease other chronic health condition
## 57 28
## other disability prefer not to say
## 27 24
## something else or not applicable three or more conditions
## 950 9
## two conditions
## 41
d$health_group[d$health == "something else or not applicable"] <- "no_health_condition"
d$health_group[d$health == "high blood pressure"] <- "health_condition"
d$health_group[d$health == "diabetes"] <- "health_condition"
d$health_group[d$health == "cancer"] <- "health_condition"
d$health_group[d$health == "heart disease"] <- "health_condition"
d$health_group[d$health == "lung disease"] <- "health_condition"
d$health_group[d$health == "other chronic health condition"] <- "health_condition"
d$health_group[d$health == "other disability"] <- "health_condition"
d$health_group[d$health == "prefer not to say"] <- "health_condition"
d$health_group[d$health == "three or more conditions"] <- "health_condition"
d$health_group[d$health == "two conditions"] <- "health_condition"
table(d$health_group)
##
## health_condition no_health_condition
## 215 950
d$health_group <- as.factor(d$health_group)
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame': 1165 obs. of 9 variables:
## $ X : int 1 321 401 469 520 1390 1849 2183 2247 2482 ...
## $ gender : Factor w/ 4 levels "female","I use another term",..: 1 3 1 1 1 3 1 1 1 1 ...
## $ health : Factor w/ 11 levels "cancer","diabetes",..: 9 9 4 2 9 9 9 9 9 9 ...
## $ pswq : num 4.94 1.71 2.44 2.5 2.71 ...
## $ pswq.1 : num 4.94 1.71 2.44 2.5 2.71 ...
## $ phq : num 1.33 1.89 2.44 1.22 1.56 ...
## $ gad : num 1.86 1 2.14 1.71 1.14 ...
## $ row_id : Factor w/ 1165 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ health_group: Factor w/ 2 levels "health_condition",..: 2 2 1 1 2 2 2 2 2 2 ...
# check our DV skew and kurtosis
describe(d$phq)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1165 2.07 0.86 1.89 1.99 0.99 1 4 3 0.65 -0.65 0.03
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$phq, group = d$health_group )
##
## Descriptive statistics by group
## group: health_condition
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 215 2.26 0.91 2.11 2.2 0.99 1 4 3 0.42 -1 0.06
## ------------------------------------------------------------
## group: no_health_condition
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 950 2.03 0.84 1.78 1.94 0.82 1 4 3 0.7 -0.56 0.03
describeBy(d$phq, group = d$gender)
##
## Descriptive statistics by group
## group: female
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 950 2.07 0.85 1.89 2 0.82 1 4 3 0.65 -0.61 0.03
## ------------------------------------------------------------
## group: I use another term
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 29 3.14 0.78 3.22 3.2 0.99 1.11 4 2.89 -0.67 -0.5 0.14
## ------------------------------------------------------------
## group: male
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 168 1.8 0.77 1.56 1.71 0.66 1 4 3 0.92 -0.21 0.06
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 18 2.48 1.02 2.72 2.47 1.48 1 4 3 0.05 -1.55 0.24
# also use histograms to examine your continuous variable
hist(d$phq)
# and cross_cases() to examine your categorical variables' cell count
cross_cases(d,health_group,gender)
gender | ||||
---|---|---|---|---|
female | I use another term | male | Prefer not to say | |
health_group | ||||
health_condition | 169 | 11 | 31 | 4 |
no_health_condition | 781 | 18 | 137 | 14 |
#Total cases | 950 | 29 | 168 | 18 |
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# One-Way
table(d$health_group)
##
## health_condition no_health_condition
## 215 950
# 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
leveneTest(phq~health_group, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 3.6497 0.05632 .
## 1163
## ---
## 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(phq~health_group, data = d) #for One-Way
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
Our cell sizes are relatively balanced between the health group levels, so we do not have major concerns about power or increased Type II error rate.
Levene’s test for homogeneity of variance was marginally significant (p = .056) for our two-level health group variable with the One-Way ANOVA. We are ignoring this slight violation and continuing with the analysis anyway for this class.
We did not identify or remove any outliers for the One-Way ANOVA.
[UPDATE this section in your HW.]
# One-Way
aov_model <- aov_ez(data = d,
id = "X",
between = c("health_group"),
dv = "phq",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: health_group
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: phq
## Effect df MSE F pes p.value
## 1 health_group 1, 1163 0.73 12.78 *** .011 <.001
## ---
## 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 = "health_group")
# 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="health_group", adjust="sidak")
## health_group emmean SE df lower.CL upper.CL
## health_condition 2.26 0.0584 1163 2.13 2.39
## no_health_condition 2.03 0.0278 1163 1.96 2.09
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model, specs="health_group", adjust="sidak"))
## contrast estimate SE df t.ratio p.value
## health_condition - no_health_condition 0.231 0.0647 1163 3.574 0.0004
To test our hypothesis that there would be a significant difference in Patient Health Questionnaire (PHQ) scores based on self-reported health conditions (health condition vs. no health condition), we used a one-way ANOVA. Our data was unbalanced, with more participants reporting a health condition (n = [215]) compared to no health condition (n = [950]). This significantly reduces the power of our test and increases the chances of a Type II error. We did not identify and remove any outliers based on visual analysis. A marginally significant Levene’s test (p = .056) suggests a potential violation of the assumption of homogeneity of variance, indicating an increased chance of Type I error. We continued with our analysis for the purpose of this class.
We found a significant effect of health group, F(1, 1163) = 12.78, p < .001, ηp2 = .011 (small effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants with a health condition (M = 2.26, SE = 0.06) reported significantly higher PHQ scores than those without a health condition (M = 2.03, SE = 0.03); the mean difference between the groups was 0.231 points (p = .0004).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.