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 married individuals will report significantly higher efficacy levels than divorced individuals, as measured by the General Self Efficacy Scale.

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':    3146 obs. of  6 variables:
##  $ race_rc  : chr  "white" "white" "white" "other" ...
##  $ marriage5: chr  "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
##  $ belong   : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ efficacy : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ socmeduse: int  47 23 34 35 37 13 37 43 37 29 ...
##  $ npi      : num  0.6923 0.1538 0.0769 0.0769 0.7692 ...
d$marriage5 <- as.factor(d$marriage5)

table(d$marriage5, useNA = "always")
## 
##             are currently divorced from one another 
##                                                 733 
##                are currently married to one another 
##                                                2121 
##       never married each other and are not together 
##                                                 245 
## never married each other but are currently together 
##                                                  47 
##                                                <NA> 
##                                                   0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$efficacy)
describe(d$efficacy)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 3146 3.13 0.45    3.1    3.13 0.44 1.1   4   2.9 -0.24     0.45 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$marriage5)
## 
##  Descriptive statistics by group 
## group: are currently divorced from one another
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 733 3.15 0.45    3.1    3.16 0.44 1.2   4   2.8 -0.27     0.29 0.02
## ------------------------------------------------------------ 
## group: are currently married to one another
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2121 3.12 0.44    3.1    3.12 0.44 1.1   4   2.9 -0.19     0.47 0.01
## ------------------------------------------------------------ 
## group: never married each other and are not together
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 245 3.13 0.49    3.1    3.15 0.44 1.2   4   2.8 -0.51     0.71 0.03
## ------------------------------------------------------------ 
## group: never married each other but are currently together
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 47 3.15 0.45    3.1    3.14 0.44 2.2   4   1.8 0.07    -0.71 0.07
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$efficacy~d$marriage5)

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!

# 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$efficacy~d$marriage5, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    3  2.1386 0.09327 .
##       3142                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

As you can see, our data is somewhat 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.

5.3 Issues with My Data

My independent variable has more than two levels. To proceed with this analysis, I will drop the never married each other and are not together and the never married each other but are currently together 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 some potential issues regarding homogeneity of variance. Although Levene’s test was not significant, it was close to the significance threshold. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test.

# once again, subsetting to drop the never married each other and are not together and never married each other but are currently together groups
d <- subset(d, marriage5 != "never married each other and are not together")
table(d$marriage5, useNA = "always")
## 
##             are currently divorced from one another 
##                                                 733 
##                are currently married to one another 
##                                                2121 
##       never married each other and are not together 
##                                                   0 
## never married each other but are currently together 
##                                                  47 
##                                                <NA> 
##                                                   0
d$marriage5 <- droplevels(d$marriage5) # using droplevels() to drop the empty factor
d <- subset(d, marriage5 != "never married each other but are currently together")
table(d$marriage5, useNA = "always")
## 
##             are currently divorced from one another 
##                                                 733 
##                are currently married to one another 
##                                                2121 
## never married each other but are currently together 
##                                                   0 
##                                                <NA> 
##                                                   0
d$marriage5 <- droplevels(d$marriage5) # using droplevels() to drop the empty factor

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$efficacy~d$marriage5)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$efficacy by d$marriage5
## t = 1.5589, df = 1242.6, p-value = 0.1193
## alternative hypothesis: true difference in means between group are currently divorced from one another and group are currently married to one another is not equal to 0
## 95 percent confidence interval:
##  -0.00774120  0.06763971
## sample estimates:
## mean in group are currently divorced from one another 
##                                              3.148431 
##    mean in group are currently married to one another 
##                                              3.118482

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.06771427 (negligible)
## 95 percent confidence interval:
##       lower       upper 
## -0.01631529  0.15174383

10 Write Up Results

To test our hypothesis that married individuals in our sample would report significantly higher levels of efficacy than divorced individuals, we used a two-sample or independent t-test. This required us to drop our participants who never married each other and are not together and our participants who never married each other but are currently together 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 = .093). This suggests that there is an increased chance of 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.

Contrary to our prediction, we found that individuals currently married to one another (M = 3.12, SD = .44) reported insignificantly lower efficacy than individuals currently divorced from one another (M = 3.15, SD = .45); t(1242.6) = 1.56, p = .119 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .068 (negligible effect; Cohen, 1988).

References

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