1 Loading Libraries

# install any packages you have not previously used, then comment them back out.

#install.packages("car")
#install.packages("effsize")

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

d <- read.csv(file="data/projectdata.csv", header=T)

# For the HW, you will import the project dataset you cleaned previously
# This will be the dataset you'll use for HWs throughout the rest of the semester

3 State Your Hypothesis

We predict that there will be a significant difference in Rosenberg Self Esteem Inventory by people’s relationship status, between single never married and ina. relationship/married but living apart.

4 Check Your Variables

# you **only** need to check the variables you're using in the current analysis

## Checking the Categorical variable (IV)

str(d)
## 'data.frame':    251 obs. of  7 variables:
##  $ X                  : int  7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
##  $ relationship_status: chr  "Single, never married" "Single, never married" "Single, never married" "Single, never married" ...
##  $ exercise           : chr  "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" ...
##  $ big5_con           : num  4 3.33 6.33 5 6.67 ...
##  $ rse                : num  2.4 1.4 4 3.4 3.9 3.6 3.2 3.3 3 2.2 ...
##  $ pas_covid          : num  4.44 3.11 3.22 1.78 2.78 ...
##  $ brs                : num  2 3.83 3.83 4 4.67 ...
# if the categorical variable you're using is showing as a "chr" (character), you must change it to be a ** factor ** -- using the next line of code (as.factor)

 d$relationship_status<- as.factor(d$relationship_status)

str(d)
## 'data.frame':    251 obs. of  7 variables:
##  $ X                  : int  7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
##  $ relationship_status: Factor w/ 5 levels "In a relationship/married and cohabiting",..: 5 5 5 5 2 5 5 5 5 5 ...
##  $ exercise           : chr  "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" ...
##  $ big5_con           : num  4 3.33 6.33 5 6.67 ...
##  $ rse                : num  2.4 1.4 4 3.4 3.9 3.6 3.2 3.3 3 2.2 ...
##  $ pas_covid          : num  4.44 3.11 3.22 1.78 2.78 ...
##  $ brs                : num  2 3.83 3.83 4 4.67 ...
table(d$relationship_status, useNA = "always")
## 
##   In a relationship/married and cohabiting 
##                                          1 
## In a relationship/married but living apart 
##                                         33 
##                          Prefer not to say 
##                                         21 
##                Single, divorced or widowed 
##                                          1 
##                      Single, never married 
##                                        195 
##                                       <NA> 
##                                          0
## Checking the Continuous variable (DV)

# you can use the describe() command on an entire dataframe (d) or just on a single variable within your dataframe -- which we will do here

describe(d$rse)
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 251 2.23 0.67    2.1    2.21 0.74   1   4     3 0.35    -0.42 0.04
# also use a histogram to visualize your continuous variable

hist(d$rse)

# 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 1  2.4 NA    2.4     2.4   0 2.4 2.4     0   NA       NA NA
## ------------------------------------------------------------ 
## group: In a relationship/married but living apart
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 33 2.23 0.76    2.3     2.2 0.89   1 3.9   2.9 0.33    -0.76 0.13
## ------------------------------------------------------------ 
## group: Prefer not to say
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 21 2.11 0.58    2.2    2.12 0.59 1.1 3.2   2.1 -0.04    -1.06 0.13
## ------------------------------------------------------------ 
## group: Single, divorced or widowed
##    vars n mean sd median trimmed mad min max range skew kurtosis se
## X1    1 1  2.1 NA    2.1     2.1   0 2.1 2.1     0   NA       NA NA
## ------------------------------------------------------------ 
## group: Single, never married
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 195 2.24 0.66    2.1    2.22 0.74   1   4     3 0.36    -0.45 0.05
# lastly, use a boxplot to examine your chosen 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
# If the IV has more than 2 levels, you must DROP any additional levels in order to meet the first assumption of a t-test.

## NOTE: This is a FOUR STEP process!

 d<- subset(d, relationship_status  != "Single, divorced or widowed") # use subset() to remove all participants from the additional level

table(d$relationship_status, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##   In a relationship/married and cohabiting 
##                                          1 
## In a relationship/married but living apart 
##                                         33 
##                          Prefer not to say 
##                                         21 
##                Single, divorced or widowed 
##                                          0 
##                      Single, never married 
##                                        195 
##                                       <NA> 
##                                          0
 d$relationship_status<- droplevels(d$relationship_status) # use droplevels() to drop the empty factor

table(d$relationship_status, useNA = "always") # verify that now the entire factor level is removed 
## 
##   In a relationship/married and cohabiting 
##                                          1 
## In a relationship/married but living apart 
##                                         33 
##                          Prefer not to say 
##                                         21 
##                      Single, never married 
##                                        195 
##                                       <NA> 
##                                          0
 d<- subset(d, relationship_status  != "Prefer not to say") # use subset() to remove all participants from the additional level

table(d$relationship_status, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##   In a relationship/married and cohabiting 
##                                          1 
## In a relationship/married but living apart 
##                                         33 
##                          Prefer not to say 
##                                          0 
##                      Single, never married 
##                                        195 
##                                       <NA> 
##                                          0
 d$relationship_status<- droplevels(d$relationship_status) # use droplevels() to drop the empty factor

table(d$relationship_status, useNA = "always") # verify that now the entire factor level is removed 
## 
##   In a relationship/married and cohabiting 
##                                          1 
## In a relationship/married but living apart 
##                                         33 
##                      Single, never married 
##                                        195 
##                                       <NA> 
##                                          0
 d<- subset(d, relationship_status  != "In a relationship/married and cohabiting") # use subset() to remove all participants from the additional level

table(d$relationship_status, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##   In a relationship/married and cohabiting 
##                                          0 
## In a relationship/married but living apart 
##                                         33 
##                      Single, never married 
##                                        195 
##                                       <NA> 
##                                          0
 d$relationship_status<- droplevels(d$relationship_status) # use droplevels() to drop the empty factor

table(d$relationship_status, useNA = "always") # verify that now the entire factor level is removed 
## 
## In a relationship/married but living apart 
##                                         33 
##                      Single, never married 
##                                        195 
##                                       <NA> 
##                                          0
## Repeat ALL THE STEPS ABOVE if your IV has more levels that need to be DROPPED. Copy the 4 lines of code, and replace the level name in the subset() command.

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
# it 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  1.4121  0.236
##       226

Levene’s test revealed that our data does not have significantly different variances between the two comparison groups, single, never married and in a relationship/married but living apart, on their levels of Rosenberg Self Esteem Inventory.

When running a t-test, we can account for heterogeneity in our variance by using the Welch’s t-test, which does not have the same assumption about variance as the Student’s t-test (the general default type of t-test in statistics). 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 using Levene’s test here to get into the habit of checking the homogeneity of our variance, even if we already have the 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 “Prefer not to say”, “In a relationship/married and cohabiting”, and “Single, divorced or widowed” participants from my sample. I will make a note to discuss this issue in my methods section of my write-up and in my discussions section as a limitation of my study.

6 Run a T-test

# Very simple! we use the same formula of y~x, where y is our DV and x is our IV

t_output <- t.test(d$rse~d$relationship_status)  # t_output will now show in your Global Environment

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$rse by d$relationship_status
## t = -0.054882, df = 40.771, p-value = 0.9565
## alternative hypothesis: true difference in means between group In a relationship/married but living apart and group Single, never married is not equal to 0
## 95 percent confidence interval:
##  -0.2908025  0.2754179
## sample estimates:
## mean in group In a relationship/married but living apart 
##                                                 2.233333 
##                      mean in group Single, never married 
##                                                 2.241026

8 Write Up Results

To test our hypothesis that there will be a significant difference in Rosenberg Self Esteem Inventory by people’s relationship status, between single never married and in a relationship/married but living apart, we used an independent samples t-test. This required us to drop our prefer not to say, in a relationship/married and cohabiting, and single, divorced or widowed 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 signs of homogeneity (p > 0.05). Our data met all other assumptions of an independent samples t-test.

Contrary to our prediction, we found that single, never married people (M = 2.24, SD = 0.66) reported significantly higher levels of conscientiousness than men (M = 2.23, SD = 0.76); t(40.77) = -0.05, p > 0.05 (see Figure 1). The results were not significant.

References

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