# install any packages you have not previously used, then comment them back out.
#install.packages("car")
#install.packages("effsize")
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
d <- read.csv(file="Data/projectdata.csv", header=T)
# For the HW, you will import the project dataset you cleaned previously
# This will be the dataset you'll use for HWs throughout the rest of the semester
I predict that Republicans will report significantly higher levels of exploitativeness than Democrats.
# you only need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 3141 obs. of 7 variables:
## $ ResponseId: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ npi : num 0.6923 0.1538 0.0769 0.0769 0.7692 ...
## $ exploit : num 2 3.67 4.33 1.67 4 ...
## $ 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 ...
## $ party_rc : chr "democrat" "independent" "apolitical" "apolitical" ...
## $ edu : chr "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
# if the categorical variable you're using is showing as a "chr" (character), you must change it to be a factor -- using the next line of code (as.factor)
d$party_rc <- as.factor(d$party_rc)
table(d$party_rc, useNA = "always")
##
## apolitical democrat independent republican <NA>
## 438 1596 326 781 0
## Checking the Continuous variable (DV)
# you can use the describe() command on an entire dataframe (d) or just on a single variable within your dataframe -- which we will do here
describe(d$exploit)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3141 2.39 1.37 2 2.21 1.48 1 7 6 0.94 0.36 0.02
# also use a histogram to visualize your continuous variable
hist(d$exploit)
# 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$exploit, group=d$party_rc)
##
## Descriptive statistics by group
## group: apolitical
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 438 2.48 1.44 2 2.3 1.48 1 7 6 0.8 -0.18 0.07
## ------------------------------------------------------------
## group: democrat
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1596 2.29 1.33 2 2.1 1.48 1 7 6 1.08 0.8 0.03
## ------------------------------------------------------------
## group: independent
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 326 2.61 1.52 2.33 2.43 1.98 1 7 6 0.81 -0.06 0.08
## ------------------------------------------------------------
## group: republican
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 781 2.44 1.35 2 2.29 1.48 1 7 6 0.8 0.05 0.05
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$exploit~d$party_rc)
# If the IV has more than 2 levels, you must drop the additional levels so that you meet the first assumption of a t-test.
#DATAFRAME <- subset(DATAFRAME, VARIABLE != "LEVEL")
d <- subset(d, party_rc != "apolitical")
table(d$party_rc, useNA = "always") #verify that now there are no participants in the removed level
##
## apolitical democrat independent republican <NA>
## 0 1596 326 781 0
d$party_rc <- droplevels(d$party_rc) # use droplevels() to drop the empty factor
table(d$party_rc, useNA = "always") #verify that now the entire factor level is removed
##
## democrat independent republican <NA>
## 1596 326 781 0
d <- subset(d, party_rc != "independent")
table(d$party_rc, useNA = "always") #verify that now there are no participants in the removed level
##
## democrat independent republican <NA>
## 1596 0 781 0
d$party_rc <- droplevels(d$party_rc) # use droplevels() to drop the empty factor
table(d$party_rc, useNA = "always")
##
## democrat republican <NA>
## 1596 781 0
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\(party_rc <- factor(d\)party_rc) # Ensure ‘group’ is a factor
# use the leveneTest() command from the car package to test homogeneity of variance
# it 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
d$party_rc <- factor(d$party_rc) # Ensure 'group' is a factor
leveneTest(d$exploit~d$party_rc, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 2.5713 0.1089
## 2375
The data does not have significantly different variances between the two comparison groups.
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 assumption about variance as Student’s t-test (the general default type of t-test). 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 the solution for any potential problems.
My independent variable has more than two levels. To proceed with this analysis, I will drop the apolitical and independent participants 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 does not have an issue regarding homogeneity of variance as my Levene’s test was not significant. I do not need to accommodate for this heterogeneity of variance by using a Welch’s t-test instead of Student’s t-test in my analysis.
# very simple! we use the same formula of y~x, where y is our DV and x is our IV
t_output <- t.test(d$exploit~d$party_rc)
t_output
##
## Welch Two Sample t-test
##
## data: d$exploit by d$party_rc
## t = -2.593, df = 1522.8, p-value = 0.009604
## alternative hypothesis: true difference in means between group democrat and group republican is not equal to 0
## 95 percent confidence interval:
## -0.26708449 -0.03703315
## sample estimates:
## mean in group democrat mean in group republican
## 2.289683 2.441741
# once again, we use the same formula, y~x, to calculate cohen's d
d_output <- cohen.d(d$exploit~d$party_rc)
d_output
##
## Cohen's d
##
## d estimate: -0.1139736 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.1996681 -0.0282791
To test my hypothesis that Republicans in my sample would report significantly higher levels of exploitativeness than Democrats, I used an independent samples t-test. This required me to drop my apolitical and independent participants from my sample, as I am limited to a two-group comparison when using this test. I tested the homogeneity of variance with Levene’s test and found not significant results (p > .001). Then I used Welch’s t-test, which does not assume homogeneity of variance.
As predicted, I found that Republicans (M = 2.44, SD = 1.35) reported significantly higher levels of exploitativeness than Democrats (M = 2.29, SD = 1.33); t(1522.8) = -2.593, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .11 (very small effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.