Skip to content

Instantly share code, notes, and snippets.

View gweissman's full-sized avatar

Gary Weissman gweissman

View GitHub Profile
@gweissman
gweissman / sample_quote_eval.R
Created April 13, 2012 15:45
Sample code using quote and eval in R
# write some code and sit on it
mycode <- quote(1 + 2 +3)
# some time later, at your convenience, evaluate the code
eval(mycode)
@gweissman
gweissman / dynamic_code.R
Created April 14, 2012 17:04
Dynamic code generation in R
# come up with some code elements
num1 <- 10
num2 <- 5
operators <- c('+','-')
# create a string of the desired code
@gweissman
gweissman / 4d_brain_teaser.R
Created April 15, 2012 01:13
Alternative approach to 4-digit brain teaser...
# alternative approach to teaser game
library(gtools)
library(lattice)
# get data set
load(url('http://www.babelgraph.org/data/brain_teaser_data.Rdata'))
ls()
# set operators
@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)))
@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 / 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 / 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 / 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 / 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 / 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)