Skip to content

Instantly share code, notes, and snippets.

View emhart's full-sized avatar

Edmund Hart emhart

View GitHub Profile
@emhart
emhart / foodwebplot.R
Created December 20, 2011 21:44
A function to plot food webs with examples
###############Food web plotting function################
### Date: 12/13/2011
### Author: Edmund Hart (edmund.m.hart@gmail.com)
### Description: A function to create grahps of trophic networks using ggplot2
### Plots food webs in a circular graph
### requires ggplot2
#########################################################
library(ggplot2)
########## support function create.xy
@emhart
emhart / ecoODE.R
Created March 28, 2013 20:49
Examples of ODE's in R from classic ecological models with a simple M-W extension on I
#' Description: Code using deSolve for some simple ecological ODE models
#' There are 3 examples, one is a simple LV Pred-Prey example with plots
#' The next is a Macarthur-Wilson model with plots of both the species curve and the equlibrium points
#' The last plot is a quick crack at putting stochasticity into the model by adding oscillations in I with t.
#' LV code is modified from a blog post here: http://assemblingnetwork.wordpress.com/2013/01/31/two-species-predator-prey-systems-with-r/
#' Other additions are my own
#'
#' Author: Edmund Hart
#' Date: 3/28/2013
#' E-mail: edmund.m.hart@gmail.com
@emhart
emhart / cointoss.R
Created December 11, 2018 18:52
Simple fair coin toss code
library(ggplot2)
### Imagine a coin is flipped 100 times
### You get 60 heads
### How can you assess if the coin is fair or not?
# Set up the plot
binom_density <- dbinom(0:100,100,.5)
flips <- data.frame(flip_count = 0:100, density = binom_density)
@emhart
emhart / keypress.py
Last active October 6, 2018 00:10
sonder_keypad
num2letter = {"2":"abc", "3":"def", "4":"ghi", "5":"jkl", "6":"mno",
"7":"pqrs", "8":"tuv", "9":"wxyz"}
def keys2letters(key_presses):
results = [x for x in num2letter[key_presses[0]]]
if len(key_presses) <= 1:
return(results)
else:
for x in key_presses[1:]:
@emhart
emhart / theme_blank.R
Last active December 22, 2017 07:56
A blank ggplot2 theme
theme.blank = function(size=12) {
theme(
#axis.line=element_blank(),
axis.text.x=element_text(size=size),
axis.text.y=element_text(size=size),
#axis.ticks.y=element_text(size=size),
# axis.ticks=element_blank(),
axis.ticks.length=unit(0.1, "lines"),
axis.ticks.margin=unit(0.5, "lines"),
axis.title.x=element_text(size=size*1),
@emhart
emhart / general_housing_model.R
Created November 28, 2017 05:14
Quasi general housing model to help you decide if it's better to rent or buy, has some things specific to California in it (like how property taxes are assessed)
### Define terms to understand investment potentional
### This function will simulate scenarios to compare renting to buying and allow us to make an informed financial decision
## Parameters of the model (some are hard coded but can still be changed)
## r - Annual appreciation rate
## term - Term you want to own the house for (in months)
## house_cos - cost of the house in dollars
## ir - Interest rate of my mortgage
## closing - total closing costs
@emhart
emhart / ted_housing_model.R
Last active November 28, 2017 05:08
Housing model simulation to help me decide if we should buy or not...
### Define terms to understand investment potentional
### This function will simulate scenarios to compare renting to buying and allow us to make an informed financial decision
## Parameters of the model (some are hard coded but can still be changed)
## r - Annual appreciation rate
## term - Term you want to own the house for (in months)
## house_cos - cost of the house in dollars
## ir - Interest rate of my mortgage
## closing - total closing costs
@emhart
emhart / birthday_song.R
Created August 8, 2016 15:45
Happy Birthday Oliver!
library("dplyr")
library("audio")
notes <- c(A = 0, B = 2, C = 3, D = 5, E = 7, F = 8, G = 10)
pitch <- "D D E D G F# D D E D A G D D D5 B G F# E C5 C5 B G A G"
duration <- c(rep(c(0.75, 0.25, 1, 1, 1, 2), 2),
0.75, 0.25, 1, 1, 1, 1, 1, 0.75, 0.25, 1, 1, 1, 2)
bday <- data_frame(pitch = strsplit(pitch, " ")[[1]],
duration = duration)
@emhart
emhart / refexp.r
Created November 16, 2012 21:23
Blog post on random effects in mixed models
library(lme4)
library(ggplot2)
#create some levels
levs <- as.factor(c("l1","l2","l3","l4","l5"))
#set the factor means
f_means <- c(6,16,2,10,13)
# set individual as a factor
### "Smart brute force" algorithm. Works in most mortal situations.
### Works by looping through a matrix that is m by k and putting numbers in
### Is aware of the top and left cell, and works by sampling based on the probabilities of
### numbers that are left in the set of valid possible numbers, e.g. those not excluded by the neighborhood rules (von Neumann)
### Parameters:
### k -- The number of rows
### m -- The number of columns
### n -- 1:N vector of desired outcomes (plants in the blog post), e.g. if N = 3, your n parameter would be n <- 1:3
### maxiter -- The maximum number of iterations you want to use in the algorithm (default is 100.