1 Loading Libraries

library(psych) # for the describe() command
library(car) # for the leveneTest() command
## Warning: package 'car' was built under R version 4.3.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
library(effsize) # for the cohen.d() command
## Warning: package 'effsize' was built under R version 4.3.3
## 
## 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
# MAKE SURE TO HAVE THE PROJECT OPEN!!!!
d <- read.csv(file="Data/Covid_Data_Final.csv", header=T)

3 State Your Hypothesis

I predict that those with reported lung disease will report higher anxiety levels regarding the pandemic based on the PAS Covid scale (pas_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':    1113 obs. of  6 variables:
##  $ health   : chr  "something else or not applicable" "something else or not applicable" "something else or not applicable" "two conditions" ...
##  $ mhealth  : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ gad      : num  1.86 3.86 1.14 2 1.43 ...
##  $ phq      : num  1.33 3.33 1 2.33 1.11 ...
##  $ iou      : num  3.19 4 1.59 3.37 1.7 ...
##  $ pas_covid: num  3.22 4.56 3.33 4.22 3.22 ...
d$health <- as.factor(d$health)

table(d$health, useNA = "always")
## 
##                           cancer                         diabetes 
##                                2                                3 
##                    heart disease              high blood pressure 
##                                2                               22 
##                     lung disease   other chronic health condition 
##                               52                               27 
##                 other disability                prefer not to say 
##                               27                               22 
## something else or not applicable         three or more conditions 
##                              908                                9 
##                   two conditions                             <NA> 
##                               39                                0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$pas_covid)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1113 3.23 0.69   3.22    3.24 0.66   1   5     4 -0.18    -0.03 0.02
# also use a histogram to examine your continuous variable
hist(d$pas_covid)

# 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$pas_covid, group=d$health)
## 
##  Descriptive statistics by group 
## group: cancer
##    vars n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 2 2.89 0.16   2.89    2.89 0.16 2.78   3  0.22    0    -2.75 0.11
## ------------------------------------------------------------ 
## group: diabetes
##    vars n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 3 2.41 0.39   2.44    2.41 0.49   2 2.78  0.78 -0.09    -2.33 0.23
## ------------------------------------------------------------ 
## group: heart disease
##    vars n mean   sd median trimmed  mad  min  max range skew kurtosis  se
## X1    1 2 3.28 0.71   3.28    3.28 0.74 2.78 3.78     1    0    -2.75 0.5
## ------------------------------------------------------------ 
## group: high blood pressure
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 22 3.36 0.58   3.33    3.35 0.49 2.22 4.78  2.56 0.32    -0.08 0.12
## ------------------------------------------------------------ 
## group: lung disease
##    vars  n mean   sd median trimmed  mad  min  max range  skew kurtosis  se
## X1    1 52 3.21 0.69   3.28    3.25 0.66 1.22 4.56  3.33 -0.61      0.5 0.1
## ------------------------------------------------------------ 
## group: other chronic health condition
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 27 3.44 0.65   3.56    3.44 0.82 2.33 4.78  2.44 0.12     -0.9 0.12
## ------------------------------------------------------------ 
## group: other disability
##    vars  n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 27 3.58 0.91   3.67    3.62 0.82 1.67   5  3.33 -0.44    -0.87 0.18
## ------------------------------------------------------------ 
## group: prefer not to say
##    vars  n mean  sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 22 3.75 0.8   3.83    3.81 0.66 1.44   5  3.56 -0.9     0.96 0.17
## ------------------------------------------------------------ 
## group: something else or not applicable
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 908 3.18 0.66   3.22    3.19 0.66   1   5     4 -0.23    -0.07 0.02
## ------------------------------------------------------------ 
## group: three or more conditions
##    vars n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 9  4.1 0.63   4.11     4.1 0.66 3.11   5  1.89 0.04    -1.51 0.21
## ------------------------------------------------------------ 
## group: two conditions
##    vars  n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 39 3.65 0.82   3.78    3.72 0.82 1.22   5  3.78 -0.83     0.43 0.13
# last, use a boxplot to examine your continuous and categorical variables together
# Dependent/Continuous goes first while Independent/Factor goes second
boxplot(d$pas_covid~d$health)

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!

5.2 Issues with My Data

My independent variable has more than two levels. To proceed with this analysis, I will drop all but “something else or not applicable” and “lung disease”. 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 also has some potential issues regarding homogeneity of variance. Although Levene’s test was not significant, it was close to the significance threshold. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test.

# subetting to drop the nb group so that our IV only has two levels
# d <- subset(d, health != "cancer")
# d <- subset(d, health != "heart disease")
# d <- subset(d, health != "other disability")
# d <- subset(d, health != "two conditions")
# d <- subset(d, health != "diabetes")
# d <- subset(d, health != "high blood pressure")
# d <- subset(d, health != "prefer not to say")
# d <- subset(d, health != "three or more conditions")

d <- subset(d, health == c("lung disease","something else or not applicable"))
## Warning in `==.default`(health, c("lung disease", "something else or not
## applicable")): 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$health <- droplevels(d$health) # using droplevels() to drop the empty factor

5.3 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(pas_covid~health, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  1.0681 0.3019
##       478

As I can see, my data is not close to significant, but we cannot say until we run Welch’s t-test if this is truly significant or not. We are using Levene’s test to check the homogeneity of our variance despite having a solution just to get into the habit of doing this.

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

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$pas_covid by d$health
## t = 0.57675, df = 29.029, p-value = 0.5686
## alternative hypothesis: true difference in means between group lung disease and group something else or not applicable is not equal to 0
## 95 percent confidence interval:
##  -0.1690022  0.3017633
## sample estimates:
##                     mean in group lung disease 
##                                       3.252137 
## mean in group something else or not applicable 
##                                       3.185756

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.1014781 (negligible)
## 95 percent confidence interval:
##      lower      upper 
## -0.2948113  0.4977675

10 Write Up Results

To test our hypothesis that those with lung disease would have more pandemic related anxiety, we used an two-sample or independent t-test. This required us to drop many of our factors, 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 high signs of heterogeneity (p = .3019). This suggests that there is a high 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.

Against prediction, those with lung disease (M = 3.21, SD = .69) did not report significantly higher anxiety levels than those without lung disease (M = 3.18, SD = .66); t(480) = .57675, p < .5686 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .1014781 (negligible effect; Cohen, 1988).

References

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