# 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)
We predict that women will report significantly higher levels of mental flexibility 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': 1238 obs. of 7 variables:
## $ X : int 1 321 401 469 520 1390 1422 1849 2183 2247 ...
## $ gender : chr "female" "male" "female" "female" ...
## $ mhealth : chr "none or NA" "none or NA" "obsessive compulsive disorder" "depression" ...
## $ mfq_26 : num 4.2 4.3 4.7 4.6 2.7 2.95 2.95 2.4 3.65 4.7 ...
## $ support : num 2.5 2.5 3.83 4.67 2.83 ...
## $ gad : num 1.86 1 2.14 1.71 1.14 ...
## $ big5_ext: num 2 4.67 4.67 6 2 ...
# 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)
str(d)
## 'data.frame': 1238 obs. of 7 variables:
## $ X : int 1 321 401 469 520 1390 1422 1849 2183 2247 ...
## $ gender : Factor w/ 4 levels "female","I use another term",..: 1 3 1 1 1 3 1 1 1 1 ...
## $ mhealth : chr "none or NA" "none or NA" "obsessive compulsive disorder" "depression" ...
## $ mfq_26 : num 4.2 4.3 4.7 4.6 2.7 2.95 2.95 2.4 3.65 4.7 ...
## $ support : num 2.5 2.5 3.83 4.67 2.83 ...
## $ gad : num 1.86 1 2.14 1.71 1.14 ...
## $ big5_ext: num 2 4.67 4.67 6 2 ...
table(d$gender, useNA = "always")
##
## female I use another term male Prefer not to say
## 993 31 194 20
## <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$mfq_26)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1238 4.31 0.67 4.35 4.33 0.67 1.8 6 4.2 -0.29 0.07 0.02
# also use a histogram to visualize your continuous variable
hist(d$mfq_26)
# 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$mfq_26, group=d$gender)
##
## Descriptive statistics by group
## group: female
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 993 4.3 0.66 4.35 4.32 0.67 1.8 5.95 4.15 -0.33 0.2 0.02
## ------------------------------------------------------------
## group: I use another term
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 31 3.81 0.54 3.8 3.81 0.37 2.65 5.1 2.45 0.06 -0.04 0.1
## ------------------------------------------------------------
## group: male
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 194 4.46 0.67 4.5 4.48 0.74 2.45 6 3.55 -0.39 -0.08 0.05
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 20 4.03 0.75 4.03 3.97 0.7 2.95 5.8 2.85 0.65 -0.25 0.17
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$mfq_26~d$gender)
# If the IV has more than 2 levels, you must DROP any additional levels in order to meet the first assumption of a t-test.
## NOTE: This is a FOUR STEP process!
d <- subset(d, gender != "I use another term") # use subset() to remove all participants from the additional level
table(d$gender, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## female I use another term male Prefer not to say
## 993 0 194 20
## <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 Prefer not to say <NA>
## 993 194 20 0
d <- subset(d, gender != "Prefer not to say") # use subset() to remove all participants from the additional level
table(d$gender, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## female male Prefer not to say <NA>
## 993 194 0 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>
## 993 194 0
## Repeat ALL THE STEPS ABOVE if your IV has more levels that need to be DROPPED. Copy the 4 lines of code, and replace the level name in the subset() command.
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(mfq_26~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.2046 0.6511
## 1185
Levene’s test revealed that our data does not have significantly different variances between the two comparison groups, men and women, on their levels of mental flexibility.
When running a t-test, we can account for heterogeneity in our variance by using the Welch’s t-test, which does not have the same assumption about variance as the Student’s t-test (the general default type of t-test in statistics). 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 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 has more than two levels. To proceed with this analysis, I will drop the “I use another term” and “prefer not to say” participants from my sample. I will make a note to discuss this issue in my methods section write-up and in my discussion section as a limitation of my study.
My data also has an issue regarding homogeneity of variance, as Levene’s test was not significant. To accommodate for this heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test in my analysis.
[Revise the above statements for you HW assignment.]
# 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$mfq_26~d$gender) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$mfq_26 by d$gender
## t = -2.932, df = 270.53, p-value = 0.003656
## alternative hypothesis: true difference in means between group female and group male is not equal to 0
## 95 percent confidence interval:
## -0.25854632 -0.05081561
## sample estimates:
## mean in group female mean in group male
## 4.301762 4.456443
# once again, we use the same formula, y~x, to calculate cohen's d
# We **only** calculate effect size if the test is SIG!
d_output <- cohen.d(d$mfq_26~d$gender) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: -0.2331526 (small)
## 95 percent confidence interval:
## lower upper
## -0.38744593 -0.07885921
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)
To test our hypothesis that women in our sample would report significantly higher levels of mental flexibility than men, we used an independent samples t-test . This required us to drop our unidentified gender participants from our sample, as we are limited to a two-group comparison when using this test. We tested the homogeneity of variance with Levene’s test and found signs of heterogeneity (p < .001). This suggests that there is an increased chance of Type I error. To correct for this issue, we used Welch’s t-test, which does not assume homogeneity of variance. Our data met all other assumptions of an independent samples t-test.
We found that women (M = 4.30) reported significantly lower levels of mental flexibility than men (M = 4.46); t(-2.932) = 0.003, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.75 (medium effect; Cohen, 1988).
[Revise the above statements for you HW assignment.]
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.