Skip to content

Instantly share code, notes, and snippets.

@dill
dill / get_res.R
Created May 31, 2015 00:05
UN Resolutions downloader
# get UN resolutions
# Annoyingly the UN website is not very crawl-able.
# Using RSelenium you can download Resolution data
library(RSelenium)
library(stringr)
# download directory
dl_dir <- path.expand("~/Downloads/UN69/")
@dill
dill / rverbr.R
Last active April 15, 2020 22:03
Make JSON for @RverbR
## This is a very silly script to get JSON data for an R twitter bot that produces
## tweets of the form "verb that noun" using verbs from package:base and nouns from
## built in R data.
##
## It uses @v21's http://cheapbotsdonequick.com/
##
## David L. Miller 2015 MIT license
## update from Nick Golding 2018
cat("{\n")
@dill
dill / failwhale
Created June 12, 2015 22:16
a red failwhale for @awegalloway
library(rphylopic)
# get a (sperm) whale! (this is "Physeter catodon" by Noah Schlottman)
# http://phylopic.org/image/dc76cbdb-dba5-4d8f-8cf3-809515c30dbd/
whale <- image_data("dc76cbdb-dba5-4d8f-8cf3-809515c30dbd", size=512)
# failwhale
image(whale[[1]][,,4])
@dill
dill / verhoef.R
Created August 28, 2015 03:18
ver Hoef and Boveng (2007) plots
# makes plots (somewhat) like those in ver Hoef and Boveng (2007)
# Jay M. Ver Hoef and Peter L. Boveng 2007. QUASI-POISSON VS. NEGATIVE BINOMIAL REGRESSION: HOW SHOULD WE MODEL OVERDISPERSED COUNT DATA? Ecology 88:2766–2772. http://dx.doi.org/10.1890/07-0043.1
# http://www.utstat.utoronto.ca/reid/sta2201s/QUASI-POISSON.pdf
# calling with something like:
# par(mfrow=c(1,2))
# # define the breaks
@dill
dill / mrf-time.R
Created September 4, 2015 18:50
Example of Markov Random Field with temporal interaction
# Markov Random Fields with temporal interactions
# David L Miller 2015
# Released under MIT license, YMMV
# example from ?mgcv::smooth.construct.mrf.smooth.spec
library(mgcv)
## Load Columbus Ohio crime data (see ?columbus for details and credits)
data(columb) ## data frame
@dill
dill / wiggly.R
Last active October 9, 2015 14:43
Goofy animation of integrating 2nd derivatives for a GAM penalty
# David L Miller 2015, MIT license
library(numDeriv)
library(animation)
# function taken from the "Gu & Wahba 4 term additive example" from mgcv::gamSim
f2 <- function(x) 0.2*x^11*(10*(1-x))^6+10*(10*x)^3*(1-x)^10
xvals <- seq(0,1,len=100)
@dill
dill / example.R
Last active August 27, 2016 17:00
Visualising concurvity between terms in a GAM
# example from ?mgcv::concurvity
library(mgcv)
## simulate data with concurvity...
set.seed(8);n<- 200
f2 <- function(x) 0.2 * x^11 * (10 * (1 - x))^6 + 10 *
(10 * x)^3 * (1 - x)^10
t <- sort(runif(n)) ## first covariate
## make covariate x a smooth function of t + noise...
x <- f2(t) + rnorm(n)*3
@dill
dill / base_graphics.R
Last active October 14, 2015 19:41
Comparison of plotting covariates against distance, base graphics vs. ggplot2
# plot of size versus distance and sea state versus distance
# w. linear model and LOESS smoother overlay
# base graphics
par(mfrow=c(1,2))
plot(distdata[c("size","distance")], xlab="Group size", ylab="Distance (m)",pch=19,col=rgb(0,0,0,0.4), cex=0.6)
# increase span from default 0.75 for slightly smoother curve
lo <- loess(distance ~ size, distdata, span=0.8)
lmm <- lm(distance ~ size, distdata)
preddat <- data.frame(size=seq(0,8,1))
### analysis of ribbon seal data using a frequentist
### approach to a GAM
### David L Miller dave@ninepointeightone.net
### License: GNU GPL v3
# load data from
# https://github.com/pconn/SpatPred/blob/master/SpatPred/data/Ribbon_data.rda
load("Ribbon_data.rda")
@dill
dill / extra_plot.R
Last active October 29, 2015 13:29
Make GAM term plots that extend outside the range of the data to show how bad extrapolation is for Laura M
load("best_model.Rdata")
library(dsm)
library(raster)
# lazily get the plot data for the rug plot
plotdat <- plot(M)
# load the raster and mudge it into the format I want
dists <- stack("NA_Shore_Dist_10km_mean_10km.img")
dists <- as.data.frame(dists)