Skip to content

Instantly share code, notes, and snippets.

Steven Worthington stevenworthington

Block or report user

Report or block stevenworthington

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@stevenworthington
stevenworthington / gist:cd5341455e4309ef204a
Created Feb 10, 2016 — forked from crsh/gist:4f9ce67f408611bc3974
Add events with time frame to plot.gantt()
View gist:cd5341455e4309ef204a
# Function definition
add_event <- function(start, end = NULL, label = NULL, line = 0.5, las = 1, col = scales::alpha("steelblue", 0.5)) {
if(is.null(end)) {
end <- paste(start, "23:59:59")
start <- paste(start, "00:00:00")
}
start <- as.POSIXct(start)
end <- as.POSIXct(end)
@stevenworthington
stevenworthington / predict_vs_simulate.R
Created Jan 5, 2016
Some plots to illustrate the differences between predict vs simulate in Lme4
View predict_vs_simulate.R
library(lme4)
head(sleepstudy)
summary(sleepstudy)
sapply(sleepstudy, class)
# Fit model using the original data:
d <- sleepstudy
d$Subject <- factor(rep(1:18, each=10))
fm1 <- lmer(Reaction ~ Days + (Days|Subject), d)
@stevenworthington
stevenworthington / get_lat_lon_exif_pil.py
Created Dec 12, 2015 — forked from erans/get_lat_lon_exif_pil.py
Get Latitude and Longitude from EXIF using PIL
View get_lat_lon_exif_pil.py
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
def get_exif_data(image):
"""Returns a dictionary from the exif data of an PIL Image item. Also converts the GPS Tags"""
exif_data = {}
info = image._getexif()
if info:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
@stevenworthington
stevenworthington / facet_reg.R
Created Apr 20, 2015
ggplot regression example using facets
View facet_reg.R
data(mtcars)
library(ggplot2)
plot1 <- ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_smooth(method = "lm") +
facet_wrap(~ cyl) +
theme_bw()
View R_packages.R
x <- c(
'knitr', # A general-purpose package for dynamic report generation in R.
# 'sqldf', # For running SQL statements on R data frames, optimized for convenience.
'randomForest', # Classification and regression based on a forest of trees using random inputs.
'arm', # R functions for processing lm, glm, svy.glm, mer and polr outputs.
'ggplot2', # An implementation of the Grammar of Graphics.
'gridExtra', # misc. high-level Grid functions
'plyr', # Tools for splitting, applying and combining data.
'tree', # Classification and regression trees.
'gbm', # Generalized Boosted Regression Models
@stevenworthington
stevenworthington / caliper_text.R
Last active Aug 29, 2015
Extract blocks of text based on patterns
View caliper_text.R
# set working directory
dir_path <- "path_to_text_files"
setwd(dir_path)
# create vector of filenames
filenames <- list.files(dir_path)
# read in files to a list
docList <- lapply(filenames, scan, what = "character", sep = "\n")
View points_on_polygons.R
library(sp)
library(maptools)
# get North Carolina shape data
NC <- readShapePoly(system.file("shapes/sids.shp", package = "maptools")[1],
IDvar = "FIPSNO", proj4string = CRS("+proj=longlat +ellps=clrk66"))
# plot polygons
plot(NC, border = "blue", axes = TRUE, las = 1)
View extract_twitter_text.R
# list with character vectors
text <- list(a = "all day I play @sworth with R",
b = "all night I play @sworth with R")
# extract letters after "@" in a single character vector
sub("^.*@(\\w+).*", "\\1", text$a)
# extract letters after "@" in a list of character vectors
gsub("^.*@(\\w+).*", "\\1", text)
@stevenworthington
stevenworthington / lme4_contrast_example.R
Created Jul 11, 2013
Example of how to create custom contrasts to test hypotheses in lme4 models.
View lme4_contrast_example.R
# Note: requires loading the "socsub" data frame (not a bundled R dataset)
# ------------------------------------------------------------------------------------
# pairwise comparisons including interactions
# use lm model to get design matrix
model1 <- lm(agro.rec.tot ~ sex*ageclass + loggrpmem, offset = logtimeage, data = socsub)
@stevenworthington
stevenworthington / k_medoids_uncent_corr.R
Last active Sep 3, 2019
Calculate K-medoids using the uncentered correlation distance method
View k_medoids_uncent_corr.R
# example of calculating K-medoids using the uncentered
# correlation metric as a measure of distance
# 0) load data
data(mtcars)
# 1) create a distance matrix using the "cosine of the angle" method (aka, uncentered correlation)
You can’t perform that action at this time.