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)
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.
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/EAMMi2_final.csv", header=T)

3 Chi-square: State Your Hypothesis

There will not be a significant result between gender and racial ethnicity.

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_rc : chr  "white" "white" "white" "other" ...
##  $ gender  : chr  "f" "m" "m" "f" ...
##  $ stress  : num  3.3 3.6 3.3 3.2 3.5 2.9 3.2 3 2.9 3.2 ...
##  $ swb     : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ efficacy: num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ mindful : num  6.6 7.2 6.8 6.8 5.8 ...
# 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_rc <- as.factor(d$race_rc)

table(d$gender, useNA = "always")
## 
##    f    m   nb <NA> 
## 2332  792   54    4
table(d$race_rc, useNA = "always")
## 
##       asian       black    hispanic multiracial  nativeamer       other 
##         210         249         286         293          12          97 
##       white        <NA> 
##        2026           9
cross_cases(d, gender, race_rc)
 race_rc 
 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 Issues with My Data

My data meets all of the assumptions for a Chi-square test except for the assumption that there are at least 5 or more participants per cell. The number of non-binary or other gender participants is relatively small, and the total number of Native Americans is also small. In the Native American group, there is only 1 male participant present. To proceed with the Chi-square test, I will drop the non-binary participants and combine the Native American participants with the ‘other’ category. I will note this issue in my Method and Discussion section as a limitation of my study.

# 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
d$gender <- droplevels(d$gender)

table(d$gender, useNA = "always")
## 
##    f    m <NA> 
## 2332  792    0
# we'll recode our race variable to combine our native american participants with our other participants
d$race_rc2 <- d$race_rc # create a new variable (race_rc2_ identical to current variable (race_rc)
d$race_rc2[d$race_rc == "nativeamer"] <- "other" # we will use some of our previous code to recode our Native American participants
d$race_rc2 <- droplevels(d$race_rc2) # once again, we need to use the droplevels() command

table(d$race_rc2, useNA = "always")
## 
##       asian       black    hispanic multiracial       other       white 
##         209         247         284         283         108        1988 
##        <NA> 
##           5
# since I made changes to my variables, I am going to re-run the cross_cases() command
cross_cases(d, gender, race_rc2)
 race_rc2 
 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$race_rc2)

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$race_rc2
## 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$race_rc2
## 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$race_rc2
## d$gender       white
##        f -0.32782212
##        m  0.32782212

9 Chi-square: Write Up Results

To test my hypothesis that there would not be a significant result between gender and racial ethnicity, I ran a Chi-square test of independence. My two variables, gender and racial ethnicity, met 3 of the 4 criteria for running a Chi-square test of analysis. The assumption that was not met by either variable was having at least five participants per cell. In the gender variable, there were a small number of participants who identified as non-binary or other. To meet the criteria for a Chi-square test, I dropped that category and proceeded with only female and male. In the racial ethnicity variable, there were a small number of participants who were Native American, so I combined the Native American participants with the ‘Other’ category. Table 1 demonstrates the final sample for analysis:

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

As predicted, there was not a significant result between gender and racial ethnicity, χ2(5, N = 3119) = 3.35, p = .646.

10 T-test: State Your Hypothesis

I predict that men will report a higher level of efficacy than women.

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_rc : 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 ...
##  $ stress  : num  3.3 3.6 3.3 3.2 3.5 2.9 3.2 3 2.9 3.2 ...
##  $ swb     : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ efficacy: num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ mindful : num  6.6 7.2 6.8 6.8 5.8 ...
##  $ race_rc2: Factor w/ 6 levels "asian","black",..: 6 6 6 5 6 6 6 6 4 4 ...
table(d$gender, useNA = "always")
## 
##    f    m <NA> 
## 2332  792    0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$swb)
describe(d$efficacy)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 3119 3.13 0.45    3.1    3.13 0.44 1.2   4   2.8 -0.24     0.46 0.01
# also use a histogram to examine your continuous variable
hist(d$efficacy)

# 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$efficacy, group=d$gender)
## 
##  Descriptive statistics by group 
## group: f
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2327  3.1 0.45    3.1    3.11 0.44 1.2   4   2.8 -0.27     0.63 0.01
## ------------------------------------------------------------ 
## group: m
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 792 3.21 0.44    3.2    3.21 0.44 1.5   4   2.5 -0.16     -0.1 0.02
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$efficacy~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
leveneTest(efficacy~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    1   0.679   0.41
##       3117

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 Issues with My Data

My independent variable gender has more than two levels, so in order to continue the T-test analysis, I dropped the non-binary participants from my sample in a previous section. I will make note of this issue in my Method and Discussion section as a limitation of my study. Although Levene’s test was not significant, I will use Welch’s t-test instead of the Student’s t-test as a precaution for other issues.

13 T-test: 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$efficacy~d$gender)

14 T-test: View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$efficacy by d$gender
## t = -5.7119, df = 1382.5, p-value = 1.366e-08
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
##  -0.13921659 -0.06803751
## sample estimates:
## mean in group f mean in group m 
##        3.101676        3.205303

15 T-test: Calculate Cohen’s d

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

16 T-test: View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: -0.2335067 (small)
## 95 percent confidence interval:
##      lower      upper 
## -0.3143758 -0.1526377

17 T-test: Write Up Results

To test my hypothesis that men in my sample would report a significantly higher level of efficacy than women, I used Welch’s two-sample t-test. This required me to drop the non-binary category of my gender variable from my sample since this test uses a two-group comparison. I utilized Levene’s test and found signs of heterogeneity (p = 0.41). This suggests that there is not a significant difference between variances; therefore, my data met all of the assumptions of a t-test.

As hypothesized, I found that men (M = 3.21, SD = 0.44) reported significantly higher levels of efficacy than women (M = 3.1, SD = 0.45); t(1382.5) = -5.71, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d and had a value of -0.23 (small effect; Cohen, 1988).

References

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