Skip to content

Instantly share code, notes, and snippets.

View emhart's full-sized avatar

Edmund Hart emhart

View GitHub Profile
@emhart
emhart / gbif_climatemap2.R
Created July 23, 2013 23:20
More package combining.
### Get your climate data..
usmex <- c(273:284,328:365)
usmex.basin <- create_map_df(usmex)
temp.dat <- get_historical_temp(usmex, "decade" )
temp.dat <- subset(temp.dat,temp.dat$year == 2000 )
usmex.map.df <- climate_map(usmex.basin,temp.dat,return_map=F)
search_datatype<- function(datastype,term){
results <- noaa_datatypes(dataset=datatype)
s_parse <- function(x){
if(sum(grep(term,x[2],ignore.case=TRUE)) > 0){
return(c(x[1],x[2]))
}
}
out <- unlist(lapply(results$dataTypeCollection$dataType,s_parse))
out <- data.frame(matrix(out,ncol=2,nrow=(length(out)/2),byrow=T))
colnames(out) <- c("ID","Description")
@emhart
emhart / ggplotmapfix.R
Created September 18, 2013 16:12
Quickly showing how to fix extra lines in map
#### Doesn't work...
map <- map_data('world')
ggplot(data = map, aes(long, lat, group=group)) + geom_path() +coord_map(xlim=c(-170, -40), ylim=c(20, 83))
### Does work
test <- map
test <- subset(test,test$long > -170 & test$long < -40)
test <- subset(test,test$lat > 20 & test$lat < 83)
ggplot(data = test, aes(long, lat, group=group)) + geom_path()
@emhart
emhart / roauth_examp.R
Created September 20, 2013 15:23
How use use the twitter api with ROAuth and twitteR.
require(twitteR)
require(ggplot2)
library(ROAuth)
library(plyr)
### See this post on how to get your key and secret to put
### in below as strings: https://dev.twitter.com/discussions/631
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "http://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
@emhart
emhart / nls_search.R
Last active December 26, 2015 20:08
A simple function for searching for decent starting parameters of nls()
### NLS multiple starting points search
### formula: a standard R formula that you want to include in the nls() call
### inits: is a list of 2x1 vectors giving the upper and lower bounds of a uniform distribution to pull random starting values from
### The list should have names that correspond to the names of your parameters, similar to the way you would call them in nls()
### e.g. a good starting list would be: starts <- list(a=c(0,50),b=c(0,50))
### miter The maximum time to try and find a solution
@emhart
emhart / shelley.py
Last active December 26, 2015 22:29
Download lots of data for Shelley!
#!/usr/bin/python
import requests
import re
import sys
import os
def download_file(url,path):
if path[-1] == "/":
@emhart
emhart / per_exp.R
Last active December 28, 2015 17:38
A simple function to extract the percent of variance explained by a multiple regression model using SSp/SSt.
## Extract % variance explained from a multiple regression model for each parameter
## Inputs: a fitted model object
## Details: Uses a simple formula for each parameter as SSparam / SST
## Returns: a p x 2 dataframe with parameters as rows and name and % explained are the two columns
per_exp <- function(mod){
## Get sums of squares total (SSt)
squares <- anova(mod)
sst <- sum(squares$Sum)
var_exp <- vector()
@emhart
emhart / mendeley_fix.R
Created December 9, 2013 16:41
Fix for RMendeley for Zopim question
# Clean up old installs, removing old RMendeley and old ROAuth
install.packages("devtools")
library(devtools)
install_github("ROAuth","duncantl")
install_github("RMendeley","ropensci")
mc <- mendeley_auth()
@emhart
emhart / DFconvert.r
Created January 10, 2014 18:51
Convert from entire data types of a column via @sckott
function (d, colClasses)
{
colClasses <- rep(colClasses, len = length(d))
d[] <- lapply(seq_along(d), function(i) switch(colClasses[i],
numeric = as.numeric(d[[i]]), character = as.character(d[[i]]),
Date = as.Date(d[[i]], origin = "1970-01-01"), POSIXct = as.POSIXct(d[[i]],
origin = "1970-01-01"), factor = as.factor(d[[i]]),
as(d[[i]], colClasses[i])))
d
}
### "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.