Skip to content

Instantly share code, notes, and snippets.

View gweissman's full-sized avatar

Gary Weissman gweissman

View GitHub Profile
@gweissman
gweissman / entropy_bs_ci.r
Last active February 12, 2020 00:59
Calculate the entropy of a set of predictions and estimate 95% confidence intervals.
# Libraries
library(gmish) # devtools::install_github('gweissman/gmish')
# Size
N <- 1000
# Softmax helper function
sm <- function(x) exp(x)/sum(exp(x))
# Make some predictions
@gweissman
gweissman / net_diff_sim.Rmd
Last active September 11, 2018 15:04
Network and diffusion process imulations
---
title: 'Aim 2: Simulated prelim data for Roybal Proposal'
author: "Gary E. Weissman, MD, MSHP"
date: "8/29/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(igraph)
@gweissman
gweissman / one_hot_encode_var.r
Last active December 21, 2018 02:46
one-hot encodes a variable and returns a data.table with new columns
# A helper function to one-hot encode a _single_ variable
# Assumes data.table column or vector input
# Returns a data.table with x columns where x is number of levels
ohev <- function(vars, drop_ref = TRUE) {
vname <- deparse(substitute(vars))
lvls <- unlist(unique(vars))
tmp_list <- list()
for (lev in lvls) {
tmp_list[[make.names(paste0(vname,'_',lev))]] <- as.numeric(vars == lev)
}
@gweissman
gweissman / highlight_giant.r
Last active May 24, 2018 15:11
How to highlight the giant component of a graph using igraph
# highlight communities and clusters in an igraph plot
library(igraph)
g <- erdos.renyi.game(50, .02)
# plot regular
plot(g)
@gweissman
gweissman / scrape_icd10_cards_proc_codes.r
Last active December 2, 2021 16:34
Code to scrape the ICD 10 cardiac procedure codes from the AHRQ PQI v6.0 Appendix B 50+ page pdf file
# extract the ICD10 Cardiac procedure codes from PDF
# Appendix B of AHRQ PQI v6.0
require(pdftools)
download.file(url = 'http://www.qualityindicators.ahrq.gov/Downloads/Modules/PQI/V60/TechSpecs/PQI_Appendix_B.pdf',
destfile = 'PQI_Appendix_B.pdf')
raw_text_pages <- pdf_text('PQI_Appendix_B.pdf')
raw_text <- paste(raw_text_list, collapse = '')
tokens <- unlist(strsplit(raw_text, split = "\\s"))
tokens_filt <- tokens[ ! tokens %in% c('APPENDIX', '(ACSCARP)')]
@gweissman
gweissman / convert_icd9to10_cancer_dx.r
Last active October 4, 2018 15:31
Convert a list of icd9 codes associated with poor prognosis cancers to icd10 codes using the 2016 General Equivalency Mappings from CMS
# Convert ICD 9 to ICD 10 codes based on the CMS General Equivalency Mappings 2016
# https://www.cms.gov/Medicare/Coding/ICD10/2016-ICD-10-CM-and-GEMs.html
library(data.table)
library(icd)
gem <- fread('~/Desktop/SCIP - Study of Critical Illness Pathways/2018-redo/final_code/2016-General-Equivalence-Mappings/2016_I9gem.txt',
colClasses = rep('character',3))
setnames(gem, c('icd9', 'icd10', 'flags_composite'))
@gweissman
gweissman / acsc.R
Created October 28, 2015 18:30
Detect presence of ambulatory care sensitive conditions (ACSCs) by AHRQ criteria in R script
# this script finds ambulatory sensitive conditions (ACSCs) in ICD-9 codes
# the data are based on the appendices from
# http://www.ahrq.gov/downloads/pub/ahrqqi/pqiguide.pdf (Appendix A)
# usage: acsc(list_of_icd9_dx_codes, list_of_drgcodes, list_of_icd9_proc_codes)
# returns: TRUE or FALSE
# or set getlist = TRUE to get the list of which ACSCs were found
# NB. that some of the definitions for these PQIs exclude hospital transfers
# paripartum admissions, or certain age groups, etc.
# This script ***only*** assess ACSC status by ICD-9 code.
@gweissman
gweissman / sample_mm_ac.R
Created April 17, 2012 02:26
sample calculating mixing matrix and assortativity coefficient with igraph
# sample calculating mixing matrix and assortativity coefficient with igraph
require(igraph)
set.seed(12)
# create a random graph
g <- random.graph.game(1000,0.15)
# assign some characteristics
V(g)$color <- c('red','white','blue','orange','green')
@gweissman
gweissman / assortativity_coefficient.R
Last active October 29, 2015 15:47
Calculate Newman's assortativity coefficient for a mixing matrix
# calculate the assortativity coefficient for a mixing matrix of a graph
# ref: MEJ Newman, 'Mixing patterns in networks', Phys Rev E 67, 026126 (2003)
#
# define assortativity coefficient as
# trace (m) - sum (m^2)
# ac = -------------------------
# 1 - sum (m^2)
#
# where m is the mixing matrix of a graph
@gweissman
gweissman / mixing_matrix.R
Created April 17, 2012 01:18
Calculate mixing matrix in igraph by vertex characteristic
# calculate the mixing matrix of in igraph graph object 'mygraph', by some vertex attribute 'attrib'
# can change the default use.density=FALSE to return a matrix with raw number of edges rather than density
mixmat <- function(mygraph, attrib, use.density=TRUE) {
require(igraph)
# get unique list of characteristics of the attribute
attlist <- sort(unique(get.vertex.attribute(mygraph,attrib)))