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
# 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/mydata.csv", header=T)
I predict that those with a higher level of education will report higher mindfulness scores, 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': 3045 obs. of 6 variables:
## $ edu : chr "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
## $ marriage5 : chr "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ moa_role : num 3 2.67 2.5 2 2.67 ...
## $ mindful : num 2.4 1.8 2.2 2.2 3.2 ...
## $ efficacy : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
d$edu <- as.factor(d$edu)
table(d$edu, useNA = "always")
##
## 1 High school diploma or less, and NO COLLEGE
## 53
## 2 Currently in college
## 2460
## 3 Completed some college, but no longer in college
## 35
## 4 Complete 2 year College degree
## 174
## 5 Completed Bachelors Degree
## 135
## 6 Currently in graduate education
## 132
## 7 Completed some graduate degree
## 56
## <NA>
## 0
# you can use the describe() command on an entire dataframe (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 3045 3.71 0.84 3.73 3.71 0.79 1.13 6 4.87 -0.07 -0.14 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$edu)
##
## Descriptive statistics by group
## group: 1 High school diploma or less, and NO COLLEGE
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 53 3.6 0.95 3.6 3.65 0.99 1.33 5.4 4.07 -0.35 -0.4 0.13
## ------------------------------------------------------------
## group: 2 Currently in college
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2460 3.67 0.84 3.67 3.68 0.79 1.13 6 4.87 -0.06 -0.13 0.02
## ------------------------------------------------------------
## group: 3 Completed some college, but no longer in college
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 35 3.77 0.8 3.73 3.78 0.79 2 5.53 3.53 -0.04 -0.45 0.14
## ------------------------------------------------------------
## group: 4 Complete 2 year College degree
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 174 3.9 0.78 3.93 3.9 0.79 1.67 5.6 3.93 -0.11 -0.2 0.06
## ------------------------------------------------------------
## group: 5 Completed Bachelors Degree
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 135 3.95 0.87 4 3.94 0.89 1.8 6 4.2 0.07 -0.42 0.07
## ------------------------------------------------------------
## group: 6 Currently in graduate education
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 132 3.76 0.85 3.8 3.79 0.79 1.87 6 4.13 -0.15 -0.15 0.07
## ------------------------------------------------------------
## group: 7 Completed some graduate degree
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 56 3.82 0.92 3.77 3.82 0.84 2.07 5.67 3.6 -0.04 -0.55 0.12
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$mindful~d$edu)
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!
d <- subset(d, edu !="1 High school diploma or less, and NO COLLEGE")
d <- subset(d, edu !="3 Completed some college, but no longer in college")
d <- subset(d, edu !="4 Complete 2 year College degree")
d <- subset(d, edu !="5 Completed Bachelors Degree")
d <- subset(d, edu !="7 Completed some graduate degree")
table(d$edu, useNA = "always")
##
## 1 High school diploma or less, and NO COLLEGE
## 0
## 2 Currently in college
## 2460
## 3 Completed some college, but no longer in college
## 0
## 4 Complete 2 year College degree
## 0
## 5 Completed Bachelors Degree
## 0
## 6 Currently in graduate education
## 132
## 7 Completed some graduate degree
## 0
## <NA>
## 0
d$edu <- droplevels(d$edu) # use droplevels() to drop the empty factor
# 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~edu, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.0126 0.9107
## 2590
As you can see, the data is not close to significant. Still, 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 changing 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 participants who reported: (1) High school diploma or less, and NO COLLEGE, (2) Completed some college, but no longer in college, (3) Complete 2 year degree, (4) Completed Bachelors Degree, and (7) Completed some graduate degree 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.
The data also has some potential issues regarding homogeneity of variance. Levene’s test was not significant, nor was it close to the significance threshold. Still, 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$edu)
t_output
##
## Welch Two Sample t-test
##
## data: d$mindful by d$edu
## t = -1.1894, df = 144.93, p-value = 0.2362
## alternative hypothesis: true difference in means between group 2 Currently in college and group 6 Currently in graduate education is not equal to 0
## 95 percent confidence interval:
## -0.24054667 0.05979772
## sample estimates:
## mean in group 2 Currently in college
## 3.673767
## mean in group 6 Currently in graduate education
## 3.764141
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$mindful~d$edu)
d_output
##
## Cohen's d
##
## d estimate: -0.1078773 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.28309386 0.06733924
To test the hypothesis that participants with a higher level of education will report higher mindfulness scores, a two-sample or independent t-test was used. This required me to drop those who reported: (1) high school diploma or less, and NO COLLEGE, (2) Completed some college, but no longer in college, (3) Complete 2 year degree, (4) Completed Bachelors Degree, and (7) Completed some graduate degree, as we are 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 = .917). Regardless, to correct for the possibility of a Type I error, I used Welch’s t-test, which does not assume homogeneity of variance. The data met all other assumptions of a t-test.
As predicted, I found that those currently in graduate education (M = 3.76, SD = .85) reported higher mindfulness scores than those currently in college (M = 3.67, SD = .84); t(144.93) = -1.19, p = 0.236 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -.108 (negligible effect; Cohen, 1988).Though there was a difference in mindfulness scores between the education levels, the results were not statistically significant and the difference in mindfulness scores was quite small.
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.