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/homeworkdataarc.csv", header=T)

3 State Your Hypothesis

We predict that those who get more exercise will show lower scores on the Penn State Worry Questionnaire.

(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':    243 obs. of  6 variables:
##  $ income      : chr  "2 middle" "2 middle" "2 middle" "2 middle" ...
##  $ age         : chr  "4 between 36 and 45" "4 between 36 and 45" "4 between 36 and 45" "4 between 36 and 45" ...
##  $ exercise_cat: chr  "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" ...
##  $ pswq_std    : num  1.1627 -0.3425 0.0159 -0.1274 1.0193 ...
##  $ mfq_26      : num  4.65 4.5 4.3 5.25 4.25 4.4 4.75 5.3 4.75 3.6 ...
##  $ edeq12      : num  1.67 1.42 1.33 3.17 2.08 ...
d$exercise_cat <- as.factor(d$exercise_cat)

table(d$exercise_cat, useNA = "always")
## 
## 1 less than 1 hour        2 1-2 hours        3 2-5 hours        4 5-8 hours 
##                 39                137                 44                 11 
##     5 over 8 hours               <NA> 
##                 12                  0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$pswq_std)
##    vars   n  mean   sd median trimmed  mad   min  max range skew kurtosis   se
## X1    1 243 -0.08 1.04   -0.2   -0.12 1.28 -2.06 2.31  4.37 0.26    -0.89 0.07
# also use a histogram to examine your continuous variable
hist(d$pswq_std)

# 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$pswq_std, group=d$exercise_cat)
## 
##  Descriptive statistics by group 
## group: 1 less than 1 hour
##    vars  n mean   sd median trimmed  mad   min  max range skew kurtosis   se
## X1    1 39  0.2 1.09   0.02    0.16 1.49 -1.27 2.31  3.58 0.27    -1.28 0.17
## ------------------------------------------------------------ 
## group: 2 1-2 hours
##    vars   n  mean   sd median trimmed  mad   min  max range skew kurtosis   se
## X1    1 137 -0.07 1.02  -0.13   -0.09 1.17 -1.92 2.09  4.01 0.19    -1.01 0.09
## ------------------------------------------------------------ 
## group: 3 2-5 hours
##    vars  n  mean   sd median trimmed  mad   min  max range skew kurtosis   se
## X1    1 44 -0.33 1.03  -0.45   -0.36 1.33 -2.06 2.09  4.16 0.21    -0.79 0.16
## ------------------------------------------------------------ 
## group: 4 5-8 hours
##    vars  n mean   sd median trimmed  mad   min  max range skew kurtosis   se
## X1    1 11 0.08 1.15   0.02    0.08 1.28 -1.56 1.74   3.3 0.11     -1.5 0.35
## ------------------------------------------------------------ 
## group: 5 over 8 hours
##    vars  n  mean   sd median trimmed  mad   min  max range skew kurtosis   se
## X1    1 12 -0.33 0.91  -0.45   -0.45 0.69 -1.42 1.95  3.37 1.08     0.63 0.26
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$pswq_std~d$exercise_cat)

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

# if you need to drop multiple levels
d <- subset(d, exercise_cat == c("2 1-2 hours","4 5-8 hours"))
## Warning in `==.default`(exercise_cat, c("2 1-2 hours", "4 5-8 hours")): 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$exercise_cat <- droplevels(d$exercise_cat) # 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 (pswq_std~exercise_cat, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  1  0.2052 0.6519
##       73

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, which does not have the same assumptions as Student’s t-test (the default type of t-test) about variance. 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 a 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 all levels besides 1-2 hours and 5-8 hours of exercise from my 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 also has some potential issues regarding homogeneity of variance. Although Levene’s test was not significant. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test.

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$pswq_std~d$exercise_cat)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$pswq_std by d$exercise_cat
## t = 1.0005, df = 2.1388, p-value = 0.4166
## alternative hypothesis: true difference in means between group 2 1-2 hours and group 4 5-8 hours is not equal to 0
## 95 percent confidence interval:
##  -1.939317  3.213507
## sample estimates:
## mean in group 2 1-2 hours mean in group 4 5-8 hours 
##                 0.1512932                -0.4858015

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.647085 (medium)
## 95 percent confidence interval:
##     lower     upper 
## -0.532011  1.826181

10 Write Up Results

To test our hypothesis that an increase in exercise will show a decrease in the values reported on the Penn State Worry Questionnaire, we used an two-sample or independent t-test. This required us to drop all participants from the groups of exercising less than 1 hour, 2-5 hours and over 8 hour participants 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 signs of heterogeneity (p = .6519).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.

Counter to our prediction, we found that those who exercise 1-2 hours in a week had a mean PSWQ of (M = .15, SD = 1.02) while those that exercised 5-8 hours a week had a mean PSWQ of (M = -.485, SD = 1.15); t(288.7) = 1.0005, p < .4166 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .647 (medium effect; Cohen, 1988).

The one thing I want to note here is the problem with participants in my 5-8 hour group. As you can see in the boxplot, it may be plausible that given more data we would find a relationship. References

Parsons, S., Todorovic, A., Lim, M. C., Songco, A., & Fox, E. (2022). Data and protocol for the Oxford Achieving Resilience during COVID-19 (ARC) study. Journal of Open Psychology Data, 10(1).