# install any packages you have not previously used, then comment them back out.
# install.packages("car")
# install.packages("effsize")
library(psych) # for the describe() command
library(car) # for the leveneTest() command
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
library(effsize) # for the cohen.d() command
##
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
##
## cohen.d
d <- read.csv(file="Data/projectdata.csv", header=T)
# For the HW, you will import the project dataset you cleaned previously
# This will be the dataset you'll use for HWs throughout the rest of the semester
We predict that women will report significantly higher Eating Disorder Questionnaire scores than men.
# you only need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 1066 obs. of 7 variables:
## $ X : int 1 20 30 31 49 57 81 86 104 113 ...
## $ gender : chr "female" "male" "female" "female" ...
## $ exercise: num 0 2 3 1.5 2 2 2 2.5 0.5 1 ...
## $ big5_ext: num 2 1.67 6 5 5.67 ...
## $ big5_neu: num 6 6.67 4 4 5 ...
## $ rse : num 2.3 1.6 3.9 1.7 2.4 1.8 3.5 2.6 3 3.5 ...
## $ edeq12 : num 1.58 1.83 1 1.67 1.33 ...
# if the categorical variable you're using is showing as a "chr" (character), you must change it to be a factor -- using the next line of code (as.factor)
d$gender <- as.factor(d$gender)
table(d$gender, useNA = "always")
##
## female I use another term male Prefer not to say
## 851 32 164 19
## <NA>
## 0
## Checking the Continuous variable (DV)
# you can use the describe() command on an entire dataframe (d) or just on a single variable within your dataframe -- which we will do here
describe(d$edeq12)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1066 1.87 0.73 1.67 1.8 0.74 1 4 3 0.73 -0.48 0.02
# also use a histogram to visualize your continuous variable
hist(d$edeq12)
# use the describeBy() command to view the means and standard deviations by group
# it's very similar to the describe() command but splits the dataframe according to the 'group' variable
describeBy(d$edeq12, group=d$gender)
##
## Descriptive statistics by group
## group: female
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 851 1.91 0.73 1.75 1.84 0.74 1 4 3 0.69 -0.47 0.02
## ------------------------------------------------------------
## group: I use another term
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 32 2.23 0.81 2.33 2.22 1.24 1.08 3.75 2.67 0.02 -1.38 0.14
## ------------------------------------------------------------
## group: male
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 164 1.6 0.65 1.33 1.49 0.49 1 3.33 2.33 1.17 0.27 0.05
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 19 2.01 0.76 1.83 1.99 0.74 1 3.33 2.33 0.45 -1.32 0.17
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$edeq12~d$gender)
# If the IV has more than 2 levels, you must drop the additional levels so that you meet the first assumption of a t-test.
d <- subset(d, gender != "Prefer not to say")
d <- subset(d, gender != "I use another term")
table(d$gender, useNA = "always") #verify that now there are no participants in the removed level
##
## female I use another term male Prefer not to say
## 851 0 164 0
## <NA>
## 0
d$gender <- droplevels(d$gender) # use droplevels() to drop the empty factor
table(d$gender, useNA = "always") #verify that now the entire factor level is removed
##
## female male <NA>
## 851 164 0
We can test whether the variances of our two groups are equal using Levene’s test. The null hypothesis is that the variance between the two groups is equal, which is the result we want. So when running Levene’s test we’re hoping for a non-significant result!
# use the leveneTest() command from the car package to test homogeneity of variance
# it uses the same 'formula' setup that we'll use for our t-test: formula is y~x, where y is our DV and x is our IV
leveneTest(edeq12~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 8.1502 0.004393 **
## 1013
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
As you can see, the data has significantly different variances between the two comparison groups.
When running a t-test, we can account for heterogeneity in our variance by using Welch’s t-test, which does not have the same assumption about variance as Student’s t-test (the general default type of t-test). R defaults to using Welch’s t-test so this doesn’t require any changes on our part! Even if your data has no issues with homogeneity of variance, you’ll still use Welch’s t-test – it handles the potential issues around variance well and there are no real downsides. We’re just using Levene’s test here to get into the habit of checking the homogeneity of our variance, even if we already have the solution for any potential problems.
My independent variable, gender, initially included four levels: “female,” “male,” “I use another term,” and “Prefer not to say.” To proceed with this analysis, I excluded participants in the “I use another term” and “Prefer not to say” categories, leaving only “female” and “male” groups. I will note this exclusion as a limitation in the Methods section and discuss its implications in the Discussion.
Levene’s test indicated a significant variance difference between the gender groups for edeq12 ( 𝐹(1,1013)=8.15 F(1,1013)=8.15,𝑝=0.004 p=0.004). To address this heterogeneity of variance, I will use Welch’s t-test rather than Student’s t-test in my analysis.
# very simple! we use the same formula of y~x, where y is our DV and x is our IV
t_output <- t.test(d$edeq12~d$gender)
t_output
##
## Welch Two Sample t-test
##
## data: d$edeq12 by d$gender
## t = 5.4531, df = 248.12, p-value = 1.195e-07
## alternative hypothesis: true difference in means between group female and group male is not equal to 0
## 95 percent confidence interval:
## 0.1974218 0.4206651
## sample estimates:
## mean in group female mean in group male
## 1.908637 1.599593
# once again, we use the same formula, y~x, to calculate cohen's d
d_output <- cohen.d(d$edeq12~d$gender)
d_output
##
## Cohen's d
##
## d estimate: 0.4314239 (small)
## 95 percent confidence interval:
## lower upper
## 0.2630269 0.5998208
To test our hypothesis that women in our sample would report significantly higher edeq12 scores than men, we used an independent samples t-test, which required excluding non-binary gender participants from our sample for a two-group comparison. We checked homogeneity of variance with Levene’s test and found a significant difference in variances (p=0.004), indicating a potential for Type I error. To address this, we used Welch’s t-test, which does not assume equal variances. Our data met all other assumptions for conducting a t-test.
As predicted, we found that women (M = 1.91, SD = 0.73) reported significantly higher edeq12 scores than men (M = 1.60, SD = 0.65); 𝑡(248.12)=5.45 t(248.12)=5.45, 𝑝< 0.001 p<0.001. The effect size, calculated using Cohen’s d, was 0.43, indicating a small effect size.
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.