Skip to content

Instantly share code, notes, and snippets.

@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 / watchr.R
Created September 19, 2012 21:21
watch a directory for changes to .Rmd (knitr+markdown files) and if any change, knit them. Best used with Marked by Brett Terpstra (set it to watch the .md files that knitr will generate).
# watch all .Rmd files in a directory, if any change, knit them
library(knitr)
# use watch from Hadley's testthat package
library(testthat)
watchr<-function(path){
# the path supplied here can just be something like "./"
@dill
dill / INSTALLpqR.md
Last active September 10, 2017 07:51
Installing Radford Neal's pqR on Mac OS 10.8. More information on pqR can be found at: http://radfordneal.wordpress.com/ and https://github.com/radfordneal/pqR

Installing pqR on Mac OS X (10.8)

** THIS DOESN'T WORK ANY MORE ** Recent updates to XCode seem to have stopped this from working. I am attempting to get this working again. Updates will be posted here.

Use the right version of gcc

The version of gcc that ships with Mac OS X doesn't have openMP enabled, which means you can't use "helper threads", which make pqR super-fast. So, we first need to install that and get OS X to use it...

Get homebrew so you can install a newer version of gcc, then:

@dill
dill / alarm.sh
Last active December 24, 2015 20:59
alarm clock
#!/bin/bash
# alarm clock script
#
# requires: get-iplayer
# git clone git@github.com:dinkypumpkin/get_iplayer.git
# get_iplayer --prefs-add --rtmp-tv-opts="--swfVfy=http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf"
# need to install rtmpdump
@dill
dill / fw2dot.R
Created January 27, 2014 20:48
Build a GraphViz compatible file showing which functions call each other in an R package.
# use MVB's foodweb to build a graphviz-compatible "dot" file
# this can then be used to generate a svg of the graph using:
# $ dot -Tsvg file.dot -o file.svg
# example usage:
# fw2dot("mrds","mrds.dot")
library(mvbutils)
fw2dot <- function(package.name,filename){
@dill
dill / distanim.R
Last active November 25, 2023 06:20
Animation of a simple distance sampling survey.
# animate a (very boring) distance sampling survey
# watch a gif at: http://converged.yt/distance-animation.gif
# written by David Lawrence Miller, with tweaks from Eric Rexstad
# released under the MIT license
library(animation)
# make sure that we get the same result every time...
set.seed(1234)
@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 / 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

@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