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
# 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/mydata.csv", header=T)
We predict that hispanics will report significantly more sense of a need to belong than white people.
# 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': 3047 obs. of 6 variables:
## $ race_rc : chr "white" "white" "white" "other" ...
## $ marriage5 : chr "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
## $ moa_role : num 3 2.67 2.5 2 2.67 ...
## $ moa_safety: num 2.75 3.25 3 1.25 2.25 2.5 4 3.25 2.75 3.5 ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
## $ npi : num 0.6923 0.1538 0.0769 0.0769 0.7692 ...
d$race_rc <- as.factor(d$race_rc)
table(d$race_rc, useNA = "always")
##
## asian black hispanic multiracial nativeamer other
## 201 226 271 281 12 94
## white <NA>
## 1962 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$belong)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3047 3.23 0.61 3.3 3.25 0.59 1.3 5 3.7 -0.27 -0.12 0.01
# also use a histogram to examine your continuous variable
hist(d$belong)
# 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$belong, 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 201 3.25 0.57 3.3 3.26 0.59 1.5 4.5 3 -0.28 0.04 0.04
## ------------------------------------------------------------
## group: black
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 226 2.88 0.63 2.9 2.87 0.59 1.3 5 3.7 0.21 0.16 0.04
## ------------------------------------------------------------
## group: hispanic
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 271 2.96 0.56 3 2.96 0.59 1.3 4.4 3.1 -0.07 -0.05 0.03
## ------------------------------------------------------------
## group: multiracial
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 281 3.19 0.63 3.3 3.21 0.59 1.4 4.6 3.2 -0.35 -0.34 0.04
## ------------------------------------------------------------
## group: nativeamer
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 12 2.98 0.48 2.85 2.94 0.44 2.4 3.9 1.5 0.58 -1.15 0.14
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 94 3.01 0.64 3 3.02 0.52 1.4 4.6 3.2 -0.19 -0.06 0.07
## ------------------------------------------------------------
## group: white
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1962 3.33 0.58 3.4 3.35 0.59 1.4 4.9 3.5 -0.31 -0.03 0.01
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$belong~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!
d <- subset(d, race_rc != "asian")
table(d$race_rc, useNA = "always")
##
## asian black hispanic multiracial nativeamer other
## 0 226 271 281 12 94
## white <NA>
## 1962 0
d$race_rc <- droplevels(d$race_rc)
d <- subset(d, race_rc != "black")
table(d$race_rc, useNA = "always")
##
## black hispanic multiracial nativeamer other white
## 0 271 281 12 94 1962
## <NA>
## 0
d$race_rc <- droplevels(d$race_rc)
d <- subset(d, race_rc != "nativeamer")
table(d$race_rc, useNA = "always")
##
## hispanic multiracial nativeamer other white <NA>
## 271 281 0 94 1962 0
d$race_rc <- droplevels(d$race_rc)
d <- subset(d, race_rc != "multiracial")
table(d$race_rc, useNA = "always")
##
## hispanic multiracial other white <NA>
## 271 0 94 1962 0
d$race_rc <- droplevels(d$race_rc)
d <- subset(d, race_rc != "other")
table(d$race_rc, useNA = "always")
##
## hispanic other white <NA>
## 271 0 1962 0
d$race_rc <- droplevels(d$race_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(belong~race_rc, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.4451 0.5048
## 2231
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 the all participants that do not fall in the hispanic or white categories 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.
# once again, subetting to drop the nb group
d <- subset(d, race_rc != "asian")
d$race_rc <- droplevels(d$race_rc) # using droplevels() to drop the empty factor
d <- subset(d, race_rc != "black")
d$race_rc <- droplevels(d$race_rc)
d <- subset(d, race_rc != "multiracial")
d$race_rc <- droplevels(d$race_rc)
d <- subset(d, race_rc != "nativeamer")
d$race_rc <- droplevels(d$race_rc)
d <- subset(d, race_rc != "other")
d$race_rc <- droplevels(d$race_rc)
# 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$belong~d$race_rc)
t_output
##
## Welch Two Sample t-test
##
## data: d$belong by d$race_rc
## t = -10.294, df = 353.05, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group hispanic and group white is not equal to 0
## 95 percent confidence interval:
## -0.4478324 -0.3041576
## sample estimates:
## mean in group hispanic mean in group white
## 2.955351 3.331346
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$belong~d$race_rc)
d_output
##
## Cohen's d
##
## d estimate: -0.6546647 (medium)
## 95 percent confidence interval:
## lower upper
## -0.7831934 -0.5261360
To test our hypothesis that hispanics in our sample would report significantly more need to belong than white people, we used an two-sample or independent t-test. This required us to drop asian, black, multiracial, native american, and others 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 = .067). 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.
Unexpectedly, we found that hispanics (M = 2.97, SD = .95) reported significantly lower need to belong (M = 3.33, SD = .58); t(-10.294) = 5.43, “p” > .001 (see Figure 1). The effect size was calculated using Cohen’s “d”, with a value of -.65 (medium effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.