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
# 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/rc_cleaned_data.csv", header=T)
We predict that people who report less income will report significantly more stress than people that report a higher income, as measured by the generalized anxiety scale (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.)
# 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': 370 obs. of 8 variables:
## $ income : chr "3 high" "2 middle" "2 middle" "2 middle" ...
## $ mhealth : chr "none or NA" "none or NA" "none or NA" "anxiety disorder" ...
## $ big5_neu : num 6 4 2.67 5.33 3.67 ...
## $ pas_covid: num 3.22 4.22 3.22 4.56 3.67 ...
## $ gad : num 1.86 2 1.43 2.86 1.57 ...
## $ phq : num 1.33 2.33 1.11 2.33 1.56 ...
## $ variable2: chr "3 high" "2 middle" "2 middle" "2 middle" ...
## $ variable3: chr "none or NA" "none or NA" "none or NA" "anxiety disorder" ...
d$income <- as.factor(d$income)
table(d$income, useNA = "always")
##
## 1 low 2 middle 3 high prefer not to say
## 42 176 99 53
## <NA>
## 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$gad)
describe(d$gad)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 370 1.54 0.61 1.43 1.43 0.42 1 4 3 1.6 2.49 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$income)
##
## Descriptive statistics by group
## group: 1 low
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 42 1.86 0.7 1.71 1.79 0.74 1 3.29 2.29 0.66 -0.7 0.11
## ------------------------------------------------------------
## group: 2 middle
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 176 1.53 0.63 1.29 1.41 0.42 1 4 3 1.81 3.46 0.05
## ------------------------------------------------------------
## group: 3 high
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 99 1.47 0.53 1.29 1.38 0.42 1 3.29 2.29 1.44 1.55 0.05
## ------------------------------------------------------------
## group: prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 53 1.46 0.5 1.43 1.38 0.42 1 3.43 2.43 1.78 3.83 0.07
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$gad~d$income)
#Depndent variable goes first
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:
# subetting to drop the nb group so that our IV only has two levels
d <- subset(d, income == c("1 low", "3 high"))
d$income <- droplevels(d$income) # using droplevels() to drop the empty factor
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
# for levens test on the HW you want the P value to be above 0.05
leveneTest(gad~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.2567 0.6141
## 64
As you can see, our data is very close to 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 medium income and not answered 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.
# 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$income)
t_output
##
## Welch Two Sample t-test
##
## data: d$gad by d$income
## t = 2.0695, df = 35.02, p-value = 0.04594
## alternative hypothesis: true difference in means between group 1 low and group 3 high is not equal to 0
## 95 percent confidence interval:
## 0.007085409 0.735771734
## sample estimates:
## mean in group 1 low mean in group 3 high
## 1.904762 1.533333
# if P value is below 0.05 its signficant
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$gad~d$income)
d_output
##
## Cohen's d
##
## d estimate: 0.5730513 (medium)
## 95 percent confidence interval:
## lower upper
## 0.03578067 1.11032197
To test our hypothesis that people who report less income will report significantly more stress than people that report a higher income, as measured by the generalized anxiety scale (gad).t. This required us to drop our low and not answered 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 of heterogeneity (p = .694). This suggests that there is an increased chance of Type I error. Our data met all of our assumption.
As predicted, we found that low income (M = 1.86, SD = .7) reported significantly higher generalized anxiety than high income (M = 1.47, SD = .53); t(18.683) = 0.34675, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .128 (negligible effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.