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

# for the homework: 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

I predicted that those with depression compared to those who reported no mental health disorders, experienced more isolation during COVID.

(Note: The above hypothesis is for the lab. You will need to come up with your own hypothesis, specific to your chosen variables, for the homework.)

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':    1211 obs. of  8 variables:
##  $ sexual_orientation: chr  "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" ...
##  $ mhealth           : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ isolation         : num  2.25 3.5 1 2.5 1.75 2 1.25 1 3 1.25 ...
##  $ support           : num  2.5 2.17 5 2.5 3.67 ...
##  $ mfq_26            : num  4.2 3.35 4.65 4.65 4.5 4.3 5.25 4.45 5 4.7 ...
##  $ pswq_std          : num  2.309 0.851 -1.124 1.163 -0.342 ...
##  $ variable2         : chr  "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" ...
##  $ variable3         : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
d$mhealth <- as.factor(d$mhealth)

table(d$mhealth, useNA = "always")
## 
##              anxiety disorder                       bipolar 
##                           125                             6 
##                    depression              eating disorders 
##                            30                            28 
##                    none or NA obsessive compulsive disorder 
##                           936                            27 
##                         other                          ptsd 
##                            36                            23 
##                          <NA> 
##                             0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$isolation)
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 1211 2.15 0.84      2    2.12 1.11   1 3.5   2.5 0.17    -1.28 0.02
# also use a histogram to examine your continuous variable
hist(d$isolation)

# 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$isolation, 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 125 2.59 0.76   2.75    2.65 0.74   1 3.5   2.5 -0.44    -1.02 0.07
## ------------------------------------------------------------ 
## group: bipolar
##    vars n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 6 2.75 0.65   2.88    2.75 0.74 1.75 3.5  1.75 -0.34    -1.67 0.27
## ------------------------------------------------------------ 
## group: depression
##    vars  n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 30 2.32 0.9   2.25    2.32 1.48   1 3.5   2.5 0.03    -1.55 0.16
## ------------------------------------------------------------ 
## group: eating disorders
##    vars  n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 28 2.74 0.67      3    2.79 0.74 1.25 3.5  2.25 -0.61    -0.85 0.13
## ------------------------------------------------------------ 
## group: none or NA
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 936 2.02 0.82      2    1.97 1.11   1 3.5   2.5 0.37    -1.13 0.03
## ------------------------------------------------------------ 
## group: obsessive compulsive disorder
##    vars  n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 27 2.63 0.62    2.5    2.66 0.74 1.25 3.5  2.25 -0.3    -0.76 0.12
## ------------------------------------------------------------ 
## group: other
##    vars  n mean  sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 36 2.65 0.8      3     2.7 0.74 1.25 3.5  2.25 -0.48    -1.39 0.13
## ------------------------------------------------------------ 
## group: ptsd
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 23 2.51 0.78   2.75    2.57 0.74   1 3.5   2.5 -0.47    -0.96 0.16
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$isolation~d$mhealth)

5 Check Your Assumptions

5.1 T-test Assumptions

  • IV must be a categorical variable with two levels
  • Data values must be independent (in this case, that the collection of one variable isn’t based on the response to another variable)
  • Data obtained via a random sample
  • Dependent variable must be normally distributed
  • Variances of the two groups are approximately equal

Some of these we can check in R, while others are down to our research design. These assumptions are confirmed by our research design, so we don’t have to do anything now:

  • Data values must be independent (in this case, that the collection of one variable isn’t based on the response to another variable) – confirmed by the data report that details how the data was collected
  • Data obtained via a random sample – confirmed by the data report that details how the data was collected

This assumption is not met:

  • IV must be a categorical variable with two levels – participants could answer M, F, or NB for their gender, so there are three levels, not two

This assumption was confirmed in the section above:

  • Dependent variable must be normally distributed – we checked skew and kurtosis above. For our class, even if your variables don’t meet the assumption we’ll proceed anyway, just make a note of it.

So we only have one assumption to test:

  • Variances of the two groups are approximately equal – AKA homogeneity of variance. Tested below!
# subetting to drop the nb group so that our IV only has two levels
d <- subset (d, mhealth == c("depression","none or NA"))
## Warning in `==.default`(mhealth, c("depression", "none or NA")): longer object
## length is not a multiple of shorter object length
## Warning in is.na(e1) | is.na(e2): longer object length is not a multiple of
## shorter object length
d$mhealth <- droplevels(d$mhealth) # using droplevels() to drop the empty factor

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(isolation~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  2.6916 0.1015
##       488

As you can see, our data is not at all significant. When running a t-test, we can’t account for heterogeneity in our variance by using Welch’s t-test,since our p value was 0.1015.

5.3 Issues with My Data

My independent variable has more than two levels. To proceed with this analysis, I will drop all of the variables but those with depression or no mental health disorders at all.

My data also has some potential issues regarding homogeneity of variance. Although Levene’s test was not significant, it still was not even close to being significant.

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

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$isolation by d$mhealth
## t = 1.6776, df = 19.041, p-value = 0.1098
## alternative hypothesis: true difference in means between group depression and group none or NA is not equal to 0
## 95 percent confidence interval:
##  -0.09466498  0.85972252
## sample estimates:
## mean in group depression mean in group none or NA 
##                 2.394737                 2.012208

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.4607249 (small)
## 95 percent confidence interval:
##        lower        upper 
## 4.988211e-05 9.213998e-01

10 Write Up Results

To test our hypothesis that I predicted that those with depression compared to those who reported no mental health disorders, experienced more isolation during COVID and we tested this with an independent t-test. This required us to drop all of the other variables except those who reported depression and no mental health disorders so that we can conduct 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 = .1015). 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.

As predicted, we found that those with depression(M = 2.32, SD = .9) reported more isolation than those with no mental health disorders (M = 2.02, SD = .82); t(490) = 19.041, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .4607249 (small effect; Cohen, 1988).

References

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