Skip to content

Instantly share code, notes, and snippets.

@acbass49
Created December 5, 2025 03:14
Show Gist options
  • Select an option

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

Select an option

Save acbass49/dd2fec0d69d2a3804d021710f4db0dce to your computer and use it in GitHub Desktop.
24 fear of death
library(tidyverse)
library(haven)
library(stringr)
library(data.table)
library(binom)
svys <- c(
"./data/fears_surveys/w2015.sav",
"./data/fears_surveys/w2017.sav",
"./data/fears_surveys/w2019.sav",
"./data/fears_surveys/w2021.sav",
"./data/fears_surveys/w2023.sav"
)
dfs <- lapply(svys, read_sav)
create_mormon_variable <- function(denom_vector) {
denom_vector <- tolower(denom_vector)
denom_vector <- str_remove_all(denom_vector, "[[:punct:]]")
denom_vector <- str_squish(denom_vector)
denom_vector <- stringr::str_detect(denom_vector, "mormon|morman|lds|latter[- ]day saint|ladder|day saint|community of christ") |>
ifelse("LDS", "Not LDS")
return(denom_vector)
}
# recode the religion variable in each dataframe
dfs[[1]]$mormon <- create_mormon_variable(dfs[[1]]$Q1_TEXT)
dfs[[1]]$relig <- dplyr::case_when(
dfs[[1]]$mormon == "LDS" ~ "Latter-day Saints",
dfs[[1]]$I_RELIGION %in% c(1) ~ "Protestant",
dfs[[1]]$I_RELIGION %in% c(2) ~ "Catholic",
dfs[[1]]$I_RELIGION %in% c(3,4) ~ "Other",
dfs[[1]]$I_RELIGION == 5 ~ "None",
TRUE ~ NA_character_
)
dfs[[1]]$dying <- dfs[[1]]$Q36_5 #dying
dfs[[2]]$mormon <- ifelse(dfs[[2]]$RELIGION == 10, "LDS", "Not LDS")
dfs[[2]]$relig <- dplyr::case_when(
dfs[[2]]$mormon == "LDS" ~ "Latter-day Saints",
dfs[[2]]$I_RELIGION %in% c(1) ~ "Protestant",
dfs[[2]]$I_RELIGION %in% c(2) ~ "Catholic",
dfs[[2]]$I_RELIGION %in% c(3,4) ~ "Other",
dfs[[2]]$I_RELIGION == 5 ~ "None",
TRUE ~ NA_character_
)
dfs[[2]]$dying <- dfs[[2]]$QN12C #dying
dfs[[3]]$mormon <- ifelse(dfs[[3]]$RELIGION == 10, "LDS", "Not LDS")
dfs[[3]]$relig <- dplyr::case_when(
dfs[[3]]$mormon == "LDS" ~ "Latter-day Saints",
dfs[[3]]$I_RELIGION %in% c(1) ~ "Protestant",
dfs[[3]]$I_RELIGION %in% c(2) ~ "Catholic",
dfs[[3]]$I_RELIGION %in% c(3,4) ~ "Other",
dfs[[3]]$I_RELIGION == 5 ~ "None",
TRUE ~ NA_character_
)
dfs[[3]]$dying <- dfs[[3]]$QN14C #dying
dfs[[3]]$WEIGHT <- dfs[[3]]$WEIGHTCV
dfs[[4]]$mormon <- ifelse(dfs[[4]]$PRELIGION == 10, "LDS", "Not LDS")
dfs[[4]]$relig <- dplyr::case_when(
dfs[[4]]$mormon == "LDS" ~ "Latter-day Saints",
dfs[[4]]$I_RELIGION %in% c(1) ~ "Protestant",
dfs[[4]]$I_RELIGION %in% c(2) ~ "Catholic",
dfs[[4]]$I_RELIGION %in% c(3) ~ "Other",
dfs[[4]]$I_RELIGION == 4 ~ "None",
TRUE ~ NA_character_
)
dfs[[4]]$dying <- dfs[[4]]$Q10C #dying
dfs[[5]]$mormon <- ifelse(dfs[[5]]$PRELIGION == 10, "LDS", "Not LDS")
dfs[[5]]$relig <- dplyr::case_when(
dfs[[5]]$mormon == "LDS" ~ "Latter-day Saints",
dfs[[5]]$I_RELIGION %in% c(1) ~ "Protestant",
dfs[[5]]$I_RELIGION %in% c(2) ~ "Catholic",
dfs[[5]]$I_RELIGION %in% c(3) ~ "Other",
dfs[[5]]$I_RELIGION == 4 ~ "None",
TRUE ~ NA_character_
)
dfs[[5]]$dying <- dfs[[5]]$Q10C
dfs <- lapply(dfs, function(df) {
df %>%
select(relig, dying, WEIGHT) %>%
mutate(
WEIGHT = as.numeric(WEIGHT),
relig = factor(relig, levels = c("Latter-day Saints", "Protestant", "Catholic", "Other", "None")),
dying = as.numeric(dying),
dying = ifelse(dying == -1, NA, dying)
)
})
final <- data.table::rbindlist(dfs, fill=TRUE)
final |>
count(relig) |>
summarise(n = sum(n))
final |>
group_by(relig) |>
count(dying, wt=WEIGHT) |>
drop_na() |>
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
) |>
pivot_wider(id_cols = relig, names_from = dying, values_from = c(prop))
# 1) Very afraid
# 2) Afraid
# 3) Slightly afraid
# 4) Not afraid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment