# 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
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
I predict that people who are older will report significantly higher levels of anxiety than children, as measured by the Pandemic Anxiety Scale.
[Remember to revise the above hypothesis in you HW assignment.]
# you only need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 316 obs. of 7 variables:
## $ X : int 6291 6294 6312 6326 6331 6335 6348 6350 6357 6359 ...
## $ mhealth : chr "none or NA" "depression" "anxiety disorder" "none or NA" ...
## $ age : chr "1 under 18" "1 under 18" "1 under 18" "2 between 18 and 25" ...
## $ big5_ext : num 3 5.67 3.33 2.67 2.33 ...
## $ pas_covid: num 2.33 3.11 3.78 2.56 3.89 ...
## $ mfq_state: num 4 4 3.12 3.12 3.88 ...
## $ brs : num 4 2.5 2.33 2 2.83 ...
# 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$age <- as.factor(d$age)
table(d$age, useNA = "always")
##
## 1 under 18 2 between 18 and 25 4 between 36 and 45 5 over 45
## 263 30 9 14
## <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 316 3.35 0.65 3.44 3.36 0.66 1 5 4 -0.32 0.58 0.04
# 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$age)
##
## Descriptive statistics by group
## group: 1 under 18
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 263 3.35 0.66 3.44 3.37 0.66 1 5 4 -0.37 0.63 0.04
## ------------------------------------------------------------
## group: 2 between 18 and 25
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 30 3.43 0.52 3.33 3.44 0.49 2.33 4.44 2.11 0 -0.82 0.09
## ------------------------------------------------------------
## group: 4 between 36 and 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 9 3.1 0.91 3 3.1 0.66 1.78 5 3.22 0.68 -0.29 0.3
## ------------------------------------------------------------
## group: 5 over 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 14 3.36 0.65 3.5 3.37 0.58 2 4.56 2.56 -0.29 -0.47 0.17
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$pas_covid~d$age)
# 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, age != "2 between 18 and 25")
d <- subset(d, age != "3 between 26 and 35")
d <- subset(d, age != "4 between 36 and 45")
table(d$age, useNA = "always") #verify that now there are no participants in the removed level
##
## 1 under 18 2 between 18 and 25 4 between 36 and 45 5 over 45
## 263 0 0 14
## <NA>
## 0
d$age <- droplevels(d$age) # use droplevels() to drop the empty factor
table(d$age, useNA = "always") #verify that now the entire factor level is removed
##
## 1 under 18 5 over 45 <NA>
## 263 14 0
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(pas_covid~age, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.0044 0.9471
## 275
As you can see, the data has no significant different variances between the two comparison groups.
[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.
My independent variable has more than two levels. To proceed with this analysis, I will drop the 18-25 year olds, 26-35 year olds, and 36-45 year olds 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 does not have an issue regarding homogeneity of variance as Levene’s test was not significant. To accommodate for any heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test in my analysis.
[Revise the above statements for you HW assignment.]
# 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$age)
t_output
##
## Welch Two Sample t-test
##
## data: d$pas_covid by d$age
## t = -0.055699, df = 14.482, p-value = 0.9563
## alternative hypothesis: true difference in means between group 1 under 18 and group 5 over 45 is not equal to 0
## 95 percent confidence interval:
## -0.3886628 0.3689271
## sample estimates:
## mean in group 1 under 18 mean in group 5 over 45
## 3.347275 3.357143
# once again, we use the same formula, y~x, to calculate cohen's d
d_output <- cohen.d(d$pas_covid~d$age)
d_output
##
## Cohen's d
##
## d estimate: -0.01498664 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.5549481 0.5249749
To test our hypothesis that people that are older will show significantly higher levels of anxiety than children, as measured by the Pandemic Anxiety Scale, our sample would report significantly higher levels of anxiety in adults over 45, we used an independent samples t-test. This required us to drop our participants aged 18-25, 26-35, and 36-45 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 minimal signs of heterogeneity (p < 0.9563). This suggests that there is not an increased chance of Type I error. To correct for any leftover issue, we used Welch’s t-test, which does not assume homogeneity of variance. Our data met all other assumptions of a t-test.
The hypothesis was not predicted correctly, as we found that children aged 18 and below (M = 3.35, SD = 0.66) reported significantly higher levels of anxiety than adults aged 45 and above as measured by the Pandemic Anxiety Scale (M = 3.36, SD = 0.65); t(-0.055699) df = 14.482, p < 0.9563 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -0.01498664 (negligible 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.