1 Loading Libraries

library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## Use magrittr pipe '%>%' to chain several operations:
##              mtcars %>%
##                  let(mpg_hp = mpg/hp) %>%
##                  take(mean(mpg_hp), by = am)
## 
## 
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
##  To return to the console output, use 'expss_output_default()'.
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
## The following object is masked from 'package:expss':
## 
##     recode
library(effsize) # for the cohen.d() command
## 
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
## 
##     cohen.d

2 Importing Data

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

3 Chi-square: State Your Hypothesis

There will be no education differences across the income categories (In other words, participants from each education level will be evenly distributed across the income variables.)

(Note: This hypothesis is predicting a non-significant result. It’s a bit backwards from how we usually do things, where a significant results supports the findings, but it makes sense for the current variables.)

4 Chi-square: 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':    3143 obs. of  7 variables:
##  $ ResponseId: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ edu_rc    : chr  "Currently in college" "Completed Bachelors Degree" "Currently in college" "Currently in college" ...
##  $ income_rc : chr  "20,000 - 39,999" "20,000 - 39,999" "Rather not say" "Rather not say" ...
##  $ stress    : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ swb       : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ support   : num  6 6.75 5.17 5.58 6 ...
##  $ belong    : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
# we can see in the str() command that our categorical variables are being read as character or string variables
# to correct this, we'll use the as.factor() command
d$edu_rc <- as.factor(d$edu_rc)
d$income_rc <- as.factor(d$income_rc)

table(d$edu_rc, useNA = "always")
## 
##                   Complete 2 year College degree 
##                                              179 
##                       Completed Bachelors Degree 
##                                              134 
## Completed some college, but no longer in college 
##                                               34 
##                   Completed some graduate degree 
##                                               60 
##                             Currently in college 
##                                             2544 
##                  Currently in graduate education 
##                                              134 
##      High school diploma or less, and NO COLLEGE 
##                                               58 
##                                             <NA> 
##                                                0
table(d$income_rc, useNA = "always")
## 
##   100,000 - 199,999     20,000 - 39,999 200,000 - 1 million     40,000 - 59,999 
##                 388                 360                 139                 344 
##     60,000 - 79,999     80,000 - 99,999      Over 1 million      Rather not say 
##                 298                 236                   7                 854 
##        Under 20,000                <NA> 
##                 517                   0
cross_cases(d, edu_rc, income_rc)
 income_rc 
 100,000 - 199,999   20,000 - 39,999   200,000 - 1 million   40,000 - 59,999   60,000 - 79,999   80,000 - 99,999   Over 1 million   Rather not say   Under 20,000 
 edu_rc 
   Complete 2 year College degree  19 27 5 17 20 7 37 47
   Completed Bachelors Degree  11 28 4 18 11 7 1 27 27
   Completed some college, but no longer in college  2 7 9 4 1 4 7
   Completed some graduate degree  8 9 15 4 4 11 9
   Currently in college  337 249 126 269 246 211 6 718 382
   Currently in graduate education  5 31 3 12 7 6 36 34
   High school diploma or less, and NO COLLEGE  6 9 1 4 6 21 11
   #Total cases  388 360 139 344 298 236 7 854 517

5 Chi-square: Check Your Assumptions

5.1 Chi-square Test Assumptions

  • Data should be frequencies or counts
  • Variables and levels should be independent
  • There are two variables
  • At least 5 or more participants per cell

5.2 Issues with My Data

While my data meets the first three assumptions, I don’t have at least 5 participants in all cells. The number of participants who completed some college, but no longer in college and participants who completed some graduate degree is pretty small, and for some of income levels it is less than five. There is only 1 participant with a bachelors degree making over 1 million dollars.

To proceed with this analysis, I will drop the participants who make over 1 million dollars and add the completed some graduate degree participants to the ‘Completed Bachelors Degree’ category. Dropping participants is always a difficult choice, but it’s a necessary compromise for my analysis. I will make a note to discuss this issue in my Method write-up and in my Discussion as a limitation of my study.

# we'll use the subset command to drop our non-binary participants
d <- subset(d, income_rc != "Over 1 million") #using the '!=' sign here tells R to filter out the indicated criteria
# once we've dropped a level from our factor, we need to use the droplevels() command to remove it, or it will still show as 0
d$income_rc <- droplevels(d$income_rc)

table(d$income_rc, useNA = "always")
## 
##   100,000 - 199,999     20,000 - 39,999 200,000 - 1 million     40,000 - 59,999 
##                 388                 360                 139                 344 
##     60,000 - 79,999     80,000 - 99,999      Rather not say        Under 20,000 
##                 298                 236                 854                 517 
##                <NA> 
##                   0
# we'll recode our race variable to combine our native american participants with our other participants
d$edu_rc2 <- d$edu_rc # create a new variable (race_rc2_ identical to current variable (race_rc)
d$edu_rc2[d$edu_rc == "Completed some graduate degree"] <- "Completed Bachelors Degree" # we will use some of our previous code to recode our Completed some graduate degree participants
table(d$edu_rc2, useNA = "always")
## 
##                   Complete 2 year College degree 
##                                              179 
##                       Completed Bachelors Degree 
##                                              193 
## Completed some college, but no longer in college 
##                                               34 
##                   Completed some graduate degree 
##                                                0 
##                             Currently in college 
##                                             2538 
##                  Currently in graduate education 
##                                              134 
##      High school diploma or less, and NO COLLEGE 
##                                               58 
##                                             <NA> 
##                                                0
d$edu_rc2 <- droplevels(d$edu_rc2) # once again, we need to use the droplevels() command

table(d$edu_rc2, useNA = "always")
## 
##                   Complete 2 year College degree 
##                                              179 
##                       Completed Bachelors Degree 
##                                              193 
## Completed some college, but no longer in college 
##                                               34 
##                             Currently in college 
##                                             2538 
##                  Currently in graduate education 
##                                              134 
##      High school diploma or less, and NO COLLEGE 
##                                               58 
##                                             <NA> 
##                                                0
# since I made changes to my variables, I am going to re-run the cross_cases() command
cross_cases(d, income_rc, edu_rc2)
 edu_rc2 
 Complete 2 year College degree   Completed Bachelors Degree   Completed some college, but no longer in college   Currently in college   Currently in graduate education   High school diploma or less, and NO COLLEGE 
 income_rc 
   100,000 - 199,999  19 19 2 337 5 6
   20,000 - 39,999  27 37 7 249 31 9
   200,000 - 1 million  5 4 126 3 1
   40,000 - 59,999  17 33 9 269 12 4
   60,000 - 79,999  20 15 4 246 7 6
   80,000 - 99,999  7 11 1 211 6
   Rather not say  37 38 4 718 36 21
   Under 20,000  47 36 7 382 34 11
   #Total cases  179 193 34 2538 134 58

6 Chi-square: Run a Chi-square Test

# we use the chisq.test() command to run our chi-square test
# the only arguments we need to specify are the variables we're using for the chi-square test
# we are saving the output from our chi-square test to the chi_output object so we can view it again later
chi_output <- chisq.test(d$income_rc , d$edu_rc2)
## Warning in chisq.test(d$income_rc, d$edu_rc2): Chi-squared approximation may be
## incorrect

7 Chi-square: View Test Output

# to view the results of our chi-square test, we just have to call up the output we saved
chi_output
## 
##  Pearson's Chi-squared test
## 
## data:  d$income_rc and d$edu_rc2
## X-squared = 125.73, df = 35, p-value = 3.7e-12

8 Chi-square: View Standardized Residuals

# to view the standardized residuals, we use the $ operator to access the stdres element of the chi_output file that we created
chi_output$stdres
##                      d$edu_rc2
## d$income_rc           Complete 2 year College degree Completed Bachelors Degree
##   100,000 - 199,999                      -0.73559838                -1.10098511
##   20,000 - 39,999                         1.55780712                 3.46012353
##   200,000 - 1 million                    -1.09728668                -1.64430700
##   40,000 - 59,999                        -0.64906655                 2.81257684
##   60,000 - 79,999                         0.78493149                -0.84627689
##   80,000 - 99,999                        -1.88801614                -0.99265907
##   Rather not say                         -2.03094414                -2.43000026
##   Under 20,000                            3.62820270                 0.83747050
##                      d$edu_rc2
## d$income_rc           Completed some college, but no longer in college
##   100,000 - 199,999                                        -1.15560534
##   20,000 - 39,999                                           1.67523356
##   200,000 - 1 million                                      -1.26261405
##   40,000 - 59,999                                           2.90811146
##   60,000 - 79,999                                           0.45226257
##   80,000 - 99,999                                          -1.01883503
##   Rather not say                                           -2.03710888
##   Under 20,000                                              0.64817672
##                      d$edu_rc2
## d$income_rc           Currently in college Currently in graduate education
##   100,000 - 199,999             3.17344062                     -3.10496155
##   20,000 - 39,999              -6.03922265                      4.32566233
##   200,000 - 1 million           2.98287660                     -1.26100328
##   40,000 - 59,999              -1.36772888                     -0.76255120
##   60,000 - 79,999               0.74795183                     -1.72625776
##   80,000 - 99,999               3.44665700                     -1.36696251
##   Rather not say                2.74155101                     -0.09740119
##   Under 20,000                 -4.46089387                      2.83374884
##                      d$edu_rc2
## d$income_rc           High school diploma or less, and NO COLLEGE
##   100,000 - 199,999                                   -0.47337619
##   20,000 - 39,999                                      0.97366915
##   200,000 - 1 million                                 -1.01154069
##   40,000 - 59,999                                     -1.00185246
##   60,000 - 79,999                                      0.22079242
##   80,000 - 99,999                                     -2.19292975
##   Rather not say                                       1.54981391
##   Under 20,000                                         0.51369252

9 Chi-square: Write Up Results

To test our hypothesis that there would be no education differences in participation across the income level categories, we ran a Chi-square test of independence. Our variables met most of the criteria for running a chi-square test of analysis (it used frequencies, the variables were independent, and there were two variables). However, we had a low number of participants who make over 1 million dollars and participants who completed some graduate degree, and did not meet the criteria for at least five participants per cell. To proceed with this analysis, we dropped the participants who made over 1 million dollars from our sample and combined our completed some graduate degree participants with the existing category for participants who completed their bachelors degree. The final sample for analysis can be seen in Table 1:

 edu_rc2 
 Complete 2 year College degree   Completed Bachelors Degree   Completed some college, but no longer in college   Currently in college   Currently in graduate education   High school diploma or less, and NO COLLEGE 
 income_rc 
   100,000 - 199,999  19 19 2 337 5 6
   20,000 - 39,999  27 37 7 249 31 9
   200,000 - 1 million  5 4 126 3 1
   40,000 - 59,999  17 33 9 269 12 4
   60,000 - 79,999  20 15 4 246 7 6
   80,000 - 99,999  7 11 1 211 6
   Rather not say  37 38 4 718 36 21
   Under 20,000  47 36 7 382 34 11

As predicted, we did not find an income difference in participation across the education level categories, χ2(35, N = 3136) = 125.7, p = 3.7e-12.

10 T-test: State Your Hypothesis

We predict that lower income participants will report significantly more stress than higher income individuals, as measured by the perceived stress scale (PSS-4).

11 T-test: 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':    3136 obs. of  8 variables:
##  $ ResponseId: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ edu_rc    : Factor w/ 7 levels "Complete 2 year College degree",..: 5 2 5 5 5 5 2 5 5 5 ...
##  $ income_rc : Factor w/ 8 levels "100,000 - 199,999",..: 2 2 7 7 6 7 8 2 1 7 ...
##  $ stress    : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ swb       : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ support   : num  6 6.75 5.17 5.58 6 ...
##  $ belong    : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ edu_rc2   : Factor w/ 6 levels "Complete 2 year College degree",..: 4 2 4 4 4 4 2 4 4 4 ...
d$income_rc <- as.factor(d$income_rc)

table(d$income_rc, useNA = "always")
## 
##   100,000 - 199,999     20,000 - 39,999 200,000 - 1 million     40,000 - 59,999 
##                 388                 360                 139                 344 
##     60,000 - 79,999     80,000 - 99,999      Rather not say        Under 20,000 
##                 298                 236                 854                 517 
##                <NA> 
##                   0
# you can use the describe() command on an entire dataframe (d) or just on a single variable (d$stress)
describe(d$stress)
##    vars    n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 3136 3.05 0.6      3    3.05 0.59 1.3 4.7   3.4 0.03    -0.16 0.01
# also use a histogram to examine your continuous variable
hist(d$stress)

# 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$stress, group=d$income_rc)
## 
##  Descriptive statistics by group 
## group: 100,000 - 199,999
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 388 2.99 0.63      3    2.98 0.59 1.5 4.6   3.1 0.07    -0.35 0.03
## ------------------------------------------------------------ 
## group: 20,000 - 39,999
##    vars   n mean  sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 360    3 0.6      3       3 0.59 1.4 4.6   3.2 -0.03    -0.14 0.03
## ------------------------------------------------------------ 
## group: 200,000 - 1 million
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 139 3.14 0.62    3.1    3.13 0.59 1.3 4.7   3.4 0.06    -0.04 0.05
## ------------------------------------------------------------ 
## group: 40,000 - 59,999
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 344 3.05 0.62    3.1    3.05 0.59 1.4 4.6   3.2 -0.05    -0.34 0.03
## ------------------------------------------------------------ 
## group: 60,000 - 79,999
##    vars   n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 298    3 0.6      3       3 0.59 1.3 4.6   3.3 0.04     0.05 0.03
## ------------------------------------------------------------ 
## group: 80,000 - 99,999
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 236 2.93 0.55    2.9    2.93 0.59 1.4 4.3   2.9 -0.02    -0.22 0.04
## ------------------------------------------------------------ 
## group: Rather not say
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 854 3.12 0.57    3.1    3.12 0.59 1.4 4.7   3.3 0.07    -0.08 0.02
## ------------------------------------------------------------ 
## group: Under 20,000
##    vars   n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 517 3.07 0.6    3.1    3.07 0.59 1.6 4.6     3 0.08    -0.32 0.03
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$stress~d$income_rc)

12 T-test: Check Your Assumptions

12.1 T-test: T-test Assumptions

  • IV must have two levels
  • Data values must be independent (independent t-test only)
  • Data obtained via a random sample
  • Dependent variable must be normally distributed
  • Variances of the two groups are approximately equal

12.2 T-test: 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!

d <- subset(d, income_rc != "20,000 - 39,999")
table(d$income_rc, useNA = "always")
## 
##   100,000 - 199,999     20,000 - 39,999 200,000 - 1 million     40,000 - 59,999 
##                 388                   0                 139                 344 
##     60,000 - 79,999     80,000 - 99,999      Rather not say        Under 20,000 
##                 298                 236                 854                 517 
##                <NA> 
##                   0
d$income_rc <- droplevels(d$income_rc)

d <- subset(d, income_rc != "200,000 - 1 million")
table(d$income_rc, useNA = "always")
## 
##   100,000 - 199,999 200,000 - 1 million     40,000 - 59,999     60,000 - 79,999 
##                 388                   0                 344                 298 
##     80,000 - 99,999      Rather not say        Under 20,000                <NA> 
##                 236                 854                 517                   0
d$income_rc <- droplevels(d$income_rc)

d <- subset(d, income_rc != "60,000 - 79,999")
table(d$income_rc, useNA = "always")
## 
## 100,000 - 199,999   40,000 - 59,999   60,000 - 79,999   80,000 - 99,999 
##               388               344                 0               236 
##    Rather not say      Under 20,000              <NA> 
##               854               517                 0
d$income_rc <- droplevels(d$income_rc)

d <- subset(d, income_rc != "80,000 - 99,999")
table(d$income_rc, useNA = "always")
## 
## 100,000 - 199,999   40,000 - 59,999   80,000 - 99,999    Rather not say 
##               388               344                 0               854 
##      Under 20,000              <NA> 
##               517                 0
d$income_rc <- droplevels(d$income_rc)

d <- subset(d, income_rc != "Rather not say")
table(d$income_rc, useNA = "always")
## 
## 100,000 - 199,999   40,000 - 59,999    Rather not say      Under 20,000 
##               388               344                 0               517 
##              <NA> 
##                 0
d$income_rc <- droplevels(d$income_rc)

d <- subset(d, income_rc != "Under 20,000")
table(d$income_rc, useNA = "always")
## 
## 100,000 - 199,999   40,000 - 59,999      Under 20,000              <NA> 
##               388               344                 0                 0
d$income_rc <- droplevels(d$income_rc)

# 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$stress~d$income_rc, date = d)
## Levene's Test for Homogeneity of Variance (center = median: d)
##        Df F value Pr(>F)
## group   1  0.0558 0.8133
##       730

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 changing the homogeneity of our variance, even if we already have a solution for any potential problems.

12.3 Issues with My Data

My independent variable has more than two levels. To proceed with this analysis, I will drop all income levels except 40,000 - 59,999 and 100,000 - 199,999. 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. Although Levene’s test was not significant, it was close to the significance threshold. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test.

13 T-test: 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$stress~d$income_rc)

14 T-test: View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$stress by d$income_rc
## t = -1.4003, df = 720.92, p-value = 0.1619
## alternative hypothesis: true difference in means between group 100,000 - 199,999 and group 40,000 - 59,999 is not equal to 0
## 95 percent confidence interval:
##  -0.15602474  0.02611584
## sample estimates:
## mean in group 100,000 - 199,999   mean in group 40,000 - 59,999 
##                        2.987371                        3.052326

15 T-test: Calculate Cohen’s d

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

16 T-test: View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: -0.1036458 (negligible)
## 95 percent confidence interval:
##       lower       upper 
## -0.24913125  0.04183964

17 T-test: Write Up Results

To test our hypothesis that lower income participants in our sample would report significantly more stress than higher income participants, we used an two-sample or independent t-test. This required us to drop participants who did not fall between the 40,000 - 59,999 dollar and 100,000 - 199,999 dollar income level 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 = 0.35). 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 lower income participants (M = 3.05, SD = .95) reported significantly higher stress than higher income participants (M = 2.99, SD = .89); t(720.92) = -1.40, p < 0.162 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .40 (small effect; Cohen, 1988).

References

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