Skip to content

Instantly share code, notes, and snippets.

View emhart's full-sized avatar

Edmund Hart emhart

View GitHub Profile
@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 / 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 / prism_leaflet.R
Created October 29, 2015 05:36
Create javascript leaflet maps of prism data
library(devtools)
install_github("ropensci/prism")
library(leaflet)
library(raster)
library(prism)
## Set default path for raster files
options(prism.path = "~/prismtmp")
get_prism_normals(type="tmean",resolution = "4km",annual =T, keepZip=F)
@emhart
emhart / roxygen_test.R
Created October 29, 2015 04:28
Minimal example of royxgen2 issue
#' The length of a string (in characters).
#'
#' @param string input character vector
#' @return numeric vector giving number of characters in each element of the
#' character vector. Missing strings have missing length.
#' @seealso \code{\link{nchar}} which this function wraps
#' @export
#' @examples
#' str_length(letters)
#' str_length(c("i", "like", "programming", NA))
@emhart
emhart / quick_comp.R
Created October 14, 2015 18:09
Quick demo of how == is faster than %in%
ptm <- proc.time()
sum(1:10000000 == 4)
proc.time() - ptm
>user system elapsed
> 0.143 0.037 0.179
ptm <- proc.time()
sum(1:10000000 %in% 4)
proc.time() - ptm
@emhart
emhart / mention_ext.R
Last active September 11, 2015 11:52
Extract mentions from a tweet string using recursion
#' Extract mentions
#' @description extract all the mentions of other twitter users from a tweet
#' @param txt the text of your tweet
#' @export
mention_ext <- local({
ment <- rep(NA,150)
f <- function(txt){
if(!is.na(txt)){
@emhart
emhart / linear_gd.R
Last active August 29, 2015 14:14
Gradient Descent algorithm for a linear model of p parameters
#' A function to do gradient descent on arbitrarily sized linear models
#' @param Y a vector of response variables
#' @param X a design matrix of predictor variables
#' @param alpha the gradient descent step size
#' @param init the seed to start off for estimating parameters, must be the same size as the columns in X
#' @param failure the number of iterations after which to say convergence has failed
#' @param epsilon the difference in size between loss function iterations after which to say convergence has been reached.
linear_gd <- function(Y, X, alpha, init = runif(dim(X)[2],-10,10),failure = 10000,epsilon = 0.0001){
theta_new <- init