# 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/bfi_labdata_updated.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 levels of conscientiousness than men.
Note: Conscientiousness was measured using the International Personality Item Pool (IPIP) in this dataset.
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d$gender)
## chr [1:2820] "m" "m" "m" "m" "m" "m" "m" "m" "m" "m" "m" "m" "m" "m" "m" ...
# 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$gender)
## Factor w/ 3 levels "m","nb","w": 1 1 1 1 1 1 1 1 1 1 ...
table(d$gender, useNA = "always")
##
## m nb w <NA>
## 919 20 1881 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$Consci_avg)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2820 3.77 0.76 3.8 3.83 0.59 1 6 5 -0.84 1.3 0.01
# also use a histogram to visualize your continuous variable
hist(d$Consci_avg)
# 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$Consci_avg, group=d$gender)
##
## Descriptive statistics by group
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 919 3.41 0.93 3.6 3.47 0.89 1 5.8 4.8 -0.62 -0.22 0.03
## ------------------------------------------------------------
## group: nb
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 20 3.87 0.63 4 3.88 0.74 2.8 4.8 2 -0.11 -1.33 0.14
## ------------------------------------------------------------
## group: w
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1881 3.94 0.58 4 3.94 0.59 1.4 6 4.6 -0.05 0.12 0.01
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$Consci_avg~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 != "nb") # 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
##
## m nb w <NA>
## 919 0 1881 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
##
## m w <NA>
## 919 1881 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(Consci_avg ~ gender, data=d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 199.71 < 2.2e-16 ***
## 2798
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Levene’s test revealed that our data has unequal variances between the two comparison groups, men and women, on their levels of conscientiousness (p < .001).
My independent variable has 3 levels. To proceed with this analysis, I will drop the non-binary (nb) 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 significant (p < .001). To accommodate for this heterogeneity of variance, I will use Welch’s t-test instead of 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(Consci_avg ~ gender, data=d) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: Consci_avg by gender
## t = -16.051, df = 1283.5, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group m and group w is not equal to 0
## 95 percent confidence interval:
## -0.6012597 -0.4702936
## sample estimates:
## mean in group m mean in group w
## 3.407073 3.942850
# 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(Consci_avg ~ gender, data=d) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: -0.7506029 (medium)
## 95 percent confidence interval:
## lower upper
## -0.8319324 -0.6692734
## 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 conscientiousness than men, we used an independent samples t-test. This required us to drop our non-binary 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.
As predicted, we found that women (M = 3.94, SD = 0.58) reported significantly higher levels of conscientiousness than men (M = 3.41, SD = 0.93); t(1283.5) = -16.05, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.75 (medium effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.