Skip to content

Instantly share code, notes, and snippets.

@JoFrhwld
Created February 10, 2015 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoFrhwld/cffbe8c152fa8265df83 to your computer and use it in GitHub Desktop.
Save JoFrhwld/cffbe8c152fa8265df83 to your computer and use it in GitHub Desktop.
library(babynames)
library(dplyr)
library(ggplot2)
lifetables %>%
mutate(decade = year)%>%
group_by(decade)%>%
mutate(prob_alive = lx/100000,
study_year = year + x)->prob_people
births %>%
mutate(decade = floor(year/10)*10)%>%
group_by(decade) %>%
summarise(births = median(births))->decade_births
entropy <- function(probs){
sum(probs * log2(1/probs), na.rm = T)
}
prob_people%>%left_join(decade_births)%>% filter(!is.na(births))%>%
mutate(people = (births/2) * prob_alive)%>%
group_by(study_year)%>%
mutate(prob_person = people/sum(people))%>%
summarise(entropy = entropy(prob_person))%>%
ggplot(aes(study_year, entropy))+
geom_line()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment