# 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, as measured by the International Personality Item Pool (IPIP).
[Remember to revise the above hypothesis in you HW assignment.]
# you only need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 2820 obs. of 9 variables:
## $ ID : int 61617 61622 61624 61629 61630 61634 61636 61639 61640 61643 ...
## $ C1 : int 2 4 5 3 2 4 5 4 5 5 ...
## $ C2 : int 3 4 4 2 2 3 4 4 5 5 ...
## $ C3 : int 3 5 4 4 3 5 5 4 5 5 ...
## $ C4 : int 4 3 2 2 4 3 4 2 2 3 ...
## $ C5 : int 4 2 3 4 5 2 5 1 2 5 ...
## $ Consci_avg: num 3.2 3.6 3.6 3 3.2 3.4 4.6 3 3.8 4.6 ...
## $ gender : chr "m" "m" "m" "m" ...
## $ age : int 16 17 18 19 19 21 16 16 17 17 ...
# 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")
##
## 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
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$Consci_avg~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 != "nb")
table(d$gender, useNA = "always") #verify that now there are no participants in the removed 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
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
As you can see, the data has significantly different variances between the two comparison groups.
[Revise the above statement for you HW assignment.]
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 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.
My data also has some an issue regarding homogeneity of variance as Levene’s test was 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$Consci_avg~d$gender)
t_output
##
## Welch Two Sample t-test
##
## data: d$Consci_avg by d$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
d_output <- cohen.d(d$Consci_avg~d$gender)
d_output
##
## Cohen's d
##
## d estimate: -0.7506029 (medium)
## 95 percent confidence interval:
## lower upper
## -0.8319324 -0.6692734
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 a 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.051, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .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.