# Load required packages
library(ggplot2)
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(tibble)
library(ggthemes)
# ----------------------------
# 1. Define totals
# ----------------------------
total_events <- 5748
# ----------------------------
# 2. Create dataset (3 groups)
# ----------------------------
data <- tibble(
Group = c("Transgender", "Cis-Gendered White Men", "Other"),
Observed = c(5, 5690, 53),
Population_Percent = c(0.016, 0.47, 0.514)
)
# ----------------------------
# 3. Expected counts
# ----------------------------
data <- data %>%
mutate(
Expected = total_events * Population_Percent
)
# ----------------------------
# 4. Chi-square goodness-of-fit test
# ----------------------------
chisq_test <- chisq.test(
x = data$Observed,
p = data$Population_Percent
)
chisq_test
Chi-squared test for given probabilities
data: data$Observed
X-squared = 6237.4, df = 2, p-value < 2.2e-16
# ----------------------------
# 5. Per-capita rate
# ----------------------------
data <- data %>%
mutate(
Rate_per_1pct = Observed / (Population_Percent * 100)
)
data
# A tibble: 3 × 5
Group Observed Population_Percent Expected Rate_per_1pct
<chr> <dbl> <dbl> <dbl> <dbl>
1 Transgender 5 0.016 92.0 3.12
2 Cis-Gendered White Men 5690 0.47 2702. 121.
3 Other 53 0.514 2954. 1.03
# ----------------------------
# 6. Plot
# ----------------------------
ggplot(data, aes(x = Group, y = Rate_per_1pct)) +
geom_col() +
labs(
title = "Per Capita Event Rate by Group",
y = "Events per 1% of Population",
x = ""
) +
theme_economist()