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 people without depression will report significantly higher levels of positive effects of COVID-19 than people with depression.

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':    716 obs. of  7 variables:
##  $ X          : int  520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
##  $ gender     : chr  "female" "male" "female" "male" ...
##  $ mhealth    : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ covid_pos  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ covid_neg  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ isolation_c: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ support    : num  2.83 3 4 4 3.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$mhealth <- as.factor(d$mhealth)

str(d)
## 'data.frame':    716 obs. of  7 variables:
##  $ X          : int  520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
##  $ gender     : chr  "female" "male" "female" "male" ...
##  $ mhealth    : Factor w/ 8 levels "anxiety disorder",..: 5 5 5 5 5 5 5 5 5 5 ...
##  $ covid_pos  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ covid_neg  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ isolation_c: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ support    : num  2.83 3 4 4 3.67 ...
table(d$mhealth, useNA = "always")
## 
##              anxiety disorder                       bipolar 
##                            79                             3 
##                    depression              eating disorders 
##                            13                            20 
##                    none or NA obsessive compulsive disorder 
##                           554                            15 
##                         other                          ptsd 
##                            19                            13 
##                          <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

(d$covid_pos)
##   [1]  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 12  0  0  0  0  1  7
##  [26]  0  0  0  0  0  0  0  0  0  0  0  0  5  0  0  0  0  0  0  0  5  0  0  9  0
##  [51]  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  8  3  0  0  4
##  [76]  9  0 12  0  0  0  0  0  4 12  9  3  0  0  0  0  0  0  0  0  0  0  0  0  0
## [101]  0 13  0  0  0  0  3  7  6  0  0  0 13 10  6  0  0  0  6  0  0  0  7  7  5
## [126] 10  0  0  0  0  0  0  0  0  0  0  0  3  8  5  0  0  0  0  0  0  0  0  0  6
## [151]  4  8  0  0  0  0  0  0  0  0 12 14  0  0  0  0  7  8  0  0  0  9  4  6  5
## [176]  0 11  5  4  0  0  0  0  0  0  0  0  0  7  0  0  0  0  0  0  6  0  2  6  5
## [201] 10  5  0  6  0  0  0  0  0  0  0  0  0  0  0  1  0  5  0  0  5  0  0  0 10
## [226]  8  3  0  0  0  0 11  5  0  0  5  4  0  0  0  0  0  0  0  0  0  0  0  0  1
## [251]  9  5  3  0  7  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  4  0  0  4
## [276] 10  0  0  0  0  9 15  6  4  0  0  0  0  3  9  1  3  6  4  3  0  0  0 13  0
## [301]  0  0  0  0  7  0  0  0  0  0  0  0  5 12 12  8  0  0  0  0  0  0  0  0  0
## [326]  6  4 12  0 14 11  0  0  4  7  8  0  0  2  0  4  0  2  0  5  8  4  0  0  0
## [351]  0  0  0  0  2  4  5  0  0  0  2  0  0  0  4  3  2  0  3  0  0 14  2  9  0
## [376]  7  0  0  0  0  8  7  4  0  0  0  0  0  0  9  3  0  0  0  0  0  3  0  0  3
## [401]  0  0  0  8  4  8  5  0  3  0  9  0  0 12  4  5  9  9  9 10  4  4  0  0  0
## [426]  0  0  6  3  0  0  0 11  2 11 10  0  0  0  7  7  0  0  7  7  0  0  0  0  7
## [451]  4  0  9  1  0  2  4 11  0  0  0 12 10  0  0  0  9  0  0 12  1  8  2  0  0
## [476]  2  8  4  0  0  0  0  0  3  5 10  0  2  4  0  0  9  3  0  0 15  4  8  4  0
## [501] 10  7  2  8  5  0  3  0  0  0  0  0  8 10  5  3  2  6  0  9  3  1  0  0  0
## [526] 10  3  6  6  0  0  0  0  0  3  1 11  7  0  0  0  0  0  0  7 10  9  0  0  0
## [551]  9  5  2  3  0  0  2  4  1  6  7  0  0  0  0  7  6  5 11  7  5  8  2  0  0
## [576]  0  0  0  5  1  4  2  4  8  2  5  0  1  5  1  0  0  5  4  0  9  0  0  0  0
## [601]  0  0  8  8  6  2  6  1  1  7  6  0  0  0  0  0  0  0  4  5  4  4  6  1  2
## [626]  3  0  8  6  3  5  0  1  7  2  0  9  1  3  0  9  0  7  3  5  0 11  0  4  0
## [651]  0  0  3  1  7  0  5  7  9  7  9  6  4  0  7  0  0 10  0  0  0  0  0  0  0
## [676]  6  0  0 12  9  1  3  5  0  0  6  5  0 10  2  9  6  3  0  4  0  0  0  7  0
## [701] 12  0  0  1  0  0  3  3  6  1  1  1  7  1  5  2
# also use a histogram to visualize your continuous variable

hist(d$covid_pos)

# 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$covid_pos, group=d$mhealth)
## 
##  Descriptive statistics by group 
## group: anxiety disorder
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 79 2.84 3.48      1    2.34 1.48   0  13    13 1.01    -0.03 0.39
## ------------------------------------------------------------ 
## group: bipolar
##    vars n mean sd median trimmed  mad min max range skew kurtosis   se
## X1    1 3    3  2      3       3 2.97   1   5     4    0    -2.33 1.15
## ------------------------------------------------------------ 
## group: depression
##    vars  n mean   sd median trimmed mad min max range skew kurtosis   se
## X1    1 13 3.23 4.75      0    2.55   0   0  14    14 1.04    -0.44 1.32
## ------------------------------------------------------------ 
## group: eating disorders
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 20  3.1 3.82      2    2.44 2.97   0  15    15 1.46     2.05 0.86
## ------------------------------------------------------------ 
## group: none or NA
##    vars   n mean   sd median trimmed mad min max range skew kurtosis   se
## X1    1 554 2.24 3.52      0    1.51   0   0  15    15 1.44     0.98 0.15
## ------------------------------------------------------------ 
## group: obsessive compulsive disorder
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 15 5.93 3.99      6    5.85 4.45   0  13    13 0.03    -1.27 1.03
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 19 3.53 3.72      2    3.41 2.97   0   9     9 0.29    -1.81 0.85
## ------------------------------------------------------------ 
## group: ptsd
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 13 3.46 3.71      2    3.27 2.97   0   9     9 0.39    -1.69 1.03
# lastly, use a boxplot to examine your chosen continuous and categorical variables together

boxplot(d$covid_pos~d$mhealth)

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 approx. 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, mhealth != "nb") # use subset() to remove all participants from the additional level

table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##              anxiety disorder                       bipolar 
##                            79                             3 
##                    depression              eating disorders 
##                            13                            20 
##                    none or NA obsessive compulsive disorder 
##                           554                            15 
##                         other                          ptsd 
##                            19                            13 
##                          <NA> 
##                             0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$health, useNA = "always") # verify that now the entire factor level is removed 
## 
## <NA> 
##    0
d <- subset(d, mhealth != "eating disorders") # use subset() to remove all participants from the additional level

table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##              anxiety disorder                       bipolar 
##                            79                             3 
##                    depression              eating disorders 
##                            13                             0 
##                    none or NA obsessive compulsive disorder 
##                           554                            15 
##                         other                          ptsd 
##                            19                            13 
##                          <NA> 
##                             0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$health, useNA = "always") # verify that now the entire factor level is removed 
## 
## <NA> 
##    0
d <- subset(d, mhealth != "anxiety disorder") # use subset() to remove all participants from the additional level

table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##              anxiety disorder                       bipolar 
##                             0                             3 
##                    depression                    none or NA 
##                            13                           554 
## obsessive compulsive disorder                         other 
##                            15                            19 
##                          ptsd                          <NA> 
##                            13                             0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$health, useNA = "always") # verify that now the entire factor level is removed 
## 
## <NA> 
##    0
d <- subset(d, mhealth != "bipolar") # use subset() to remove all participants from the additional level

table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##                       bipolar                    depression 
##                             0                            13 
##                    none or NA obsessive compulsive disorder 
##                           554                            15 
##                         other                          ptsd 
##                            19                            13 
##                          <NA> 
##                             0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$health, useNA = "always") # verify that now the entire factor level is removed 
## 
## <NA> 
##    0
d <- subset(d, mhealth != "obsessive compulsive disorder") # use subset() to remove all participants from the additional level

table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##                    depression                    none or NA 
##                            13                           554 
## obsessive compulsive disorder                         other 
##                             0                            19 
##                          ptsd                          <NA> 
##                            13                             0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$health, useNA = "always") # verify that now the entire factor level is removed 
## 
## <NA> 
##    0
d <- subset(d, mhealth != "ptsd") # use subset() to remove all participants from the additional level

table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
## depression none or NA      other       ptsd       <NA> 
##         13        554         19          0          0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$health, useNA = "always") # verify that now the entire factor level is removed 
## 
## <NA> 
##    0
d <- subset(d, mhealth != "other") # use subset() to remove all participants from the additional level

table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
## depression none or NA      other       <NA> 
##         13        554          0          0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$health, useNA = "always") # verify that now the entire factor level is removed 
## 
## <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(covid_pos~mhealth, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1   0.988 0.3207
##       565

Levene’s test revealed that our data does not have significanly different variances between the two comparison groups, depression and none, on their levels of positive effects of COVID-19.

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 participants with anxiety disorder, bipolar, easting disorders, obsessive compulsive disorder, ptsd, and other from my sample. I will make a note to discuss this issue in my methods section write-up and in my discussion 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$covid_pos~d$mhealth)  # t_output will now show in your Global Environment

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$covid_pos by d$mhealth
## t = 0.7478, df = 12.312, p-value = 0.4686
## alternative hypothesis: true difference in means between group depression and group none or NA is not equal to 0
## 95 percent confidence interval:
##  -1.887730  3.869124
## sample estimates:
## mean in group depression mean in group none or NA 
##                 3.230769                 2.240072

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$covid_pos~d$mhealth)  # d_output will now show in your Global Environment

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.2788941 (small)
## 95 percent confidence interval:
##      lower      upper 
## -0.2724637  0.8302518
## 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 people without depression in our sample would report significantly higher levels of positive effects of COVID-19 than people with depression, we used an independent samples t-test. This required us to drop our participants with anxiety disorder, bipolar, eating disorders, obsessive compulsive disorder, PTSD, and other 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 no significant evidence of heterogenity (p > .001). Our data met all other assumptions of an independent samples t-test.

Unlike we predicted, we found that women (M = 2.24, SD = 0.58) did not report significantly higher levels of positive effects of COVID-19 than people with depression (M = 3.23, SD =0.93 ); t(12.31) = 0.75, p >.c 001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.28 (small effect; Cohen, 1988).

[Revise the above statements for you HW assignment.]

References

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