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

There will be a significant difference in self-esteem by people’s sleep hours, between those who are getting 5-6 hours of sleep and those who are getting 7-8 hours of sleep.

Note: Self esteem was measured using the Rosenberg Self-Esteem Inventory in this dataset.

[Remember to revise the above hypothesis in you HW assignment. Then delete line 45 and this reminder.]

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':    140 obs. of  7 variables:
##  $ X          : int  8120 8301 8308 8389 8536 8578 8660 8680 8738 8106 ...
##  $ employment : chr  "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" ...
##  $ sleep_hours: chr  "4 8-10 hours" "3 7-8 hours" "3 7-8 hours" "1 < 5 hours" ...
##  $ rse        : num  2.1 2.9 2.4 1.2 1.2 1.9 2.7 2.9 3.3 3.5 ...
##  $ pss        : num  3 4 3 5 3.5 2.75 3.5 2.5 2.5 3.75 ...
##  $ mfq_26     : num  3.55 4.5 3.4 4.6 3.8 3.6 4.45 4.5 4.75 5.1 ...
##  $ school_att : num  3.62 3.2 4 2.6 3.89 ...
# 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$sleep_hours <- as.factor(d$sleep_hours)

str(d)
## 'data.frame':    140 obs. of  7 variables:
##  $ X          : int  8120 8301 8308 8389 8536 8578 8660 8680 8738 8106 ...
##  $ employment : chr  "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" ...
##  $ sleep_hours: Factor w/ 5 levels "1 < 5 hours",..: 4 3 3 1 3 3 3 3 3 3 ...
##  $ rse        : num  2.1 2.9 2.4 1.2 1.2 1.9 2.7 2.9 3.3 3.5 ...
##  $ pss        : num  3 4 3 5 3.5 2.75 3.5 2.5 2.5 3.75 ...
##  $ mfq_26     : num  3.55 4.5 3.4 4.6 3.8 3.6 4.45 4.5 4.75 5.1 ...
##  $ school_att : num  3.62 3.2 4 2.6 3.89 ...
table(d$sleep_hours, useNA = "always")
## 
##  1 < 5 hours  2 5-6 hours  3 7-8 hours 4 8-10 hours 5 > 10 hours         <NA> 
##           16           44           54           23            3            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 140 2.35 0.65   2.35    2.33 0.67   1   4     3 0.24    -0.48 0.05
# 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$sleep_hours)
## 
##  Descriptive statistics by group 
## group: 1 < 5 hours
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 16 1.79 0.52   1.85    1.78 0.52   1 2.8   1.8 0.14    -0.98 0.13
## ------------------------------------------------------------ 
## group: 2 5-6 hours
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 44 2.18 0.51    2.1    2.17 0.59 1.2 3.5   2.3 0.26    -0.51 0.08
## ------------------------------------------------------------ 
## group: 3 7-8 hours
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 54 2.57 0.65   2.55    2.55 0.82 1.2 3.9   2.7 0.05    -0.79 0.09
## ------------------------------------------------------------ 
## group: 4 8-10 hours
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 23 2.55 0.67    2.7    2.54 0.59 1.2   4   2.8 0.06    -0.67 0.14
## ------------------------------------------------------------ 
## group: 5 > 10 hours
##    vars n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 3 2.47 0.67    2.3    2.47 0.59 1.9 3.2   1.3 0.23    -2.33 0.38
# lastly, use a boxplot to examine your chosen continuous and categorical variables together

boxplot(d$rse~d$sleep_hours)

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, sleep_hours != "5 > 10 hours") # use subset() to remove all participants from the additional level

table(d$sleep_hours, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##  1 < 5 hours  2 5-6 hours  3 7-8 hours 4 8-10 hours 5 > 10 hours         <NA> 
##           16           44           54           23            0            0
d$sleep_hours <- droplevels(d$sleep_hours) # use droplevels() to drop the empty factor

table(d$sleep_hours, useNA = "always") # verify that now the entire factor level is removed 
## 
##  1 < 5 hours  2 5-6 hours  3 7-8 hours 4 8-10 hours         <NA> 
##           16           44           54           23            0
d <- subset(d, sleep_hours != "4 8-10 hours") # use subset() to remove all participants from the additional level

table(d$sleep_hours, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##  1 < 5 hours  2 5-6 hours  3 7-8 hours 4 8-10 hours         <NA> 
##           16           44           54            0            0
d$sleep_hours <- droplevels(d$sleep_hours) # use droplevels() to drop the empty factor

table(d$sleep_hours, useNA = "always") # verify that now the entire factor level is removed 
## 
## 1 < 5 hours 2 5-6 hours 3 7-8 hours        <NA> 
##          16          44          54           0
d <- subset(d, sleep_hours != "1 < 5 hours") # use subset() to remove all participants from the additional level

table(d$sleep_hours, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
## 1 < 5 hours 2 5-6 hours 3 7-8 hours        <NA> 
##           0          44          54           0
d$sleep_hours <- droplevels(d$sleep_hours) # use droplevels() to drop the empty factor

table(d$sleep_hours, useNA = "always") # verify that now the entire factor level is removed 
## 
## 2 5-6 hours 3 7-8 hours        <NA> 
##          44          54           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~sleep_hours, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value  Pr(>F)  
## group  1   3.672 0.05831 .
##       96                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Levene’s test revealed that our data did not show significantly different variances between the two sleep groups (5–6 hours vs. 7–8 hours) on levels of self-esteem, F(1, 96) = 3.67, p = .058. This indicates that the assumption of homogeneity of variance was met.

[Revise the above statement for you HW assignment. Then delete this reminder.]

When running a t-test, we can account for potential differences in variance by using Welch’s t-test, which does not assume equal variances between groups. Because R uses Welch’s t-test by default, no additional adjustments were necessary. Even if variances had been unequal, Welch’s t-test would still provide a valid result. Levene’s test is included here to confirm that we checked this assumption as part of good statistical practice.

5.3 Issues with My Data

My independent variable, sleep hours, originally had more than two levels (< 5, 5–6, 7–8, 8–10, > 10 hours). To meet the assumptions of an independent-samples t-test, I restricted my analysis to participants who reported sleeping 5–6 hours or 7–8 hours per night. I will note this data-reduction step as a limitation in my Methods and Discussion sections.

Because Levene’s test was not significant, indicating equal variances, no further corrections were needed. However, I still used Welch’s t-test, which is robust to unequal variances and is the recommended default in R.

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$sleep_hours)  # 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$sleep_hours
## t = -3.3075, df = 95.807, p-value = 0.001326
## alternative hypothesis: true difference in means between group 2 5-6 hours and group 3 7-8 hours is not equal to 0
## 95 percent confidence interval:
##  -0.6194575 -0.1547849
## sample estimates:
## mean in group 2 5-6 hours mean in group 3 7-8 hours 
##                  2.179545                  2.566667

8 Calculate Cohen’s d - Effect Size

# once again, we use the same formula, y~x, to calculate cohen's d

# We **only** calculate effect size if the test is SIG!

d_output <- cohen.d(d$rse~d$sleep_hours)  # d_output will now show in your Global Environment

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: -0.6548262 (medium)
## 95 percent confidence interval:
##      lower      upper 
## -1.0685111 -0.2411413
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)

10 Write Up Results

To test our hypothesis that self-esteem would differ between individuals who sleep 5–6 hours and those who sleep 7–8 hours per night, we conducted an independent-samples t-test. Because this analysis requires only two comparison groups, we limited our sample to participants who reported sleeping within those two categories. We verified the assumption of equal variances with Levene’s test, which was not significant, F(1, 96) = 3.67, p = .058, indicating that the assumption of homogeneity of variance was met. Therefore, we proceeded with Welch’s t-test, which R uses by default and is robust to unequal variances. All other assumptions of the independent-samples t-test were satisfied.

Results showed a significant difference in self-esteem between the two sleep groups, t(95.81) = -3.31, p = .001. Participants who slept 5–6 hours per night reported lower self-esteem (M = 2.18, SD = 0.51) than those who slept 7–8 hours (M = 2.57, SD = 0.65). The effect size calculated using Cohen’s d, was 0.65, indicating (medium effect; Cohen, 1988). These results suggest that individuals who get slightly more sleep tend to report higher self-esteem levels.

[Revise the above statements for you HW assignment.]

References

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