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="HWData/updated_eammi2_data.csv", header=T)
I predict that there will be a significant difference in satisfaction with life scores between participants who believe the American Dream is important and achievable and participants who do not think it is important and maybe not achievable for them, with participants who think the American Dream is important and achievable for them reporting a significantly higher average satisfaction with life score.
# 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': 3141 obs. of 6 variables:
## $ income : chr "1 low" "1 low" "rather not say" "rather not say" ...
## $ usdream : chr "american dream is important and achievable for me" "american dream is important and achievable for me" "american dream is not important and maybe not achievable for me" "american dream is not important and maybe not achievable for me" ...
## $ idea : num 3.75 3.88 3.75 3.75 3.5 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ efficacy: num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
#turns income data from strings into factors
d$usdream <- as.factor(d$usdream)
#makes table
table(d$usdream, useNA = "always")
##
## american dream is important and achievable for me
## 1454
## american dream is important but maybe not achievable for me
## 341
## american dream is not important and maybe not achievable for me
## 585
## american dream is not important but is achievable for me
## 182
## not sure if american dream important
## 579
## <NA>
## 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable
describe(d$swb)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3141 4.48 1.32 4.67 4.53 1.48 1 7 6 -0.36 -0.45 0.02
# also use a histogram to examine your continuous variable
hist(d$swb)
# 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
# this is where we get standard deviation
describeBy(d$swb, group=d$usdream)
##
## Descriptive statistics by group
## group: american dream is important and achievable for me
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1454 4.84 1.21 5 4.9 1.24 1 7 6 -0.46 -0.32 0.03
## ------------------------------------------------------------
## group: american dream is important but maybe not achievable for me
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 341 3.91 1.35 4 3.92 1.48 1 7 6 -0.07 -0.65 0.07
## ------------------------------------------------------------
## group: american dream is not important and maybe not achievable for me
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 585 4.07 1.38 4.17 4.11 1.48 1 7 6 -0.25 -0.66 0.06
## ------------------------------------------------------------
## group: american dream is not important but is achievable for me
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 182 4.63 1.28 4.92 4.71 1.24 1 7 6 -0.56 -0.06 0.09
## ------------------------------------------------------------
## group: not sure if american dream important
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 579 4.27 1.25 4.33 4.3 1.24 1 7 6 -0.2 -0.48 0.05
# last, use a boxplot to examine your continuous and categorical variables together, DV then IV as arguments
boxplot(d$swb~d$usdream)
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, usdream == c("american dream is important and achievable for me", "american dream is not important and maybe not achievable for me"))
## Warning in `==.default`(usdream, c("american dream is important and achievable
## for me", : 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$usdream <- droplevels(d$usdream) # 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 (above 0.05)!
# 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(swb~usdream, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 5.6239 0.01791 *
## 997
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
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 “american dream is not important but is achievable for me” group, the “not sure if american dream important” group, and the “american dream is important but maybe not achievable for me” group 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.
Levene’s test resulted in significant result of p = 0.0179, indicating there is likely an issue with homogeneity of variance in the data. I will proceed to run a Welch’s t-test on the data.
# 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$swb~d$usdream)
# this is where useful information for write-up comes from
# t = 8.2406, df = 458.57, p-value = 1.813e-15
t_output
##
## Welch Two Sample t-test
##
## data: d$swb by d$usdream
## t = 8.2406, df = 458.57, p-value = 1.813e-15
## alternative hypothesis: true difference in means between group american dream is important and achievable for me and group american dream is not important and maybe not achievable for me is not equal to 0
## 95 percent confidence interval:
## 0.5873419 0.9551916
## sample estimates:
## mean in group american dream is important and achievable for me
## 4.854600
## mean in group american dream is not important and maybe not achievable for me
## 4.083333
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$swb~d$usdream)
d_output
##
## Cohen's d
##
## d estimate: 0.6104282 (medium)
## 95 percent confidence interval:
## lower upper
## 0.4693223 0.7515342
To test my hypothesis that participants who believe the American Dream is important and achievable will have significantly higher satisfaction with life scores than participants who do not think the American Dream is important and maybe not achievable for them, we used an two-sample or independent t-test. This required me to drop participants who didn’t know if the American Dream was important, participants who thought the American Dream was important but maybe not achievable, and participants who did not think the American Dream was important but was achievable, 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 signs of heterogeneity (p = .0179). Our data met all other assumptions of a t-test.
As predicted, we found that participants who believed the American Dream was important and achievable (M = 4.85, SD = 1.21) reported significantly higher satisfaction wih life than participants who believed the American Dream is not important and maybe not achievable (M = 4.08, SD 1.38); t(458.57) = 8.2406, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .61 (medium effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.