library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To select columns from data: columns(mtcars, mpg, vs:carb)
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
# 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/cashw.csv", header=T)
There will be income differences in participation across political affiliations categories.
(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.)
# 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': 3182 obs. of 6 variables:
## $ income : int 3 3 1 1 6 1 2 3 7 1 ...
## $ politics: int 2 1 2 8 1 8 4 2 8 4 ...
## $ stress : num 3.1 3.8 4.3 3 3.3 3.7 3.4 2.2 2.9 2.6 ...
## $ SocMedia: num 4.27 2.09 3.09 3.18 3.36 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ belong : num 2.6 4.2 3.8 4.2 3.4 4.2 4.3 3.8 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$politics <- as.factor(d$politics)
d$income <- as.factor(d$income)
table(d$income, useNA = "always")
##
## 1 2 3 4 5 6 7 8 9 <NA>
## 860 518 361 344 302 236 389 140 7 25
table(d$politics, useNA = "always")
##
## 1 2 3 4 5 6 7 8 <NA>
## 235 772 373 568 308 332 57 532 5
cross_cases(d, income, politics)
| politics | ||||||||
|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
| income | ||||||||
| 1 | 48 | 197 | 82 | 158 | 71 | 75 | 11 | 214 |
| 2 | 50 | 135 | 58 | 94 | 48 | 39 | 6 | 88 |
| 3 | 28 | 94 | 42 | 61 | 35 | 32 | 5 | 64 |
| 4 | 33 | 78 | 52 | 79 | 23 | 33 | 6 | 40 |
| 5 | 21 | 67 | 46 | 52 | 37 | 31 | 2 | 46 |
| 6 | 18 | 64 | 35 | 36 | 31 | 19 | 9 | 24 |
| 7 | 22 | 93 | 43 | 62 | 46 | 72 | 12 | 39 |
| 8 | 11 | 41 | 15 | 20 | 15 | 25 | 5 | 8 |
| 9 | 1 | 1 | 2 | 3 | ||||
| #Total cases | 231 | 770 | 373 | 563 | 308 | 329 | 56 | 523 |
While my data meets the first three assumptions, I don’t have at least 5 participants in all cells. The number of participants whose income is over 1 million is pretty small, and for one of the political affiliation groups, conservative, with an income of group 5, is less than five.
To proceed with this analysis, I will drop the group 9, over 1 million, income participants from my sample and add the group 7, conservative, politics group participants to the 8, I dont know, category. Dropping participants is always a difficult choice, and has the potential to further marginalize already minoritized groups, 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 income group 9 participants
d <- subset(d, income != "9") #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
table(d$income, useNA = "always")
##
## 1 2 3 4 5 6 7 8 9 <NA>
## 860 518 361 344 302 236 389 140 0 0
d$income <- droplevels(d$income)
table(d$income, useNA = "always")
##
## 1 2 3 4 5 6 7 8 <NA>
## 860 518 361 344 302 236 389 140 0
# we'll recode our politics variable to combine our conservative participants with our i dont know participants
d$politics_rc <- d$politics
# create a new variable (race_rc2_ identical to current variable (politics_rc)
d$politics_rc[d$politics == "7"] <- "8"
table(d$politics_rc, useNA = "always")
##
## 1 2 3 4 5 6 7 8 <NA>
## 231 769 373 562 306 326 0 579 4
# we will use some of our previous code to recode our conservative participants
d$politics_rc <- droplevels(d$politics_rc) # once again, we need to use the droplevels() command
table(d$politics_rc, useNA = "always")
##
## 1 2 3 4 5 6 8 <NA>
## 231 769 373 562 306 326 579 4
# since I made changes to my variables, I am going to re-run the cross_cases() command
cross_cases(d, income, politics_rc)
| politics_rc | |||||||
|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 8 | |
| income | |||||||
| 1 | 48 | 197 | 82 | 158 | 71 | 75 | 225 |
| 2 | 50 | 135 | 58 | 94 | 48 | 39 | 94 |
| 3 | 28 | 94 | 42 | 61 | 35 | 32 | 69 |
| 4 | 33 | 78 | 52 | 79 | 23 | 33 | 46 |
| 5 | 21 | 67 | 46 | 52 | 37 | 31 | 48 |
| 6 | 18 | 64 | 35 | 36 | 31 | 19 | 33 |
| 7 | 22 | 93 | 43 | 62 | 46 | 72 | 51 |
| 8 | 11 | 41 | 15 | 20 | 15 | 25 | 13 |
| #Total cases | 231 | 769 | 373 | 562 | 306 | 326 | 579 |
# 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, d$politics_rc)
# 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 and d$politics_rc
## X-squared = 138.65, df = 42, p-value = 2.94e-12
# 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$politics_rc
## d$income 1 2 3 4 5
## 1 -2.28126984 -1.14085451 -2.41528954 0.53176741 -1.65750364
## 2 2.20520628 0.93756466 -0.50794803 0.18382300 -0.38676145
## 3 0.32019051 0.74950362 -0.13866174 -0.50949842 -0.02136207
## 4 1.69555197 -0.80911775 1.98182498 2.61720620 -2.01660706
## 5 -0.27259494 -0.96046621 1.90844962 -0.30796320 1.55747633
## 6 0.17419894 0.99421050 1.46955525 -1.08824474 1.83759214
## 7 -1.36275525 -0.26291225 -0.52290655 -1.05915040 1.49209039
## 8 0.23875666 1.36380569 -0.42762146 -1.13073974 0.40345047
## d$politics_rc
## d$income 6 8
## 1 -1.80106384 6.97382601
## 2 -2.31507968 -0.16553583
## 3 -0.99262734 0.36959534
## 4 -0.49609988 -2.55206360
## 5 -0.05845057 -1.18398789
## 6 -1.21146649 -1.82239541
## 7 5.63169683 -2.87811685
## 8 2.97668599 -2.84828845
# the apply_labels() command can make our levels long and difficult to work with, so we'll only set them at the end
d <- apply_labels (d,
gender = "Gender",
gender = c("Women" = "f", "Men" = "m"),
race_rc2 = "Race/Ethnicity",
race_rc2 = c("Asian" = "asian",
"Black/African American" = "black",
"Hispanic, Latino, or Latina" = "hispanic",
"Multiracial" = "multiracial",
"Small Groups Combined" = "other",
"White" = "white"))
## Warning in apply_labels.list(data, ...): Some names don't exist in `data`:
## gender, race_rc2
To test our hypothesis that there would be income differences in participation across political affiliation 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 conservative and income of above 1 million participants and did not meet the criteria for at least five participants per cell. To proceed with this analysis, we dropped the group of an income above 1 million participants from our sample and combined our conservative participants with the existing category for participants who did not know their affiliation. The final sample for analysis can be seen in Table 1:
| politics_rc | |||||||
|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 8 | |
| income | |||||||
| 1 | 48 | 197 | 82 | 158 | 71 | 75 | 225 |
| 2 | 50 | 135 | 58 | 94 | 48 | 39 | 94 |
| 3 | 28 | 94 | 42 | 61 | 35 | 32 | 69 |
| 4 | 33 | 78 | 52 | 79 | 23 | 33 | 46 |
| 5 | 21 | 67 | 46 | 52 | 37 | 31 | 48 |
| 6 | 18 | 64 | 35 | 36 | 31 | 19 | 33 |
| 7 | 22 | 93 | 43 | 62 | 46 | 72 | 51 |
| 8 | 11 | 41 | 15 | 20 | 15 | 25 | 13 |
There was some oversampling and undersampling in certain categories. The section for politics 8, I dont know, had a few undersampling and oversampling analyses. This section was more drastic but section 1, 3, 4, and 6 had 1-3 instances of oversampling or undersampling. This shows significance in these areas and coincides with our hypothesis that there is a difference in income across political affiliation categories.
As predicted, we did find an income difference in participation across the political affiliation categories, χ2(42, N = 3150) = 138.65, p = 2.94e-12
–alternative–
We predict that lower income groups will report significantly more stress than high income groups, as measured by the perceived stress scale (PSS-4).
# 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': 3150 obs. of 7 variables:
## $ income : Factor w/ 8 levels "1","2","3","4",..: 3 3 1 1 6 1 2 3 7 1 ...
## $ politics : Factor w/ 8 levels "1","2","3","4",..: 2 1 2 8 1 8 4 2 8 4 ...
## $ stress : num 3.1 3.8 4.3 3 3.3 3.7 3.4 2.2 2.9 2.6 ...
## $ SocMedia : num 4.27 2.09 3.09 3.18 3.36 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ belong : num 2.6 4.2 3.8 4.2 3.4 4.2 4.3 3.8 2.9 2.5 ...
## $ politics_rc: Factor w/ 7 levels "1","2","3","4",..: 2 1 2 7 1 7 4 2 7 4 ...
d$income <- as.factor(d$income)
table(d$income, useNA = "always")
##
## 1 2 3 4 5 6 7 8 <NA>
## 860 518 361 344 302 236 389 140 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$stress)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3146 3.06 0.66 3.1 3.06 0.59 1 5 4 0.03 -0.04 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)
##
## Descriptive statistics by group
## group: 1
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 857 3.13 0.63 3.1 3.12 0.59 1.3 5 3.7 0.08 0.08 0.02
## ------------------------------------------------------------
## group: 2
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 518 3.11 0.68 3.1 3.11 0.59 1.4 5 3.6 0.11 -0.28 0.03
## ------------------------------------------------------------
## group: 3
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 361 3.04 0.65 3 3.04 0.59 1.1 5 3.9 -0.04 0.24 0.03
## ------------------------------------------------------------
## group: 4
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 344 3.06 0.7 3.1 3.07 0.74 1 5 4 -0.04 -0.29 0.04
## ------------------------------------------------------------
## group: 5
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 302 3 0.66 3 3 0.59 1.2 5 3.8 0.02 -0.09 0.04
## ------------------------------------------------------------
## group: 6
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 236 2.95 0.63 3 2.96 0.59 1.3 4.7 3.4 -0.17 -0.2 0.04
## ------------------------------------------------------------
## group: 7
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 388 2.98 0.67 3 2.98 0.74 1.2 4.7 3.5 0 -0.16 0.03
## ------------------------------------------------------------
## group: 8
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 140 3.12 0.67 3.1 3.1 0.44 1.5 4.9 3.4 0.3 -0.04 0.06
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$stress~d$income)
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 != "1")
table(d$income, useNA = "always")
##
## 1 2 3 4 5 6 7 8 <NA>
## 0 518 361 344 302 236 389 140 0
d$income <- droplevels(d$income) #using droplevels() to drop the empty factor
d$newincome[d$income == "2"] <- 1
d$newincome[d$income == "3"] <- 1
d$newincome[d$income == "4"] <- 1
d$newincome[d$income == "5"] <- 1
d$newincome[d$income == "6"] <- 2
d$newincome[d$income == "7"] <- 2
d$newincome[d$income == "8"] <- 2
# 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(stress~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 6 0.7735 0.5907
## 2282
As you can see, our data is not 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.
My independent variable has more than two levels. To proceed with this analysis, I will drop the group 1 participants of income from my sample. I will then recode a new variable and split the income levels through the middle to make a “1” as a low level for income and a “2” as a high level for income. Therefore I can run the t-test. 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.
# 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$newincome)
t_output
##
## Welch Two Sample t-test
##
## data: d$stress by d$newincome
## t = 2.3181, df = 1550.6, p-value = 0.02058
## alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
## 95 percent confidence interval:
## 0.01048813 0.12588059
## sample estimates:
## mean in group 1 mean in group 2
## 3.061902 2.993717
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$stress~d$newincome)
## Warning in cohen.d.formula(d$stress ~ d$newincome): Cohercing rhs of formula to
## factor
d_output
##
## Cohen's d
##
## d estimate: 0.1021619 (negligible)
## 95 percent confidence interval:
## lower upper
## 0.0151916 0.1891323
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 our “wont say” participants from our sample, as we are limited to a two-group comparison when using this test. Then we combined levels 2,3,4,5 into a level of “1” to show a lower level of income and we combined levels 6,7,8 into a “2” level to show a high income level. We tested the homogeneity of variance with Levene’s test and found some signs of heterogeneity (p = .5907). 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 low level income (M = 3.06, SD = .95) reported significantly higher stress than high level income (M = 2.99, SD = .89); “t”(1550.6) = 2.318, “p” < .021 (see Figure 1). The effect size was calculated using Cohen’s “d”, with a value of .10 (negligible; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.