Skip to content

Instantly share code, notes, and snippets.

View timriffe's full-sized avatar

Tim Riffe timriffe

View GitHub Profile
@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 = "[email protected]")
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") |>
@timriffe
timriffe / deaths_share.csv
Last active November 28, 2024 12:47
decompose 80q0 by age and cause
age cause males females
0 001-008 I.Enfermedades infecciosas y parasitarias 4.674897119341564e-5 2.6033519553072625e-5
0 009-041 II.Tumores 7.012345679012346e-5 6.508379888268157e-5
0 042-043 III.Enfermedades de la sangre y de los órganos hematopoyéticos, y ciertos trastornos que afectan al mecanismo de la inmunidad 9.349794238683128e-5 9.11173184357542e-5
0 044-045 IV.Enfermedades endocrinas, nutricionales y metabólicas 2.220576131687243e-4 2.3430167597765365e-4
0 046-049 V.Trastornos mentales y del comportamiento 0 0
0 050-052 VI-VIII.Enfermedades del sistema nervioso y de los órganos de los sentidos 1.8699588477366256e-4 2.08268156424581e-4
0 053-061 IX.Enfermedades del sistema circulatorio 1.2855967078189302e-4 1.1715083798882682e-4
0 062-067 X.Enfermedades del sistema respiratorio 1.168724279835391e-4 6.508379888268157e-5
0 068-072 XI.Enfermedades del sistema digestivo 3.506172839506173e-5 1.3016759776536313e-5
@timriffe
timriffe / fertility_decomp.R
Created September 19, 2024 15:02
some fertility decompositions
library(tidyverse)
library(HMDHFDplus)
fert <- readHFDweb("USA",
"asfrRR",
username = Sys.getenv("us"),
password = Sys.getenv("pw"))
# ASFR as function of rates:
# decomposition result is just the age-specific rate differences
fert |>
@timriffe
timriffe / DemoDecomp_check_LTabr.R
Created August 8, 2024 03:44
checking consistency between LTabr() and Mxc2e0abr()
Mxc <- structure(c(1.93e-05, 1.02e-05, 5.84e-07, 7.03e-07, 5.57e-07,
3.75e-06, 8.07e-06, 1.21e-05, 2.24e-05, 3.27e-05, 5.34e-05, 6.14e-05,
8.53e-05, 0.000137, 0.000166, 0.000221, 0.000257, 0.000337, 0.000445,
0.000825, 0.000991, 0.001587473, 0.000106, 4.83e-06, 5.85e-07,
2.84e-07, 2.02e-07, 1.19e-06, 2.89e-06, 2.86e-06, 9.94e-06, 1.02e-05,
2.21e-05, 3.92e-05, 7.44e-05, 0.000113, 0.000183, 0.000312, 0.000458,
0.000939, 0.00176391, 0.003744596, 0.006769298, 0.011084971,
0, 0, 0, 0, 0, 3.12e-07, 3.89e-07, 6.73e-08, 6.58e-07, 2.9e-06,
8.29e-06, 1.8e-05, 5.81e-05, 0.000141, 3e-04, 0.000607, 0.00120023,
0.002377745, 0.004082495, 0.00780333, 0.014229324, 0.021679745,