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

3 State Your Hypothesis

I predict that idividuals who do not identify as transgender will report higher self-esteem ratings, as measured by the Rosenberg Self-Esteem Inventory.

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':    1210 obs. of  7 variables:
##  $ X        : int  1 20 30 31 33 49 57 68 81 86 ...
##  $ trans    : chr  "no" "no" "no" "no" ...
##  $ ethnicity: chr  "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" ...
##  $ rse      : num  2.3 1.6 3.9 1.7 3.9 2.4 1.8 1.3 3.5 2.6 ...
##  $ gad      : num  1.86 3.86 1.14 2 1.43 ...
##  $ support  : num  2.5 2.17 5 2.5 3.67 ...
##  $ mfq_26   : num  4.2 3.35 4.65 4.65 4.5 4.3 5.25 5 4.7 4.05 ...
d$trans <- as.factor(d$trans)

table(d$gender_rc, useNA = "always")
## 
## <NA> 
##    0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$rse)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1210 2.63 0.72    2.7    2.64 0.74   1   4     3 -0.22    -0.72 0.02
# library(psychTools)
# installed.packages(psych)


# also use a histogram to examine your continuous variable
hist(d$rse)

# 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$rse, group=d$trans)
## 
##  Descriptive statistics by group 
## group: no
##    vars    n mean  sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1132 2.66 0.7    2.7    2.68 0.74   1   4     3 -0.25    -0.65 0.02
## ------------------------------------------------------------ 
## group: Prefer not to say
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 41 2.44 0.76    2.5    2.44 1.04   1 3.8   2.8 0.07    -0.93 0.12
## ------------------------------------------------------------ 
## group: yes
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 37 1.89 0.67    1.7    1.82 0.59 1.1 3.7   2.6 0.94     0.11 0.11
# last, use a boxplot to examine your continuous and categorical variables together dp,iv
boxplot(d$rse~d$trans)

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 I can check in R, while others are down to my research design. These assumptions are confirmed by my research design, so I 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 no, yes, or Prefer not to say for their transgender identity, so there are three levels, not two

This assumption was confirmed in the section above:

  • Dependent variable must be normally distributed – I 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, trans != "Prefer not to say")
d$trans <- droplevels(d$trans) # using droplevels() to drop the empty factor

5.2 Testing Homogeneity of Variance with Levene’s Test

I can test whether the variances of my 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 I am hoping for a non-significant result!

# use the leveneTest() command from the car package to test homogeneity of variance
# install.packages("car")
# library(car)
# 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$rse~d$trans, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    1  0.8571 0.3548
##       1167

As you can see, the data is not statistically significant. When running a t-test, I can account for heterogeneity in my 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.

5.3 Issues with My Data

My independent variable has more than two levels. To proceed with this analysis, I will drop the participants who answered “Prefer not to say” 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.

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

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$rse by d$trans
## t = 6.8315, df = 38.662, p-value = 3.816e-08
## alternative hypothesis: true difference in means between group no and group yes is not equal to 0
## 95 percent confidence interval:
##  0.5379348 0.9906489
## sample estimates:
##  mean in group no mean in group yes 
##          2.656184          1.891892

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 1.086064 (large)
## 95 percent confidence interval:
##     lower     upper 
## 0.7553352 1.4167928

10 Write Up Results

To test my hypothesis that individuals who do not identify as transgender report higher self-esteem ratings, I used an two-sample or independent t-test. This required me to drop those who answered “Prefer not to say” from my sample, as I am limited to a two-group comparison when using this test. I tested the homogeneity of variance with Levene’s test and found the homegeneity of variance to not be statisticaly significant (p = .35). My data met all other assumptions of a t-test.

As predicted, I found that those who do not identify as transgender (no = 2.66, SD = .7) reported significantly higher self esteem ratings than those who identify as transexual (yes = 1.89, SD = .67); t(38.66) = 6.83, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 1.09 (large effect; Cohen, 1988).

References

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