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

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

3 State Your Hypothesis

We predict that people who identify as transgender will land higher on the Pandemic Anxiety Scale (pas_covid) than those who do not.

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':    1009 obs. of  6 variables:
##  $ trans    : chr  "no" "no" "no" "no" ...
##  $ age      : chr  "1 under 18" "1 under 18" "4 between 36 and 45" "4 between 36 and 45" ...
##  $ big5_agr : num  4.33 6.67 4.67 6.67 5.33 ...
##  $ big5_ext : num  1.67 6 5 5.67 4 ...
##  $ rse      : num  1.6 3.9 1.7 2.4 1.8 3.5 3 3.5 2.5 3.4 ...
##  $ pas_covid: num  4.56 3.33 4.22 3.56 4.56 ...
d$trans <- as.factor(d$trans)

table(d$trans, useNA = "always")
## 
##                no Prefer not to say               yes              <NA> 
##               941                37                31                 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$pas_covid)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1009 3.24 0.68   3.22    3.26 0.66   1   5     4 -0.23     0.12 0.02
# also use a histogram to examine your continuous variable
hist(d$pas_covid)

# 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$pas_covid, group=d$trans)
## 
##  Descriptive statistics by group 
## group: no
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 941 3.23 0.67   3.22    3.25 0.66   1   5     4 -0.27      0.1 0.02
## ------------------------------------------------------------ 
## group: Prefer not to say
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 37 3.03 0.78   3.11    3.05 0.66 1.22 4.56  3.33 -0.3    -0.57 0.13
## ------------------------------------------------------------ 
## group: yes
##    vars  n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 31 3.69 0.73   3.44    3.65 0.66 2.67   5  2.33 0.41    -1.32 0.13
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$pas_covid~d$trans)

5 Check Your Assumptions

5.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

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!

d <- subset(d, trans != "Prefer not to say")
table(d$trans, useNA = "always")
## 
##                no Prefer not to say               yes              <NA> 
##               941                 0                31                 0
d$trans <- droplevels(d$trans)

# 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(d$pas_covid~d$trans, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  0.6803 0.4097
##       970

To run these tests we had to drop those who prefered not to answer for the gender category. As you can see, our data is not very close to significant. This is a result of not having a high number of transgender identifying individuals in the study.

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$pas_covid~d$trans)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$pas_covid by d$trans
## t = -3.448, df = 31.72, p-value = 0.001615
## alternative hypothesis: true difference in means between group no and group yes is not equal to 0
## 95 percent confidence interval:
##  -0.7253442 -0.1864822
## sample estimates:
##  mean in group no mean in group yes 
##          3.232259          3.688172

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: -0.6759587 (medium)
## 95 percent confidence interval:
##      lower      upper 
## -1.0354382 -0.3164792

10 Write Up Results

To test our hypothesis that trans individuals in our sample would report significantly more anxiety than CIS individuals, we used an two-sample or independent t-test. This required us to drop our “Prefer not to say” 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 no signs of heterogeneity (p = .41). Our data met all other assumptions of a t-test.

As predicted, we found that trans individuals (M = 3.69, SD = .95) reported sightly higher Anxiety than non-trans identifying (M = 3.23, SD = .89); t(31.72) = 3.45, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .68 (medium effect; Cohen, 1988).

References

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