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
# for the homework: import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
d <- read.csv(file="Data/eammi2_new_final.csv", header=T)
I predict that women will show greater mindfulness than men, as measured by the mindful attention awareness scale.
# 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': 2156 obs. of 6 variables:
## $ gender : chr "f" "m" "m" "f" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ idea : num 3.75 3.88 3.75 3.75 3.5 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ mindful : num 2.4 1.8 2.2 2.2 3.2 ...
## $ socmeduse: int 47 23 34 35 37 13 37 43 37 29 ...
d$gender <- as.factor(d$gender)
table(d$gender, useNA = "always")
##
## f m nb <NA>
## 1583 542 31 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$mindful)
describe(d$mindful)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2156 3.72 0.84 3.73 3.72 0.79 1.13 6 4.87 -0.04 -0.15 0.02
# also use a histogram to examine your continuous variable
hist(d$mindful)
# can 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$mindful, group=d$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1583 3.68 0.84 3.67 3.69 0.79 1.13 6 4.87 -0.03 -0.14 0.02
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 542 3.83 0.83 3.87 3.83 0.79 1.6 6 4.4 -0.04 -0.15 0.04
## ------------------------------------------------------------
## group: nb
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 31 3.43 0.83 3.47 3.45 0.89 1.87 4.8 2.93 -0.21 -1.09 0.15
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$mindful~d$gender)
# Check Your Assumptions
Some of these we can check in R, while others are down to our research design. These assumptions are confirmed by our research design, so we don’t have to do anything now:
This assumption is not met:
This assumption was confirmed in the section above:
So we only have one assumption to test:
# subetting to drop the nb group so that our IV only has two levels
d <- subset(d, gender != "nb")
d$gender <- droplevels(d$gender) # using droplevels() to drop the empty factor
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
# 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(mindful~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.4107 0.5217
## 2123
As you can see, my data is not significant. 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 assumptions as Student’s t-test (the default type of t-test) about variance. 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 a solution for any potential problems.
My independent variable has more than two levels. To proceed with this analysis, I will drop the non-binary participants from my sample. I will make a note to discuss this issue in my Method write-up and in my Discussion as a limitation of my study.
Levene’s test was not significant, but to accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test.
# very simple! we specify the dataframe alongside the variables instead of having a separate argument for the dataframe like we did for leveneTest()
t_output <- t.test(d$mindful~d$gender)
t_output
##
## Welch Two Sample t-test
##
## data: d$mindful by d$gender
## t = -3.4182, df = 951.08, p-value = 0.0006572
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
## -0.22269550 -0.06024913
## sample estimates:
## mean in group f mean in group m
## 3.684481 3.825953
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$mindful~d$gender)
d_output
##
## Cohen's d
##
## d estimate: -0.1687247 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.26645331 -0.07099614
To test my hypothesis that women in our sample would be significantly more mindful than men, I used an two-sample or independent t-test. This required me to drop my non-binary and other gender participants from my sample, as I am limited to a two-group comparison when using this test. I tested the homogeneity of variance with Levene’s test and found no signs of heterogeneity (p = .521). My data met all other assumptions of a t-test.
As predicted, I found that women (M = 3.68, SD = .84) were more mindful than men (M = 3.83, SD = .83); t(-3.4182) = 951.08, p < .001 (see Figure 1). (A higher score on the mindful attention awareness scale means less mindfulness.) The effect size was calculated using Cohen’s d, with a value of -0.1687 (negligible effect; Cohen, 1988). The p-value shows that there was a statistically significant difference between the groups, but Cohen’s d tells us this difference is negligible.
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.