Skip to content

Instantly share code, notes, and snippets.

View stevenworthington's full-sized avatar

Steven Worthington stevenworthington

View GitHub Profile
@stevenworthington
stevenworthington / ipak.R
Created July 25, 2012 19:44
Install and load multiple R packages at once
# ipak function: install and load multiple R packages.
# check to see if packages are installed. Install them if they are not, then load them into the R session.
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
@stevenworthington
stevenworthington / python_cheatsheets.txt
Created January 4, 2023 02:07 — forked from simplymanas/python_cheatsheets.txt
Collection of Python cheatsheet
https://docs.python.org/3/reference/index.html
https://docs.python.org/3/library/index.html
https://www.cs.put.poznan.pl/csobaniec/software/python/py-qrc.html
https://perso.limsi.fr/pointal/python:abrege
https://perso.limsi.fr/pointal/python:memento
https://perso.limsi.fr/pointal/_media/python:cours:abregepython.pdf
https://perso.limsi.fr/pointal/_media/python:cours:abregepython-english.pdf
https://perso.limsi.fr/pointal/_media/python:cours:mementopython3.pdf
@stevenworthington
stevenworthington / k_medoids_uncent_corr.R
Last active March 4, 2021 10:58
Calculate K-medoids using the uncentered correlation distance method
# 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)
@stevenworthington
stevenworthington / lme4_contrast_example.R
Created July 11, 2013 19:23
Example of how to create custom contrasts to test hypotheses in lme4 models.
# 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 / gist:cd5341455e4309ef204a
Created February 10, 2016 00:53 — forked from crsh/gist:4f9ce67f408611bc3974
Add events with time frame to plot.gantt()
# 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 January 5, 2016 20:59
Some plots to illustrate the differences between predict vs simulate in Lme4
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)
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)
# 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 / get_lat_lon_exif_pil.py
Created December 12, 2015 02:49 — forked from erans/get_lat_lon_exif_pil.py
Get Latitude and Longitude from EXIF using PIL
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 / centroid_perm.R
Created July 25, 2012 19:50
Permutation test for group differences using 3D coordinate data
# ===============================================================================
# Name : centroid_perm
# Original author : Steven Worthington (sworthington@iq.harvard.edu)
# Affiliation : IQSS, Harvard University
# Date (mm/dd/yyyy) : 06/14/2012
# Version : v0.8
# Aim : exact permutation test for group differences
# ===============================================================================
# Goal: