Skip to content

Instantly share code, notes, and snippets.

@USMortality
Last active April 15, 2025 13:47
Show Gist options
  • Save USMortality/17bb9ce18e756c54d434f0b4d0e3b4eb to your computer and use it in GitHub Desktop.
Save USMortality/17bb9ce18e756c54d434f0b4d0e3b4eb to your computer and use it in GitHub Desktop.
ASMR Excess vs Confounders Correlation [World]
library(readr)
library(GGally)
sf <- 2
width <- 900 * sf
height <- 450 * sf
options(vsc.dev.args = list(width = width, height = height, res = 72 * sf))
source(paste0(
"https://gist.githubusercontent.com/USMortality/",
"cf0dc23d0d5ca609539fe11c9c089fcc/raw/asmr_country_dataset.r"
))
# Analysis
df2 <- df |>
filter(date == 2020) |>
select(
-iso3c,
-date,
-total_vaccinations_per_hundred,
-people_vaccinated_per_hundred,
-people_fully_vaccinated_per_hundred,
-total_boosters_per_hundred,
)
chart <- GGally::ggcorr(df2, geom = "blank", label = TRUE, hjust = 1) +
geom_point(
size = 10, aes(color = coefficient < 0, alpha = abs(coefficient) >= 0.4)
) +
scale_alpha_manual(values = c("TRUE" = 0.25, "FALSE" = 0)) +
guides(color = FALSE, alpha = FALSE) +
ggplot2::coord_cartesian(clip = "off") +
ggplot2::theme(plot.margin = ggplot2::unit(c(1, 0, 0, 2.5), "cm")) +
labs(title = "Correlation Matrix [2020]")
ggplot2::ggsave(
filename = "chart1.png", plot = chart, width = width, height = height,
units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo")
)
# By Date
cor_by_date <- function(df) {
df |>
group_by(date) |>
summarize(
across(everything(), ~ cor(.x, asmr_who_ex_p,
use = "pairwise.complete.obs"
))
) |>
tidyr::pivot_longer(
cols = -date, names_to = "variable", values_to = "correlation"
) |>
filter(variable != "asmr_who_ex_p") # Exclude self-correlation
}
# Create correlation bins
cor_results <- cor_by_date(df |> select(-iso3c)) |>
mutate(correlation_group = cut(
correlation,
breaks = c(-Inf, -0.4, 0.4, Inf),
labels = c("< -0.4", "-0.4 to 0.4", "> 0.4"),
include.lowest = TRUE
))
# Plot the heatmap with the simplified grouped correlation and values
chart <- ggplot(cor_results, aes(x = date, y = variable, fill = correlation_group)) +
geom_tile() +
geom_text(
aes(label = sprintf("%.1f", correlation)),
size = 3, color = "black"
) +
scale_fill_manual(values = c(
"< -0.4" = "#1a9850",
"-0.4 to 0.4" = "white",
"> 0.4" = "#d73027"
)) +
theme_minimal() +
labs(
title = "Correlation of eASMR with other variables",
fill = "Correlation"
)
ggplot2::ggsave(
filename = "chart2.png", plot = chart, width = width, height = height,
units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment