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 predicted that people with previous mental health issues likely to experience greater pandemic anxiety during the Covid-19 pandemic than individuals that did not have previous mental health issues.

[Remember to revise the above hypothesis in you HW assignment.]

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':    669 obs. of  7 variables:
##  $ X          : int  20 30 81 104 117 119 120 123 125 140 ...
##  $ age        : chr  "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
##  $ mhealth    : chr  "anxiety disorder" "none or NA" "none or NA" "none or NA" ...
##  $ rse        : num  1.6 3.9 3.5 3 2.5 3.4 2 2.9 3.2 3.6 ...
##  $ pas_covid  : num  4.56 3.33 2.33 3.67 2.67 ...
##  $ phq        : num  3.33 1 1.22 1.56 1.56 ...
##  $ isolation_c: num  3.5 1 1.25 2 2.75 1.25 1.5 2.25 2.5 1.5 ...
# 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)

table(d$mhealth, useNA = "always")
## 
##              anxiety disorder                       bipolar 
##                            75                             3 
##                    depression              eating disorders 
##                             9                            17 
##                    none or NA obsessive compulsive disorder 
##                           521                            15 
##                         other                          ptsd 
##                            17                            12 
##                          <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$pas_covid)
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 669 3.25 0.68   3.33    3.27 0.66   1   5     4 -0.3     0.18 0.03
# also use a histogram to visualize your continuous variable

hist(d$pas_covid)

# 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$pas_covid,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 75 3.42 0.71   3.56    3.46 0.66 1.22 4.78  3.56 -0.59     0.16 0.08
## ------------------------------------------------------------ 
## group: bipolar
##    vars n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 3 2.96 0.23   2.89    2.96 0.16 2.78 3.22  0.44 0.29    -2.33 0.13
## ------------------------------------------------------------ 
## group: depression
##    vars n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 9 3.36 0.96   3.44    3.36 0.49 1.67   5  3.33 -0.15    -0.77 0.32
## ------------------------------------------------------------ 
## group: eating disorders
##    vars  n mean  sd median trimmed  mad  min  max range  skew kurtosis   se
## X1    1 17  3.5 0.5   3.67    3.52 0.33 2.33 4.33     2 -0.49    -0.22 0.12
## ------------------------------------------------------------ 
## group: none or NA
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 521 3.17 0.66   3.22    3.19 0.66   1   5     4 -0.34     0.11 0.03
## ------------------------------------------------------------ 
## group: obsessive compulsive disorder
##    vars  n mean   sd median trimmed  mad min  max range skew kurtosis   se
## X1    1 15 3.81 0.49   3.78    3.82 0.49   3 4.56  1.56 0.07     -1.5 0.13
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad  min  max range  skew kurtosis   se
## X1    1 17 3.48 0.87   3.44    3.54 0.66 1.22 4.78  3.56 -0.63     0.37 0.21
## ------------------------------------------------------------ 
## group: ptsd
##    vars  n mean  sd median trimmed  mad min max range skew kurtosis  se
## X1    1 12 3.95 0.7   3.89    3.94 0.99   3   5     2 0.14    -1.59 0.2
# last, use a boxplot to examine your continuous and categorical variables together

boxplot(d$pas_covid~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 approximately equal
# If the IV has more than 2 levels, you must drop the additional levels so that you meet the first assumption of a t-test.

d <- subset(d,mhealth!="depression")

table(d$mhealth,useNA = "always") #verify that now there are no participants in the removed level
## 
##              anxiety disorder                       bipolar 
##                            75                             3 
##                    depression              eating disorders 
##                             0                            17 
##                    none or NA obsessive compulsive disorder 
##                           521                            15 
##                         other                          ptsd 
##                            17                            12 
##                          <NA> 
##                             0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$mhealth, useNA = "always") #verify that now the entire factor level is removed 
## 
##              anxiety disorder                       bipolar 
##                            75                             3 
##              eating disorders                    none or NA 
##                            17                           521 
## obsessive compulsive disorder                         other 
##                            15                            17 
##                          ptsd                          <NA> 
##                            12                             0
d <- subset(d,mhealth!="bipolar")

table(d$mhealth,useNA = "always") #verify that now there are no participants in the removed level
## 
##              anxiety disorder                       bipolar 
##                            75                             0 
##              eating disorders                    none or NA 
##                            17                           521 
## obsessive compulsive disorder                         other 
##                            15                            17 
##                          ptsd                          <NA> 
##                            12                             0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$mhealth, useNA = "always") #verify that now the entire factor level is removed 
## 
##              anxiety disorder              eating disorders 
##                            75                            17 
##                    none or NA obsessive compulsive disorder 
##                           521                            15 
##                         other                          ptsd 
##                            17                            12 
##                          <NA> 
##                             0
d <- subset(d,mhealth!="eating disorders")

table(d$mhealth,useNA = "always") #verify that now there are no participants in the removed level
## 
##              anxiety disorder              eating disorders 
##                            75                             0 
##                    none or NA obsessive compulsive disorder 
##                           521                            15 
##                         other                          ptsd 
##                            17                            12 
##                          <NA> 
##                             0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$mhealth, useNA = "always") #verify that now the entire factor level is removed 
## 
##              anxiety disorder                    none or NA 
##                            75                           521 
## obsessive compulsive disorder                         other 
##                            15                            17 
##                          ptsd                          <NA> 
##                            12                             0
d <- subset(d,mhealth!="obsessive compulsive disorder")

table(d$mhealth,useNA = "always") #verify that now there are no participants in the removed level
## 
##              anxiety disorder                    none or NA 
##                            75                           521 
## obsessive compulsive disorder                         other 
##                             0                            17 
##                          ptsd                          <NA> 
##                            12                             0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$mhealth, useNA = "always") #verify that now the entire factor level is removed 
## 
## anxiety disorder       none or NA            other             ptsd 
##               75              521               17               12 
##             <NA> 
##                0
d <- subset(d,mhealth!="ptsd")

table(d$mhealth,useNA = "always") #verify that now there are no participants in the removed level
## 
## anxiety disorder       none or NA            other             ptsd 
##               75              521               17                0 
##             <NA> 
##                0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$mhealth, useNA = "always") #verify that now the entire factor level is removed 
## 
## anxiety disorder       none or NA            other             <NA> 
##               75              521               17                0
d <- subset(d,mhealth!="other")

table(d$mhealth,useNA = "always") #verify that now there are no participants in the removed level
## 
## anxiety disorder       none or NA            other             <NA> 
##               75              521                0                0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$mhealth, useNA = "always") #verify that now the entire factor level is removed 
## 
## anxiety disorder       none or NA             <NA> 
##               75              521                0

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(d$pas_covid~d$mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  0.4585 0.4986
##       594

The test shows that the there is not significance between individuals who are had previous mental health issues and increase in symptoms of pandemic anxiety, which means we fail to reject the null hypothesis.

[Revise the above statement for you HW assignment.]

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 assumption about variance as Student’s t-test (the general default type of t-test). 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 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 test, I will drop the depression,bipolar, ocd, eating disorders, and other responses from my data 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 showed that through as Levene’s test, that the relationship between individuals with previous menatl health issues and the rise of symptoms of pandemic anxiety, was not significant. To accommodate for the variance differences, I will use Welch’s t-test instead of Student’s t-test in my analysis.

[Revise the above statements for you HW assignment.]

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$pas_covid~d$mhealth)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$pas_covid by d$mhealth
## t = 2.8684, df = 93.011, p-value = 0.005104
## alternative hypothesis: true difference in means between group anxiety disorder and group none or NA is not equal to 0
## 95 percent confidence interval:
##  0.07676487 0.42219013
## sample estimates:
## mean in group anxiety disorder       mean in group none or NA 
##                       3.422222                       3.172745

8 Calculate Cohen’s d

# once again, we use the same formula, y~x, to calculate cohen's d
d_output <- cohen.d(d$pas_covid~d$mhealth)

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.3765417 (small)
## 95 percent confidence interval:
##     lower     upper 
## 0.1330443 0.6200391

10 Write Up Results

To test our hypothesis that individuals with previous mental health issuses in sample would report significantly higher levels of pandemic anxiety than those who did not have previous mental health issues, we used an independent samples t-test. This required us to drop our individuals in the ocd, bipolar, depression, eating disoders,and others, 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 heterogeneity (p > 04986). This suggests that there is an increased chance of Type II error. To correct for this issue, we used Welch’s t-test, which does not assume equal variance. Our data met all other assumptions of a t-test.

As predicted, we found that individuals with prior mental health disorders reported significantly higher levels of pandemic anxiety than those who did not have prior mental health disorders (M =-0.75, SD = 0.68); t(2.864), p.0.4585 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -.75 (medium 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.