Skip to content

Instantly share code, notes, and snippets.

@acbass49
Created November 4, 2025 06:08
Show Gist options
  • Select an option

  • Save acbass49/732c8220e5bc17f7197575d8ed594361 to your computer and use it in GitHub Desktop.

Select an option

Save acbass49/732c8220e5bc17f7197575d8ed594361 to your computer and use it in GitHub Desktop.
22 Mormon Hispanics
library(tidyverse)
library(binom)
library(haven)
library(car)
library(ggtext)
library(sjlabelled)
# Load the dataset
data <- readRDS("./data/CES/cumulative_2006-2024.rds")
state_to_region <- c(
# Northeast Region
"CT" = "Northeast", "ME" = "Northeast", "MA" = "Northeast", "NH" = "Northeast",
"RI" = "Northeast", "VT" = "Northeast", "NJ" = "Northeast", "NY" = "Northeast",
"PA" = "Northeast",
# Midwest Region
"IL" = "Midwest", "IN" = "Midwest", "MI" = "Midwest", "OH" = "Midwest",
"WI" = "Midwest", "IA" = "Midwest", "KS" = "Midwest", "MN" = "Midwest",
"MO" = "Midwest", "NE" = "Midwest", "ND" = "Midwest", "SD" = "Midwest",
# South Region
"DE" = "South", "FL" = "South", "GA" = "South", "MD" = "South",
"NC" = "South", "SC" = "South", "VA" = "South", "WV" = "South",
"AL" = "South", "KY" = "South", "MS" = "South", "TN" = "South",
"AR" = "South", "LA" = "South", "OK" = "South", "TX" = "South",
"DC" = "South", # DC is typically grouped with the South/South Atlantic Division
# West Region (Highly relevant for your Mormon Metrics work!)
"AZ" = "West", "CO" = "West", "ID" = "West", "MT" = "West",
"NV" = "West", "NM" = "West", "UT" = "West", "WY" = "West",
"AK" = "West", "CA" = "West", "HI" = "West", "OR" = "West",
"WA" = "West"
)
data$region <- as.character(state_to_region[data$st])
data$relig_church_rc <- car::recode(as.numeric(data$relig_church),"1:2 = 'Weekly or More'; 3 = 'Monthly'; 4:5 = 'A few times a year/Seldom'; 6 = 'Never'")
data$relig_church_rc <- factor(data$relig_church_rc,
levels = c("Weekly or More", "Monthly", "A few times a year/Seldom", "Never"))
data$relig_church_rc2 <- car::recode(as.numeric(data$relig_church),"1:2 = 'Weekly or More'; 3:4 = 'Monthly/A few times a year'; 5:6 = 'Seldom/Never'")
data$relig_church_rc2 <- factor(data$relig_church_rc2,
levels = c("Weekly or More", "Monthly/A few times a year", "Seldom/Never"))
data$year_4 <- car::recode(as.numeric(data$year), "2008:2012 = '2008-12'; 2013:2016 = '2013-16'; 2017:2020 = '2017-20'; 2021:2024 = '2021-24'")
data$year_4 <- factor(data$year_4,
levels = c("2008-12", "2013-16", "2017-20", "2021-24"))
data$year_2 <- car::recode(as.numeric(data$year), "2007:2008 = '2007-08'; 2009:2010 = '2009-10'; 2011:2012 = '2011-12'; 2013:2014 = '2013-14'; 2015:2016 = '2015-16'; 2017:2018 = '2017-18'; 2019:2020 = '2019-20'; 2021:2022 = '2021-22'; 2023:2024 = '2023-24'")
data$year_2 <- factor(data$year_2,
levels = c("2007-08", "2009-10", "2011-12", "2013-14", "2015-16", "2017-18", "2019-20", "2021-22", "2023-24"))
# prayer was not in the cumulative file, so I need to merge it in
# I went through each year and added the prayer variable manually.
data <- data |>
mutate(id = paste0(year, "X", case_id)) |>
left_join(read.csv("./data/CES/prayer.csv"), by = "id") |>
mutate(year = year.x)
data <- data |>
mutate(id = paste0(year, "X", case_id)) |>
left_join(read.csv("./data/CES/hisp_origin.csv"), by = "id") |>
mutate(year = year.x)
data$Utah <- ifelse(data$state == "Utah", "Utah Resident", "Non-Utah Resident")
data$jello_belt <- ifelse(data$state %in% c("Utah", "Idaho", "Arizona"), 1, 0)
mormon_data <- data |> filter(data$religion == 3)
new_nms <- c(
'all_hisp_origin_1',
'all_hisp_origin_2',
'all_hisp_origin_3',
'all_hisp_origin_4',
'all_hisp_origin_5',
'all_hisp_origin_6',
'all_hisp_origin_7',
'all_hisp_origin_8',
'all_hisp_origin_9',
'all_hisp_origin_10',
'all_hisp_origin_11',
'all_hisp_origin_12'
)
do.call(rbind,lapply(new_nms,function(x){
data |>
count(!!as.symbol(x), wt = weight) |>
drop_na() |>
mutate(
nm = x,
prop = n / sum(n),
total_n = sum(n),
lower = binom.confint(x = n, n = total_n, method = "asymptotic")$lower,
upper = binom.confint(x = n, n = total_n, method = "asymptotic")$upper
) |>
filter(!!as.symbol(x) == 1) |>
select(nm, n, total_n, prop, lower, upper)
}))
do.call(rbind,lapply(new_nms,function(x){
mormon_data |>
count(!!as.symbol(x), wt = weight) |>
drop_na() |>
mutate(
nm = x,
prop = n / sum(n),
total_n = sum(n),
lower = binom.confint(x = n, n = total_n, method = "asymptotic")$lower,
upper = binom.confint(x = n, n = total_n, method = "asymptotic")$upper
) |>
filter(!!as.symbol(x) == 1) |>
select(nm, n, total_n, prop, lower, upper)
}))
data |>
filter(race == 3) |>
count(region, wt = weight) |>
mutate(
prop = n / sum(n),
total_n = sum(n),
lower = binom.confint(x = n, n = total_n, method = "asymptotic")$lower,
upper = binom.confint(x = n, n = total_n, method = "asymptotic")$upper
)
data |>
filter(religion == 3) |>
filter(race == 3) |>
count(region, wt = weight) |>
mutate(
prop = n / sum(n),
total_n = sum(n),
lower = binom.confint(x = n, n = total_n, method = "asymptotic")$lower,
upper = binom.confint(x = n, n = total_n, method = "asymptotic")$upper
)
data |>
filter(race == 3) |>
count(Utah, wt = weight) |>
mutate(
prop = n / sum(n),
total_n = sum(n),
lower = binom.confint(x = n, n = total_n, method = "asymptotic")$lower,
upper = binom.confint(x = n, n = total_n, method = "asymptotic")$upper
)
data |>
filter(religion == 3) |>
filter(race == 3) |>
count(Utah, wt = weight) |>
mutate(
prop = n / sum(n),
total_n = sum(n),
lower = binom.confint(x = n, n = total_n, method = "asymptotic")$lower,
upper = binom.confint(x = n, n = total_n, method = "asymptotic")$upper
)
data |>
filter(race == 3) |>
count(jello_belt, wt = weight) |>
mutate(
prop = n / sum(n),
total_n = sum(n),
lower = binom.confint(x = n, n = total_n, method = "asymptotic")$lower,
upper = binom.confint(x = n, n = total_n, method = "asymptotic")$upper
)
data |>
filter(religion == 3) |>
filter(race == 3) |>
count(jello_belt, wt = weight) |>
mutate(
prop = n / sum(n),
total_n = sum(n),
lower = binom.confint(x = n, n = total_n, method = "asymptotic")$lower,
upper = binom.confint(x = n, n = total_n, method = "asymptotic")$upper
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment