1 Loading Libraries

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

2 Importing Data

# 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_data_final.csv", header=T)

3 State Your Hypothesis

Gender is associated with differences in markers of adulthood. I expect that some markers may be more pronounced in women compared to men (after dropping non-binary participants), specifically seen in higher maturity levels among women.

4 Check Your Variables

# 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':    3182 obs. of  27 variables:
##  $ ResponseId      : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender          : chr  "f" "m" "m" "f" ...
##  $ race_rc         : chr  "white" "white" "white" "other" ...
##  $ age             : chr  "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
##  $ income          : chr  "1 low" "1 low" "rather not say" "rather not say" ...
##  $ edu             : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ sibling         : chr  "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
##  $ party_rc        : chr  "democrat" "independent" "apolitical" "apolitical" ...
##  $ disability      : chr  NA NA "psychiatric" NA ...
##  $ 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" ...
##  $ phys_sym        : chr  "high number of symptoms" "high number of symptoms" "high number of symptoms" "high number of symptoms" ...
##  $ pipwd           : num  NA NA 2.33 NA NA ...
##  $ moa_independence: num  3.67 3.67 3.5 3 3.83 ...
##  $ moa_role        : num  3 2.67 2.5 2 2.67 ...
##  $ moa_safety      : num  2.75 3.25 3 1.25 2.25 2.5 4 3.25 2.75 3.5 ...
##  $ moa_maturity    : num  3.67 3.33 3.67 3 3.67 ...
##  $ 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 ...
##  $ belong          : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ efficacy        : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ support         : num  6 6.75 5.17 5.58 6 ...
##  $ socmeduse       : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ usdream         : chr  "american dream is important and achievable for me" "american dream is important and achievable for me" "american dream is not important and maybe not achievable for me" "american dream is not important and maybe not achievable for me" ...
##  $ npi             : num  0.6923 0.1538 0.0769 0.0769 0.7692 ...
##  $ exploit         : num  2 3.67 4.33 1.67 4 ...
##  $ stress          : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
d$gender <- as.factor(d$gender)

table(d$gender, useNA = "always")
## 
##    f    m   nb <NA> 
## 2332  792   54    4
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$moa_maturity)
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 3146 3.59 0.43   3.67    3.65 0.49   1   4     3 -1.2     1.87 0.01
# also use a histogram to examine your continuous variable
hist(d$moa_maturity)

# 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$moa_maturity, group=d$gender)
## 
##  Descriptive statistics by group 
## group: f
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2303 3.61 0.42   3.67    3.67 0.49   1   4     3 -1.22     1.94 0.01
## ------------------------------------------------------------ 
## group: m
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 785 3.54 0.46   3.67     3.6 0.49 1.33   4  2.67 -1.16     1.65 0.02
## ------------------------------------------------------------ 
## group: nb
##    vars  n mean  sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 54  3.5 0.4   3.33    3.53 0.49 2.33   4  1.67 -0.5    -0.13 0.05
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$moa_maturity~d$gender)

# dependent/continuous variable first in above code for boxplot

5 Check Your Assumptions

5.1 T-test Assumptions

  • IV must be a categorical variable with two levels
  • Data values must be independent (in this case, that the collection of one variable isn’t based on the response to another variable)
  • Data obtained via a random sample
  • Dependent variable must be normally distributed
  • Variances of the two groups are approximately equal

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:

  • Data values must be independent (in this case, that the collection of one variable isn’t based on the response to another variable) – confirmed by the data report that details how the data was collected
  • Data obtained via a random sample – confirmed by the data report that details how the data was collected

This assumption is not met:

  • IV must be a categorical variable with two levels – participants could answer M, F, or NB for their gender, so there are three levels, not two

This assumption was confirmed in the section above:

  • Dependent variable must be normally distributed – we checked skew and kurtosis above. For our class, even if your variables don’t meet the assumption we’ll proceed anyway, just make a note of it.

So we only have one assumption to test:

  • Variances of the two groups are approximately equal – AKA homogeneity of variance. Tested below!
# 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

5.2 Testing Homogeneity of Variance with Levene’s Test

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(moa_maturity~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value   Pr(>F)   
## group    1  7.9336 0.004883 **
##       3086                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

As you can see, our data is 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.

5.3 Issues with My Data

My independent variable (gender) 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 potential issues regarding homogeneity of variance. The Levene’s test resulted in a value less than 0.05, which means that the test is statistically significant and the two groups (men and women) are significantly different.

6 Run a 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$moa_maturity~d$gender)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$moa_maturity by d$gender
## t = 3.6899, df = 1249.3, p-value = 0.000234
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
##  0.03242206 0.10604258
## sample estimates:
## mean in group f mean in group m 
##        3.609784        3.540552

8 Calculate Cohen’s d

# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$moa_maturity~d$gender)

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.1601786 (negligible)
## 95 percent confidence interval:
##      lower      upper 
## 0.07904457 0.24131254

10 Write Up Results

To test our hypothesis that women in our sample would report to have more markers of adulthood than men, specifically in terms of maturity, we used an two-sample or independent t-test. This required us to drop our non-binary and other 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 = .048). This suggests that there is an increased chance of Type I error. To correct for this possible issue, we use 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.61, SD = .42) reported higher markers of adulthood (within the level of maturity) than men (M = 3.54, SD = .46); t(1249.3) = 3.6899, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .16 (small effect; Cohen, 1988).

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.