library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To drop variable use NULL: let(mtcars, am = NULL) %>% head()
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/final copy.csv", header=T)
There will be no income differences in believed marriage importance across the income scale.
(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:
## $ efficacy : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ belong : int 4 4 2 4 4 3 4 4 4 3 ...
## $ marriageimportance: int 2 3 2 1 2 3 4 3 4 2 ...
## $ race_rc : chr "white" "white" "white" "other" ...
## $ politicalviews : num 2.5 2.5 5 8 4.5 8 4 1.5 5.5 6 ...
## $ income : int 3 3 1 1 6 1 2 3 7 1 ...
# 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$income <- as.factor(d$income)
d$marriageimportance <- as.factor (d$marriageimportance)
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$marriageimportance, useNA = "always")
##
## 1 2 3 4 5 <NA>
## 154 361 746 1155 756 10
cross_cases(d, marriageimportance, income)
| income | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
| marriageimportance | |||||||||
| 1 | 56 | 34 | 15 | 18 | 12 | 4 | 10 | 4 | |
| 2 | 103 | 74 | 40 | 48 | 24 | 21 | 28 | 16 | 1 |
| 3 | 216 | 145 | 88 | 90 | 48 | 49 | 80 | 22 | 1 |
| 4 | 293 | 171 | 130 | 130 | 122 | 98 | 154 | 49 | 5 |
| 5 | 190 | 94 | 88 | 58 | 93 | 64 | 116 | 49 | |
| #Total cases | 858 | 518 | 361 | 344 | 299 | 236 | 388 | 140 | 7 |
Upon looking at the codebook for the EAMMI2 data, I saw that income is measured 1-rather not say, 2- under 20,000, 9- over 1 million. Because 1 is “rather not say” rather than representing the lowest income, I will remove this level from my data.
I will also drop level 9 of income because there are not 5 or more participants per cell.
# we'll use the subset command to drop our non-binary participants
d <- subset(d, income != "9")
d <- subset(d, income != "1")
#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 <- droplevels(d$income)
table(d$income, useNA = "always")
##
## 2 3 4 5 6 7 8 <NA>
## 518 361 344 302 236 389 140 0
# since I made changes to my variables, I am going to re-run the cross_cases() command
cross_cases(d, income, marriageimportance)
| marriageimportance | |||||
|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | |
| income | |||||
| 2 | 34 | 74 | 145 | 171 | 94 |
| 3 | 15 | 40 | 88 | 130 | 88 |
| 4 | 18 | 48 | 90 | 130 | 58 |
| 5 | 12 | 24 | 48 | 122 | 93 |
| 6 | 4 | 21 | 49 | 98 | 64 |
| 7 | 10 | 28 | 80 | 154 | 116 |
| 8 | 4 | 16 | 22 | 49 | 49 |
| #Total cases | 97 | 251 | 522 | 854 | 562 |
# 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$marriageimportance)
# 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$marriageimportance
## X-squared = 88.083, df = 24, p-value = 2.986e-09
# 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$marriageimportance
## d$income 1 2 3 4 5
## 2 2.97926047 2.73653135 3.17983017 -2.32515588 -3.86930200
## 3 -0.09048880 0.06652802 0.76063538 -0.57642007 -0.09987187
## 4 0.98765468 1.91396510 1.59546268 0.18006255 -3.60970971
## 5 -0.21148084 -1.75191597 -2.99618608 1.32074897 2.80809618
## 6 -2.05085973 -1.08010473 -0.80072750 1.39758527 0.95477369
## 7 -1.78657923 -2.60220442 -1.14126040 1.04250026 2.66712051
## 8 -0.83973456 0.17526476 -2.07148077 -0.59521736 2.95399584
# 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, income = "income", marriageimportance = "believed marriage importance")
To test my hypothesis that there would be no income differences in believed marriage importance across the income scale, I ran a Chi-square test of independence. My 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, I had a low number of people whose yearly salary is over 1 million dollars, and one of the income categories was “rather not say” which throws off my data. To proceed with this analysis, I dropped those with a salary over 1 million dollars, and dropped the “rather not say” participants from my sample. The final sample for analysis can be seen in Table 1:
| believed marriage importance | |||||
|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | |
| income | |||||
| 2 | 34 | 74 | 145 | 171 | 94 |
| 3 | 15 | 40 | 88 | 130 | 88 |
| 4 | 18 | 48 | 90 | 130 | 58 |
| 5 | 12 | 24 | 48 | 122 | 93 |
| 6 | 4 | 21 | 49 | 98 | 64 |
| 7 | 10 | 28 | 80 | 154 | 116 |
| 8 | 4 | 16 | 22 | 49 | 49 |
As predicted, I did not find an income difference in believed marriage importance across the income scale, X^2(24, N = 2286 ) = 88.083, p = 2.99.
We predict that white individuals will report being more republican/conservative than black individuals, as measured by the politics and party surveys.
# 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': 2290 obs. of 6 variables:
## $ efficacy : num 3.4 3.4 3 2.3 3 3 3.3 2.9 2.9 2.5 ...
## $ belong : int 4 4 4 4 4 4 4 4 5 2 ...
## $ marriageimportance:Class 'labelled' Factor w/ 5 levels "1","2","3","4",..: 2 3 2 4 3 4 2 1 2 2 ...
## .. .. LABEL: believed marriage importance
## $ race_rc : chr "white" "white" "white" "white" ...
## $ politicalviews : num 2.5 2.5 4.5 4 1.5 5.5 5.5 8 4 1.5 ...
## $ income :Class 'labelled' Factor w/ 7 levels "2","3","4","5",..: 2 2 5 1 2 6 2 1 2 1 ...
## .. .. LABEL: income
d$race_rc <- as.factor(d$race_rc)
table(d$politicalviews, useNA = "no")
##
## 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8
## 100 147 283 262 230 157 129 125 197 143 197 79 54 7 175
d$politicalviews <- as.numeric(d$politicalviews)
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$politicalviews)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2285 4.01 1.99 3.5 3.88 2.22 1 8 7 0.44 -0.83 0.04
# also use a histogram to examine your continuous variable
hist(d$politicalviews)
# 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$politicalviews, group=d$race_rc)
##
## Descriptive statistics by group
## group: asian
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 123 3.96 1.93 3.5 3.74 1.48 1 8 7 0.82 -0.39 0.17
## ------------------------------------------------------------
## group: black
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 165 3.76 2.07 3.5 3.58 2.22 1 8 7 0.73 -0.4 0.16
## ------------------------------------------------------------
## group: hispanic
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 222 3.71 1.96 3 3.48 1.48 1 8 7 0.88 -0.24 0.13
## ------------------------------------------------------------
## group: nativeamer
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 8 4.44 2.04 3.75 4.44 1.85 2 8 6 0.5 -1.39 0.72
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 51 4.29 1.93 4.5 4.24 2.22 1 8 7 0.06 -1.14 0.27
## ------------------------------------------------------------
## group: white
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1491 4.09 1.99 4 3.98 2.22 1 8 7 0.32 -0.95 0.05
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$politicalviews~d$race_rc)
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
leveneTest(politicalviews~race_rc, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 5 1.8383 0.1022
## 2054
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.
My independent variable has more than two levels. To proceed with this analysis, I will drop all races except for black and white 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.
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.
d <- subset(d, race_rc != "hispanic")
d <- subset(d, race_rc != "asian")
d <- subset(d, race_rc != "nativeamer")
d <- subset(d, race_rc != "other")
table(d$race_rc, useNA = "always")
##
## asian black hispanic nativeamer other white <NA>
## 0 167 0 0 0 1492 0
d$race_rc <- droplevels(d$race_rc)
# using droplevels() to drop the empty factor
# 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$politicalviews~d$race_rc)
t_output
##
## Welch Two Sample t-test
##
## data: d$politicalviews by d$race_rc
## t = -1.9022, df = 198.79, p-value = 0.05858
## alternative hypothesis: true difference in means between group black and group white is not equal to 0
## 95 percent confidence interval:
## -0.6562338 0.0118097
## sample estimates:
## mean in group black mean in group white
## 3.763636 4.085848
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$politicalviews~d$race_rc)
d_output
##
## Cohen's d
##
## d estimate: -0.1615057 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.3225218318 -0.0004896355
To test our hypothesis that that white individuals will report being more republican/conservative than black individuals, I used a two-sample or independent t-test. This required me to drop all races except for black and white from my 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 homogeneity (p = .4128). I then used Welche’s T-test, which does not assume homogeneity of variance. Our data met all other assumptions of a t-test.
Based on my results from the T-test, I would not conclude that there is a significant difference in means between the “black” and “white” groups. The effect size was calculated using Cohen’s d, with a value of -0.1615 (negligible effect; Cohen, 1988). “t”(198.79) = -1.902, “p” > .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -0.1615 (negligible effect; Cohen, 1988).
In this case, the p-value of 0.05858 is greater than 0.05, indicating that the difference in means between the groups is not statistically significant at the 5% level.
The 95% confidence interval for the difference in means is -1.31246765 to 0.02361941. Since this confidence interval includes zero, it suggests that the true difference in means between the “black” and “white” groups could be zero.
There is no significant evidence to suggest that there is a difference in means between the “black” and “white” groups.
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.