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 single, never married will reflect higher score on the Rosenberg Self-Esteem Inventory (rse).

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':    1337 obs. of  6 variables:
##  $ sexual_orientation : chr  "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" ...
##  $ relationship_status: chr  "In a relationship/married and cohabiting" "Prefer not to say" "Prefer not to say" "In a relationship/married and cohabiting" ...
##  $ big5_open          : num  5.33 5.33 5 6 5 ...
##  $ pswq               : num  4.94 3.36 1.86 3.94 2.62 ...
##  $ mfq_26             : num  4.2 3.35 4.65 4.65 4.5 4.3 5.25 5 4.7 4.05 ...
##  $ rse                : num  2.3 1.6 3.9 1.7 3.9 2.4 1.8 1.3 3.5 2.6 ...
d$relationship_status <- as.factor(d$relationship_status)

table(d$relationship_status, useNA = "always")
## 
##   In a relationship/married and cohabiting 
##                                        282 
## In a relationship/married but living apart 
##                                        106 
##                          Prefer not to say 
##                                        104 
##                Single, divorced or widowed 
##                                         44 
##                      Single, never married 
##                                        801 
##                                       <NA> 
##                                          0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$rse)
describe(d$rse)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1337 2.61 0.72    2.7    2.62 0.74   1   4     3 -0.17    -0.72 0.02
# 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$relationship_status)
## 
##  Descriptive statistics by group 
## group: In a relationship/married and cohabiting
##    vars   n mean  sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 282  3.1 0.5    3.1    3.11 0.59 1.4   4   2.6 -0.39    -0.13 0.03
## ------------------------------------------------------------ 
## group: In a relationship/married but living apart
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 106 2.39 0.76    2.5    2.38 0.89   1   4     3 0.06    -0.97 0.07
## ------------------------------------------------------------ 
## group: Prefer not to say
##    vars   n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 104 2.45 0.7    2.4    2.44 0.82 1.1 3.9   2.8  0.1     -0.8 0.07
## ------------------------------------------------------------ 
## group: Single, divorced or widowed
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 44 2.94 0.54    2.9    2.94 0.52 1.2   4   2.8 -0.41     0.86 0.08
## ------------------------------------------------------------ 
## group: Single, never married
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 801 2.47 0.71    2.4    2.47 0.74   1   4     3 0.03     -0.7 0.03
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$rse~d$relationship_status)

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!

# once again, subetting to drop the nb group
d <- subset(d, !(relationship_status %in% c('Single, divorced or widowed', 'In a relationship/married but living apart', 'Prefer not to say')))
table(d$relationship_status, useNA = 'always')
## 
##   In a relationship/married and cohabiting 
##                                        282 
## In a relationship/married but living apart 
##                                          0 
##                          Prefer not to say 
##                                          0 
##                Single, divorced or widowed 
##                                          0 
##                      Single, never married 
##                                        801 
##                                       <NA> 
##                                          0
d$relationship_status <- droplevels(d$relationship_status) # using droplevels() to drop the empty factor

# 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(rse~relationship_status, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value    Pr(>F)    
## group    1  50.953 1.732e-12 ***
##       1081                      
## ---
## 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 changing 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 has more than two levels. To proceed with this analysis, I will drop the Single, divorced or widowed, In a relationship/married but living apart, and Prefer not to say 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 issues regarding homogeneity of variance, the Levene’s test was not significant. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test.

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$relationship_status)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$rse by d$relationship_status
## t = 15.959, df = 692.12, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group In a relationship/married and cohabiting and group Single, never married is not equal to 0
## 95 percent confidence interval:
##  0.5469026 0.7003469
## sample estimates:
## mean in group In a relationship/married and cohabiting 
##                                               3.095035 
##                    mean in group Single, never married 
##                                               2.471411

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.9421043 (large)
## 95 percent confidence interval:
##     lower     upper 
## 0.8005523 1.0836563

10 Write Up Results

To test our hypothesis that the single, never married participants in our sample would report significantly higher score in the Rosenberg Self-Esteem Inventory than the in a relationship/married and cohabiting participants, we used an two-sample or independent t-test. This required us to drop our single, divorced or widowed participants, and other participants with other relationship statuses 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 some signs of heterogeneity (p = 1.732e-12). This suggests that there is a 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 not predicted, we found that single, never married participants (M = 2.47, SD = .71) did not report higher score than in a relationship/married and cohabiting participants (M = 3.09, SD = .50); t(692.12) = 15.96, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .94 (large effect; Cohen, 1988).

References

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