This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# highlight communities and clusters in an igraph plot | |
library(igraph) | |
g <- erdos.renyi.game(50, .02) | |
# plot regular | |
plot(g) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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))) | |
NewerOlder