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

3 State Your Hypothesis

I predict that there will be a significant difference in satisfaction with life scores between lower household income participants and higher household income participants, with higher household income participants reporting a significantly higher average satisfaction with life score. High household income is defined as those reporting over 99k dollars in income a year and lower household income is defined as those reporting less than 30k dollars in income a year. Satisfaction with life is measured with the satisfaction with life scale.

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':    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$income <- as.factor(d$income)

#makes table
table(d$income, useNA = "always")
## 
##          1 low       2 middle         3 high rather not say           <NA> 
##            876            879            534            852              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$income)
## 
##  Descriptive statistics by group 
## group: 1 low
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 876 4.24 1.32   4.33    4.28 1.48   1   7     6 -0.23    -0.48 0.04
## ------------------------------------------------------------ 
## group: 2 middle
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 879  4.6 1.32   4.83    4.68 1.24   1   7     6 -0.48    -0.36 0.04
## ------------------------------------------------------------ 
## group: 3 high
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 534 4.79 1.32      5    4.88 1.24   1   7     6 -0.61    -0.08 0.06
## ------------------------------------------------------------ 
## group: rather not say
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 852 4.39 1.28    4.5    4.43 1.48   1   7     6 -0.28    -0.55 0.04
# last, use a boxplot to examine your continuous and categorical variables together, DV then IV as arguments
boxplot(d$swb~d$income)

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
d <- subset(d, income == c("1 low", "3 high"))
## Warning in `==.default`(income, c("1 low", "3 high")): 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$income <- droplevels(d$income) # 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 (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~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  0.0622 0.8031
##       709

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.

5.3 Issues with My Data

My independent variable has more than two levels. To proceed with this analysis, I will drop the middle income group and rather not say income 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 was not close to significance threshold, indicating there are likely no issues with homogeneity of variance in the data. I will proceed to run a Welch’s t-test on the data regardless.

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$swb~d$income)

7 View Test Output

# this is where useful information for write-up comes from
t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$swb by d$income
## t = -4.7778, df = 547.98, p-value = 2.279e-06
## alternative hypothesis: true difference in means between group 1 low and group 3 high is not equal to 0
## 95 percent confidence interval:
##  -0.6841135 -0.2854818
## sample estimates:
##  mean in group 1 low mean in group 3 high 
##             4.234899             4.719697

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: -0.3717005 (small)
## 95 percent confidence interval:
##      lower      upper 
## -0.5253188 -0.2180822

10 Write Up Results

To test my hypothesis that those with higher household income in our sample would report significantly higher satisfaction with life scores than those with lower household income, we used an two-sample or independent t-test. This required me to drop middle income participants and rather not say income 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 signs of heterogeneity (p = .77). Our data met all other assumptions of a t-test.

As predicted, we found that higher household income participants (M = 4.79, SD = 1.32) reported significantly higher satisfaction wih life than lower household income participants (M = 4.24, SD 1.32); t(547.98) = -4.7778, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -.37 (small effect; Cohen, 1988).

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.