Skip to content

Instantly share code, notes, and snippets.

View timriffe's full-sized avatar

Tim Riffe timriffe

View GitHub Profile
@timriffe
timriffe / shapley.R
Created July 2, 2026 16:09
Shapley decomposition of HLE variation
load("Data/jdm_estimates.Rda")
library(tidyverse)
library(DemoTools)
calc_Lx <- function(lmx){
n <- length(lmx)
ax <- c(.5,rep(2.5,n-1))
qx <- lt_id_ma_q(nMx = exp(lmx),
nax = ax,
@timriffe
timriffe / plottingFunctions.R
Created May 22, 2026 17:10
CodHump lexis plot functions
# Author: tim
###############################################################################
#get packages sorted out
if( !"colorspace" %in% rownames(installed.packages())){
install.packages("colorspace")
}
# this is installed for certain grayscale tools in it.
# too bad we end up with this huge package just for that,
# but really the functions are convenient.
@timriffe
timriffe / wpp_reverse_survival.R
Last active October 6, 2025 08:35
an experimental script for first tricky step of single age basepop
# Idea: use wpp2022 or wpp2024 data
# 1) select year whose pop we want to adjust (2000)
# 2) get mort for 10 prior years
# 3) in cohort lines, get Lx, reverse-Sx, reverse cumprod of reverse-Sx
# 4) inflate year 2000 pop to fill Lexis space 1990-1999
# 5) calculate exposures by age and year 1990-1999, ages 15-49
# TODO: 1. merge with asfr, get births, use to get Bt series.
# 2. use same general idea to forward-survive Bt to Jan 1 2000, giving adjusted pop ages 0-9
# 3. make adjustments for mid-year census, in case given.
@timriffe
timriffe / openalex_second_order.R
Created March 29, 2025 09:44
a third script to see how many citations the citations of ego have, this time with openalex data
# note, our citation lookup needs an openalex paper id
# but you probably only have the doi. This does the translation
# this returns a full url, where the id is the last part
get_openalex_id_from_doi <- function(doi, email) {
base_url <- "https://api.openalex.org/works/doi:"
doi_encoded <- URLencode(doi, reserved = TRUE)
url <- paste0(base_url, doi_encoded, "?mailto=",email)
res <- httr::GET(url, httr::user_agent(email))
@timriffe
timriffe / opencite_second_order.R
Created March 24, 2025 18:05
a second script to get the same statistics from opencite.org (second-order citations)
# See also https://gist.github.com/timriffe/f820143e939f9262b3083122e1ce49ae
# for semantic scholar. Sorry output isn't harmonized
get_citing_papers_opencite <- function(doi){
openciteurl <- paste0( "https://opencitations.net/index/coci/api/v1/citations/",doi )
result <- rjson::fromJSON(file = openciteurl)
result |> lapply(as_tibble) |> bind_rows()
}
get_second_order_citations_opencite <- function(doi){
citing <- get_citing_papers_opencite(doi)
@timriffe
timriffe / semantic_scholar_second_order.R
Last active March 24, 2025 18:03
Directly estimate second order citations of a paper using semantic scholar API (just functions, for source()ing)
# get the semantic scholar method
library(httr)
library(jsonlite)
library(dplyr)
get_citing_papers <- function(doi, page_size = 100, max_results = 10000) {
base_url <- paste0("https://api.semanticscholar.org/graph/v1/paper/DOI:",
URLencode(doi, reserved = TRUE),
"/citations")
fields <- "citingPaper.paperId,citingPaper.title,citingPaper.citationCount"
@timriffe
timriffe / Fig1_dominguez_gonzalez_2024.R
Created March 20, 2025 17:37
A script to make Fig 1 for Antia Dominguez and Yolanda Gonzalez (2024) Self-rated health, time of residence and social determinants of health in immigrant populations: A complex relationship in groups of different origins in a Southern European region. Journal of Migration and Health, DOI: https://doi.org/10.1016/j.jmh.2024.100216
# download Excel from here:
# https://docs.google.com/spreadsheets/d/1zJPZ9uu6NpYruV8utJ-Cby4BGMpHw4XE/edit?usp=sharing&ouid=107044799582363262397&rtpof=true&sd=true
library(tidyverse)
library(readxl)
library(colorspace)
dat <- read_excel("Data/graficos_prev_ratio_antia_yolanda.xlsx", sheet = "table")
p <-
dat %>%
@timriffe
timriffe / compile_citations_OPIK.R
Created March 20, 2025 08:32
compiles publications & citation list for OPIK output for a given year range; requires some metadata in Drive
library(scholar)
library(googlesheets4)
library(tidyverse)
gs4_auth(email = "tim.riffe@gmail.com")
authors <- read_sheet(ss = "https://docs.google.com/spreadsheets/d/18RWKaMvkUOGp_URLwRu-3jTLUz2iI4y3oN_hu8MLoRg/edit#gid=0")
authors
pubsi <- list()
for (i in 1:nrow(authors)){
pubsi[[i]] <- get_publications(id = authors$scholar_id[i],
@timriffe
timriffe / cdc_bulk_download.R
Created January 31, 2025 12:03
downloading all cdc vital stats public use microdata files
library(tidyverse)
library(rvest)
library(httr)
options(timeout = 1e6)
cdc_url <- "https://www.cdc.gov/nchs/data_access/vitalstatsonline.htm"
pg <- read_html(cdc_url)
test <- html_attr(html_nodes(pg, "a"), "href")
@timriffe
timriffe / HFD_TFRadj.R
Last active January 12, 2025 09:45
how to caclulate HFD's Bongaarts-Feeney adjusted TFR using dplyr
library(HMDHFDplus)
library(tidyverse)
library(data.table)
# read and reshape births by year, age, order
Bxi <- readHFDweb("SWE","birthsRRbo",
username = Sys.getenv("us"),
password = Sys.getenv("pw"))|>
select(-OpenInterval, -Total) |>
pivot_longer(B1:B5p, names_to = "order", values_to = "Bxi") |>