Skip to content

Instantly share code, notes, and snippets.

View timriffe's full-sized avatar

Tim Riffe timriffe

View GitHub Profile
@timriffe
timriffe / download_icd10.R
Created February 21, 2022 19:02
Script to download and merge WHO ICD10 files
# these come from here: https://www.who.int/data/data-collection-tools/who-mortality-database
icd_base_url <-"https://cdn.who.int/media/docs/default-source/world-health-data-platform/mortality-raw-data/"
icd_files <- c("mort_country_codes.zip","morticd10_part1.zip","morticd10_part2.zip",
"morticd10_part3.zip","morticd10_part4.zip","morticd10_part5.zip")
for (i in 1:length(icd_files)){
url_i <- paste0(icd_base_url,icd_files[i])
local_i <- file.path(icd_files[i])
download.file(url_i, destfile = local_i, overwrite = TRUE)
}
@timriffe
timriffe / prevratio_II.R
Created July 21, 2021 10:58
another prevalence ratio plot, this one with more wrangling involved.
library(tidyverse)
library(readxl)
library(zoo)
IN <- read_excel("/home/tim/Data/grafico rp ambiental.xlsx",
sheet = 2,
range = "B6:F71") %>%
mutate(Variable = na.locf(Variable)) %>%
@timriffe
timriffe / prevratios.R
Created July 12, 2021 16:38
code for an ad hoc prevalence ratio dot plot
library(tidyverse)
library(readxl)
dat <- read_excel("Data/Gráficos.xlsx", sheet = "table")
p <-
dat %>%
ggplot(aes(y = origen,
x = Valor,
@timriffe
timriffe / ORplot.R
Last active June 10, 2021 10:38
ORplot example
library(tidyverse)
library(readxl)
IN <- read_excel("RAZONES DE PREVALENCIA.xlsx")[-c(1,2),-c(1,4,6,8)]
colnames(IN) <- c("Grupo","TABACO EN GENERAL_Hombres","TABACO EN GENERAL_Mujeres", "TABACO DE LIAR_Hombres","TABACO DE LIAR_Mujeres")
IN$Grupo[1] <- "General"
redux <-
IN %>%
@timriffe
timriffe / agg_eurostat.R
Last active May 27, 2021 10:34
aggregagte eurostat weekly deaths to years
library(eurostat)
library(tidyverse)
library(countrycode)
IN <- get_eurostat("demo_r_mwk_05")
db_eurs <-
IN %>%
mutate(time = as.character(time)) %>%
separate(time, sep = "W", into = c("year","week"), convert = TRUE) %>%
dplyr::filter(year >= 2016,
@timriffe
timriffe / make_adjacency_matrix.R
Created May 14, 2021 16:12
make a little adjacency matrix from a particular data format
conditions <- structure(list(idno = c(1, 2, 3, 4, 5), cvd = c(1, 1, 0, 1, 1
), depression = c(0, 1, 1, 0, 0), diabetes = c(0, 1, 0, 1, 1)), class = "data.frame", row.names = c(NA,
-5L))
data <- conditions
RS_adj_mat <- function(data, the_conditions = c("cvd","depression","diabetes")){
stopifnot(all(the_conditions %in% colnames(data)))
@timriffe
timriffe / LexisOpcionesParaUnai.R
Created May 6, 2021 07:14
unas maneras de visualizar una tabla Lexis de las tasas de fecundidad del Pais Vasco
library(tidyverse)
library(readxl)
library(colorspace)
# convertir a formato tidy y crear coordinatas centricas
ISF_tidy <-
read_excel("ISF_PC.xlsx", sheet = "simplified") %>%
pivot_longer(`2015-2011`:`1930-1926`, names_to = "YOB", values_to = "ASFR") %>%
filter(!is.na(ASFR)) %>%
@timriffe
timriffe / Sex_Ratio_CIs_for_Pietro.R
Created May 1, 2021 07:31
demonstrate approach to sex ratio confidence intervals
library(tidyverse)
# example: uses Ohio, March 1 (dunno why),
# and population is actually 2018, because I can't find 2020 pops yet.
# slightly less of a problem because I grouped to 5-year intervals.
X <-tibble(Sex = c(rep("f",21),rep("m",21)),
Age = c(seq(0,100,by=5),seq(0,100,by=5)),
Deaths = c(0.9, 1.5, 2.5, 4, 3.4, 5.6, 14.2, 24.8, 35.1, 63.9,
111.9, 213.5, 363.8, 566.4, 811.4, 1076.6, 1271.3, 1012.5, 1990.5,
@timriffe
timriffe / MortSmooth2d.R
Created March 9, 2021 09:41
example for the EW drug data
rm(list = ls())
getwd()
load("Data/Smoothing.RData")
library(MortalitySmooth)
library(ungroup)
# ?Mort2Dsmooth
x <- c(0,1,seq(5,85,by=5))
@timriffe
timriffe / calc_stable.R
Created February 16, 2021 21:00
estimate r, then calculate stable population structure.
library(DemoTools)
calc_stable <- function(nLx, asfr, Age_nLx, Age_asfr){
asfr_vec <- rep(0,length(Age_nLx))
names(asfr_vec) <- Age_nLx
asfr_vec[as.character(Age_asfr)] <- asfr
int <- age2int(Age_nLx)
int[length(int)] <- int[(length(int)-1)]
x <- Age_nLx + int / 2