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
# 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)
We predict that younger individuals will report significantly more anxiety than older individuals, as measured by general anxiety disorder (gad).
(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.) (need IV with 2 levels like gender and a continuous DV)
# 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': 912 obs. of 7 variables:
## $ X : int 20 30 31 33 57 58 81 104 113 117 ...
## $ age : chr "1 under 18" "1 under 18" "4 between 36 and 45" "4 between 36 and 45" ...
## $ mhealth : chr "anxiety disorder" "none or NA" "none or NA" "none or NA" ...
## $ pas_covid: num 4.56 3.33 4.22 3.22 4.56 ...
## $ phq : num 3.33 1 2.33 1.11 2.33 ...
## $ gad : num 3.86 1.14 2 1.43 2.86 ...
## $ swemws : num 2.29 4.29 3.29 4 3.29 ...
d$age <- as.factor(d$age)
table(d$age, useNA = "always")
##
## 1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45
## 585 54 6 87
## 5 over 45 <NA>
## 180 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$gad)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 912 1.98 0.9 1.71 1.88 0.85 1 4 3 0.75 -0.61 0.03
# also use a histogram to examine your continuous variable
hist(d$gad)
# 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$gad, 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 585 2.16 0.93 2 2.09 1.06 1 4 3 0.47 -1.04 0.04
## ------------------------------------------------------------
## group: 2 between 18 and 25
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 54 2.43 0.89 2.5 2.41 0.95 1 4 3 0.1 -1.05 0.12
## ------------------------------------------------------------
## group: 3 between 26 and 35
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 6 1.95 0.67 1.71 1.95 0.21 1.57 3.29 1.71 1.23 -0.3 0.27
## ------------------------------------------------------------
## group: 4 between 36 and 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 87 1.68 0.73 1.43 1.56 0.64 1 4 3 1.49 1.87 0.08
## ------------------------------------------------------------
## group: 5 over 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 180 1.42 0.49 1.29 1.33 0.42 1 3.29 2.29 1.54 2.11 0.04
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$gad~d$age)
#variable is DV and group is IV (gender) must be in order for boxplot
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:
This assumption is not met:
This assumption was confirmed in the section above:
So we only have one assumption to 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!
# subetting to drop the nb group so that our IV only has two levels
d <- subset(d, age == c("1 under 18", "5 over 45"))
d$age <- droplevels(d$age) # using droplevels() to drop the empty factor
# 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(d$gad~d$age, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 74.325 < 2.2e-16 ***
## 373
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
As you can see, our data is statistically significant. 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 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.
My independent variable has more than two levels. To proceed with this analysis, I will drop the non-binary participants 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. My Levene’s test was statistically significant. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s 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$gad~d$age)
t_output
##
## Welch Two Sample t-test
##
## data: d$gad by d$age
## t = 11.515, df = 310.93, p-value < 2.2e-16
## 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.7120941 1.0055966
## sample estimates:
## mean in group 1 under 18 mean in group 5 over 45
## 2.243461 1.384615
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$gad~d$age)
d_output
##
## Cohen's d
##
## d estimate: 1.003087 (large)
## 95 percent confidence interval:
## lower upper
## 0.7555174 1.2506571
To test our hypothesis that younger individuals in our sample would report significantly more anxiety than older individuals, we used an two-sample or independent t-test. This required us to drop our non-binary and other gender 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 some signs of heterogeneity (p = <.001). 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 younger individuals (M = 2.16, SD = .93) reported significantly higher anxiety than older individuals (M = 1.42, SD = .49); t(310.93) = 11.515, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .75 (small effect; Cohen, 1988).
look at describe by command for M and SD
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.