1 Loading Libraries

library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## To aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)
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
## The following object is masked from 'package:expss':
## 
##     recode
library(effsize) # for the cohen.d() command
## 
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
## 
##     cohen.d

2 Importing Data

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

3 Chi-square: State Your Hypothesis

There will be gender differences in participation across the racial/ethnic categories (in other words, men, women, and non-binary participants will be evenly distributed across the racial/ethnic categories).

4 Chi-square: 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  6 variables:
##  $ race    : chr  "white" "white" "white" "other" ...
##  $ gender  : chr  "f" "m" "m" "f" ...
##  $ support : num  6 6.75 5.17 5.58 6 ...
##  $ SocMedia: num  4.27 2.09 3.09 3.18 3.36 ...
##  $ swb     : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ belong  : num  3.4 3.4 3.6 3.6 3.2 3.4 3.5 3.2 3.5 2.7 ...
# we can see in the str() command that our categorical variables are being read as character or string variables
# to correct this, we'll use the as.factor() command
d$gender <- as.factor(d$gender)
d$race <- as.factor(d$race)

table(d$gender, useNA = "always")
## 
##    f    m   nb <NA> 
## 2332  792   54    4
table(d$race, useNA = "always")
## 
##       asian       black    hispanic multiracial  nativeamer       other 
##         210         249         286         293          12          97 
##       white        <NA> 
##        2026           9
cross_cases(d, gender,race)
 race 
 asian   black   hispanic   multiracial   nativeamer   other   white 
 gender 
   f  152 184 207 222 11 72 1480
   m  57 63 77 61 1 24 508
   nb  1 2 2 10 1 38
   #Total cases  210 249 286 293 12 97 2026

5 Chi-square: Check Your Assumptions

5.1 Chi-square Test Assumptions

  • Data should be frequencies or counts
  • Variables and levels should be independent
  • There are two variables
  • At least 5 or more participants per cell

5.2 Chi-square: Issues with My Data

Looking at my data, I have filled all assumptions except one. The requirement of having a minimum of five participants in all cells is not met. Specifically, I have less than five non-binary and other gender participants across every race. There is also only one reported male, native american participant.

Moving forward, I have made the decision to recount native american participants to the “other” category and will exclude non-binary cases from the study. Because the number of reported non-binary participants were so small, it posed limitations on further analysis. The idea of dropping participants is not ideal and is always a hard decision. Though it can push the notion of excluding underrepresented groups, it can be essential in advancing the study. This will be discussed as a limitation later on.

# we'll use the subset command to drop our non-binary participants
d <- subset(d, gender != "nb") #using the '!=' sign here tells R to filter out the indicated criteria
# once we've dropped a level from our factor, we need to use the droplevels() command to remove it, or it will still show as 0

table(d$gender, useNA = "always")
## 
##    f    m   nb <NA> 
## 2332  792    0    0
# we'll recode our race variable to combine our native american participants with our other participants
d$race2 <- d$race # create a new variable (race_rc2_ identical to current variable (race_rc)
d$race2[d$race == "nativeamer"] <- "other" # we will use some of our previous code to recode our Native American participants
table(d$race2, useNA = "always")
## 
##       asian       black    hispanic multiracial  nativeamer       other 
##         209         247         284         283           0         108 
##       white        <NA> 
##        1988           5
d$gender <- droplevels(d$gender) # once again, we need to use the droplevels() command
d$race2 <- droplevels(d$race2)

table(d$gender, useNA = "always")
## 
##    f    m <NA> 
## 2332  792    0
# since I made changes to my variables, I am going to re-run the cross_cases() command
cross_cases(d, gender, race2)
 race2 
 asian   black   hispanic   multiracial   other   white 
 gender 
   f  152 184 207 222 83 1480
   m  57 63 77 61 25 508
   #Total cases  209 247 284 283 108 1988

6 Chi-square: Run a Chi-square Test

# we use the chisq.test() command to run our chi-square test
# the only arguments we need to specify are the variables we're using for the chi-square test
# we are saving the output from our chi-square test to the chi_output object so we can view it again later
chi_output <- chisq.test(d$gender, d$race2)

7 Chi-square: View Test Output

# to view the results of our chi-square test, we just have to call up the output we saved
chi_output
## 
##  Pearson's Chi-squared test
## 
## data:  d$gender and d$race2
## X-squared = 3.3508, df = 5, p-value = 0.6461

8 Chi-square: View Standardized Residuals

# to view the standardized residuals, we use the $ operator to access the stdres element of the chi_output file that we created
chi_output$stdres
##         d$race2
## d$gender       asian       black    hispanic multiracial       other
##        f -0.65775743 -0.05472746 -0.71179673  1.54327508  0.53788796
##        m  0.65775743  0.05472746  0.71179673 -1.54327508 -0.53788796
##         d$race2
## d$gender       white
##        f -0.32782212
##        m  0.32782212

9 Chi-square: Write Up Results

I ran a Chi-square test of independence to test our hypothesis that a gender difference would exist in participation across the racial categories. Most of the requirements for running this analysis were met, including the use of frequencies, independence of variables, and having two variables. A limitation was met in Native American and non-binary individuals. Due to the low number of participants (under five for each category), Native Americans were recounted into the “other” category and non-binary participants were excluded from our sample. An analysis was made with the concluded sample below.

 race2 
 asian   black   hispanic   multiracial   other   white 
 gender 
   f  152 184 207 222 83 1480
   m  57 63 77 61 25 508

As predicted, we found a gender difference in participation across the racial/ethnic categories, χ2(5, N = 3124) = 3.35, p = .646.

10 T-test: State Your Hypothesis

I predict that women will report lower subjective well-being (swb) scores than men.

11 T-test: 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':    3124 obs. of  7 variables:
##  $ race    : Factor w/ 7 levels "asian","black",..: 7 7 7 6 7 7 7 7 4 4 ...
##  $ gender  : Factor w/ 2 levels "f","m": 1 2 2 1 2 1 1 1 1 1 ...
##  $ support : num  6 6.75 5.17 5.58 6 ...
##  $ SocMedia: num  4.27 2.09 3.09 3.18 3.36 ...
##  $ swb     : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ belong  : num  3.4 3.4 3.6 3.6 3.2 3.4 3.5 3.2 3.5 2.7 ...
##  $ race2   : Factor w/ 6 levels "asian","black",..: 6 6 6 5 6 6 6 6 4 4 ...
d$gender<- as.factor(d$gender)
table(d$gender, useNA = "always")
## 
##    f    m <NA> 
## 2332  792    0
# you can use the describe() command on an entire dataframe or just on a single variable
describe(d$swb)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 3120 4.49 1.32   4.67    4.54 1.48   1   7     6 -0.37    -0.44 0.02
# also use a histogram to examine your continuous variable
hist(d$swb)

# 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$swb, group=d$gender)
## 
##  Descriptive statistics by group 
## group: f
##    vars    n mean  sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2329 4.47 1.3    4.5    4.54 1.48   1   7     6 -0.38    -0.45 0.03
## ------------------------------------------------------------ 
## group: m
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 791 4.52 1.36   4.67    4.57 1.48   1   7     6 -0.34    -0.46 0.05
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$swb~d$gender)

12 T-test: Check Your Assumptions

12.1 T-test Assumptions

  • IV must have two levels
  • Data values must be independent (independent t-test only)
  • Data obtained via a random sample
  • Dependent variable must be normally distributed
  • Variances of the two groups are approximately equal

12.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
table(d$gender, useNA = "always")
## 
##    f    m <NA> 
## 2332  792    0
leveneTest(swb~d$gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    1  2.0455 0.1528
##       3118

As you can see, our data is very close to 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 changing the homogeneity of our variance, even if we already have a solution for any potential problems.

12.3 T-test: Issues with My Data

My independent variable (gender) used to have more than two levels (male, female and non-binary). In previous analysis, I found it necessary to drop non-binary participants. Because of this, the sample used for this test met the assumptions for a t-test, as the independent variable only accounted for male and female (two levels).

Through a Levene’s test, my data was shown to have a significant effect on the homogeneity of variance, as it exceeded the significance threshold level. Because of this, Welch’s t-test will be used to assess heterogeneity of variance.

13 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$swb~d$gender)

14 T-test: View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$swb by d$gender
## t = -0.86381, df = 1311.5, p-value = 0.3879
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
##  -0.15688631  0.06096318
## sample estimates:
## mean in group f mean in group m 
##        4.473952        4.521913

15 T-test: Calculate Cohen’s d

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

16 T-test: View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: -0.03638119 (negligible)
## 95 percent confidence interval:
##      lower      upper 
## -0.1170765  0.0443141

17 T-test: Write Up Results

My hypothesis that women will report lower subjective well-being (swb) scores than men was tested by running an independent t-test. Because t-tests must have independent variables with two levels to be conducted, the sample used within analysis excluded non-binary participants. When testing homogeneity of variance using Levene’s test, the report suggests that the data is reasonably consistent with the assumption of equal variances (p = 0.152). In order to further test the significance of this, I conducted Welch’s t-test to test the heterogeneity of variance.

Surprisingly, it was found that men (M = 4.52, sd = 1.36) reported lower subjective well-being (swb) scores than women (M = 4.47, sd = 1.3)– this is the opposite of what was predicted. t(1311.5) = -0.86 , p < .001 (see Figure 1). Cohen’s d was also conduced to measure effect size, and was found to be “negligible” (d estimate: -0.036). This suggests that the observed difference between men and women are unlikely to be meaningful (negligible; Cohen, 1988).

References

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