Skip to content

Instantly share code, notes, and snippets.

@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 / plots.R
Created September 21, 2014 20:39
Transformation of variables in a GAM
# R plotting routines for the package mgcv (c) Simon Wood 2000-2010
## With contributions from Henric Nilsson
plot.mgcv.smooth <- function(x,P=NULL,data=NULL,label="",se1.mult=1,se2.mult=2,
partial.resids=FALSE,rug=TRUE,se=TRUE,scale=-1,n=100,n2=40,
pers=FALSE,theta=30,phi=30,jit=FALSE,xlab=NULL,ylab=NULL,main=NULL,
ylim=NULL,xlim=NULL,too.far=0.1,shade=FALSE,shade.col="gray80",
shift=0,trans=I,by.resids=FALSE,scheme=0,...) {
## default plot method for smooth objects `x' inheriting from "mgcv.smooth"
@dill
dill / abstract.md
Last active August 29, 2015 14:16
ESA 2015 abstract

Background/Question/Methods

Our understanding of a biological population can be greatly enhanced by modelling their distribution in space and as a function of environmental covariates. Such models can be used to investigate the relationships between distribution and environmental covariates as well as reliably estimate abundances and create maps of animal and plant distributions.

Here I'll give an overview of "density surface models", which consist of a spatial model of the abundance which has been corrected for uncertain detection via distance sampling methods. The spatial model consists of a generalised additive (mixed) model, which can include various components, such as smooth terms and random effects.

Results/Conclusions

In particular, I'll highlight modelling techniques that may be advantageous to applied ecologists: quantification of uncertainty in a two-stage model, correction for availability bias, alternative response distributions, autocorrelation and smoothing in areas with complex bound

@dill
dill / gist:2d1baac441db6fd60764
Last active August 29, 2015 14:18
useR! 2015 abstract

Building ecological models bit-by-bit

Many models that we build rely on combining several components, often representing different processes at work in the system. For example in ecology we may have a component for detectability of the species by observers, one for whether the animals are available, one describing their spatial distribution.

Defining such a complex model in R can result in writing a very long call to a particular function, which may or may not work at any point. Since these processes are often (conditionally) independent, we can separate these components in the likelihood and code. Using the + operator we can then combine them in a simple way that's easy for those doing the modelling to understand. Since each step produces an object, we can perform model validation, diagnostics and checking during construction.

Through an example applied to distance sampling, I'll show an example of this kind of approach to model building and propose some other situations where this kind of strategy may

@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 / 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 / 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 / dsm2-syntax.txt
Created September 14, 2012 20:30
New DSM syntax proposal
Proposal for new DSM syntax
The current syntax for dsm.fit is based on pretty old code, I think that it makes sense (especially as we add more functionality) to make a change to the way models are specified. The basic idea is to make things look more like mgcv::gam().
Currently things like the family and knots are specified using lists of lists, this involves lots of typing and doesn't make much sense if you're used to fitting GAMs using mgcv. This becomes even more of a pain if we want to start using gamm() for mixed models (I do anyway...)
Proposal:
dsm(formula, ddf.obj, segment.data, observation.data, engine=c("gam","gamm","glm"), convert.units=1, family=quasipoisson(link="log"), group=FALSE, ...)
@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))
@dill
dill / yosemiteR.md
Last active October 25, 2015 19:09
Getting Yosemite and R to play nice...

Getting Yosemite and R to play nice

Here are some tips on getting R development working with Yosemite. Contribute what you know below and I'll add it in.

homebrew

I went ahead and re-installed all of my homebrew. You can find out what you have installed with