Skip to content

Instantly share code, notes, and snippets.

View docsteveharris's full-sized avatar
💭
I may be slow to respond.

Steve Harris docsteveharris

💭
I may be slow to respond.
View GitHub Profile
@docsteveharris
docsteveharris / drug_freq.R
Created May 23, 2016 11:10
Snippet to check median number of drugs administered per drug per site across all patients
# calculate average number of drug doses per patient
col.names.drugs <- names(Filter(function(y) (!is.null(y) && y=="Drugs") , map(dict, "Classification1")))
tdt <- copy(data2d.wide)
tdt[, id:= paste0(site, "_", episode_id)]
setkey(tdt, id)
dim(tdt)
names(tdt)
col.names <- col.names.drugs[col.names.drugs %in% names(tdt)]
tdt[1:10,print(.SD), by=id, .SDcols=col.names]
drug.doses <- tdt[,lapply(.SD, function(x) sum(ifelse(x=="NA",as.integer(NA),1L), na.rm=TRUE)), by=.(site, id), .SDcols=col.names]
@docsteveharris
docsteveharris / ggMMplot.R
Last active February 16, 2017 16:04
Mosaic plot using ggplot
#' @title Mosaic plot using ggplot.
#'
#' @description
#' Creates a mosaic plot where the dimensions of the cells of a
#' confusion matrix represent their marginal proportions.
#'
#' @details
#' Credit for initial iteration to
#' [Edwin](http://stackoverflow.com/a/19258045/992999)
#' This version adds color brewer options and tidies the labelling
@docsteveharris
docsteveharris / MOR.R
Created November 28, 2015 00:34
Function to calculate the median odds ratio
# via https://stat.ethz.ch/pipermail/r-sig-mixed-models/2008q2/000874.html
# Calculate the median odds ratio
# -------------------------------
MOR <- function(my.var, digits = 2)
{ # MOR arguments: my.var = variance associated with level 2 clustering variable
# digits = number of decimal places to which MOR value will be rounded.
Median.OR <- round(exp(sqrt(2*my.var)*qnorm(.75)), digits)
paste("Median Odds-Ratio (MOR) = ", Median.OR) }
/*
## Create a rug plot
_____________________
CreatedBy: Steve Harris
CreatedAt: 111291
ModifiedAt: 120701
Log
@docsteveharris
docsteveharris / derive_sofa.R
Created February 11, 2015 12:54
Derive SOFA score
gen.sofa.c <- function(bpsys, bpdia, rxcvs_drug=NULL, rxcvs_dose=NULL) {
rx1 <- c("Adrenaline", "Noradrenaline")
rx2 <- c("Dopamine")
rx3 <- c("Vasopressin")
rx <- c(rx1, rx2, rx3, "Other")
# Print these descriptions so you know what you have passed to the function
bpmap <- round(bpdia + (bpsys - bpdia) / 3)
@docsteveharris
docsteveharris / charlson.score.R
Created November 27, 2014 12:13
Create Charlson Score
score.charlson <- function(data) {
# data is a datatable containing the columns you need
# identify the columns with pmh in the name
require(data.table)
charlson.values <- data[,.(pmhmi, pmhhf, pmhpvd, pmhcvd, pmhdem, pmhcopd, pmhcopd, pmhctd, pmhud, pmhmld, pmhdm, pmhhemi, pmhckd, pmhdmend, pmhtumour, pmhleuk, pmhlymph, pmhsld, pmhmets, pmhaids)]
charlson.matrix <- as.matrix(charlson.values)
# Now I need to convert the NA's to 0 and >0 to 1 in matrix
charlson.matrix <- apply(charlson.matrix, 2, function(x) ifelse(is.na(x), 0, ifelse(x>=1,1, 0)))
# Now create a vector of scores
# Note that the order of the columns selected above *must* match the order of the scores here
@docsteveharris
docsteveharris / qqplot.R
Last active August 29, 2015 14:10
qqplot in R
# Test data: 1000 patients with two different age distributions
tdt <- data.table(age.old=rnorm(1000, mean=65, sd=15), age.young=rnorm(1000,mean=55,sd=10))
# Function to compare two distributions using qplot
qqplot <- function(x,y, data=wdt, n=100) {
lab.x = x
lab.y = y
x <- with(data, get(x))
x <- sapply(seq(0,1,1/n), function(q) quantile(x, q, na.rm=TRUE))
y <- with(data, get(y))
@docsteveharris
docsteveharris / Asset.py
Created July 14, 2014 16:11
24 hour clock time regex with option separator
time24_regex = re.compile( r"""\b ([01]?\d|2(?=[0-3])\d) [:|.]? ([0-5]?\d) \b""", re.VERBOSE)
@docsteveharris
docsteveharris / Asset.js
Created July 14, 2014 16:09
Load data in d3
var dataset; // declare a global var to hold data
d3.csv("data/univar.csv", function(error, data) {
if(error) {
console.log(error);
}
else {
console.log(data);
}
#! /usr/bin/python
# author: Steve Harris
# Convert short nvAlt image links to full path for previewing in Marked
# Todo
# ====
# - move the pathto line to a variable
# - post this script as a gist