Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Created February 18, 2021 12:57
Show Gist options
  • Save explodecomputer/4c91280357a3f19ae0e5e24629596339 to your computer and use it in GitHub Desktop.
Save explodecomputer/4c91280357a3f19ae0e5e24629596339 to your computer and use it in GitHub Desktop.
Sex differences in dementia rates over time
---
title: Survival bias in sex association with dementia
author: Gibran Hemani
date: `r Sys.time()`
---
```{r}
library(dplyr)
library(tidyr)
library(ggplot2)
```
In week 5 there is a question about why rates of dementia change over time differently in men vs women. It starts off roughly equal at age 65, but by age 90 the percentage of men with dementia is roughly half the percentage of women with dementia.
The answers suggest this could be explained by survivor bias, because men die earlier than women. However I think this would only be true if dementia causes death in men more quickly than women. The fact that men die earlier is not sufficient to explain the difference
```{r}
n <- 10000
timepoints <- 1:10
male_mortality <- 0.1
female_mortality <- 0.05
dementia_risk <- 0.05
dat <- tibble(
id = 1:n,
sex = rbinom(n, 1, 0.5),
tp0_dementia = 0,
tp0_alive = 1,
death_risk = case_when(
sex == 0 ~ female_mortality,
TRUE ~ male_mortality
),
dementia_risk = dementia_risk
)
for(i in timepoints)
{
last_dementia <- paste0("tp", i-1, "_dementia")
this_dementia <- paste0("tp", i, "_dementia")
last_alive <- paste0("tp", i-1, "_alive")
this_alive <- paste0("tp", i, "_alive")
ind <- which(dat[[last_alive]] == 1)
dat[[this_alive]][ind] <- ! (! dat[[last_alive]][ind] | rbinom(length(ind), 1, dat$death_risk[ind]))
ind <- which(dat[[this_alive]] == 1)
dat[[this_dementia]][ind] <- (dat[[last_dementia]][ind] | rbinom(length(ind), 1, dat$dementia_risk[ind]))
print(table(dat[[this_dementia]]))
print(table(dat[[this_alive]]))
dat[[this_alive]][is.na(dat[[this_alive]])] <- dat[[last_alive]][is.na(dat[[this_alive]])]
dat[[this_dementia]][is.na(dat[[this_dementia]])] <- dat[[last_dementia]][is.na(dat[[this_dementia]])]
}
datl <- dat %>% pivot_longer(
c(ends_with("alive"), ends_with("dementia"))
) %>%
tidyr::separate(name, sep="_", into=c("tp", "measure")) %>%
mutate(tp = gsub("tp", "", tp) %>% as.numeric()) %>%
pivot_wider(
id_cols=c(id, sex, death_risk, dementia_risk, tp),
names_from=measure,
values_from=value
)
datl %>%
group_by(sex, tp) %>%
summarise(alive=sum(alive)/n()) %>%
ggplot(., aes(x=tp, y=alive)) +
geom_line(aes(colour=as.factor(sex)))
datl %>%
group_by(sex, tp) %>%
summarise(dementia=sum(dementia)/n()) %>%
ggplot(., aes(x=tp, y=dementia)) +
geom_line(aes(colour=as.factor(sex)))
datl %>%
dplyr::filter(alive==1) %>%
group_by(sex, tp) %>%
summarise(dementia=sum(dementia)/n()) %>%
ggplot(., aes(x=tp, y=dementia)) +
geom_line(aes(colour=as.factor(sex)))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment