Skip to content

Instantly share code, notes, and snippets.

View aammd's full-sized avatar

Andrew MacDonald aammd

  • Université de Sherbrooke
  • Montreal, Canada
View GitHub Profile
@aammd
aammd / loopfree.R
Created November 16, 2011 01:34
howtokillaforloop
# by Andrew MacDonald Nov 2011
## please add your own tips and tricks!
## the idea here is to demonstrate how for loops are used
## and then immediately demonstrate how they can be avoided.
## we'll begin by creating a fake dataset.
## This experiment is a 3x3 factorial with 2 replicates of each treatment combination.
## The scientist applied nitrogen and phosphorous to the soil
## then she measured two responses: plant growth and seed production.
@aammd
aammd / galls.R
Created December 7, 2011 01:58
simulations of gall damage
## a simulation of detecting effect size on gall parasitisim rates
## and occupancy.
## basically ask: how many larvae are in each gall (poisson)
## how many samples do you need to see the difference?
##
#first, a homemade function for the Zero-Inflated Negative Binomial distribution
rzinbinom <- function(n,mu,size,zprob){
ifelse(runif(n)<zprob,0,rnbinom(n,mu=mu,size=size))
}
@aammd
aammd / mathom.R
Created March 24, 2012 19:31
A simulation of size-dependent diversity effects in experiments that test for diversity
## simulation of experiment
## to test patterns of randomization
## first set up the relationships
## volume is a uniform variable
n.mathoms <- 30
@aammd
aammd / ch2.R
Created November 5, 2012 22:58 — forked from mabarbour/ch2
Code for recreating figures in Chapter 2
######### These are my attempts to incorporate McCann's models into R to help me interpret these food web models ######################
library(deSolve)
### Section 2.2.5
# parameters and state variables for the R-M model
r <- 1.0 # per capita rate of increase in resource
K <- 2.0
e <- 0.7
Ro <- 0.5
## file-scan.r
## Function to find a pattern in files with user-defined extension (.r by default)
## Returns file name and approximate row matches if positive.
## -------------------------------------------------------
## Author: Laura Tremblay-Boyer (l.boyer@fisheries.ubc.ca)
## Written on: March 26, 2013
## Time-stamp: <2013-03-26 16:19:28 Laura>
file.scan <- function(pattern, dir=getwd(), fpattern=NA, ftype=".r") {
@aammd
aammd / refexp.r
Created July 24, 2013 18:14 — forked from emhart/refexp.r
library(lme4)
library(ggplot2)
#create some levels
levs <- as.factor(c("l1","l2","l3","l4","l5"))
#set the factor means
f_means <- c(6,16,2,10,13)
# set individual as a factor
getmatch <<- function(x,str2match,...) {
unlist(regmatches(x,regexpr(str2match,x,...))) }
@aammd
aammd / local_maxima.md
Created August 19, 2013 23:08
a friend and I worked out a simple means to detect local maxima of a mixed frequency distribution -- useful for determining if populations have diverged or not!
x1 <- rnorm(100,5,sd=2)
x2 <- rnorm(200,15,3)
x3 <- c(x1,x2)

## calculate the density function 
den_x <- density(x3)]
## this can be plotted:
plot(den_x)
@aammd
aammd / eps.R
Created August 22, 2013 21:26
Writing publication-pretty *.eps files in R
eps <- function(file, onefile=FALSE, width=5.5, height = 5.5, horizontal=FALSE,paper="special", ...){
postscript(file=file,width=width,height=height,onefile=onefile,horizontal=horizontal,paper=paper,title=file,...)
par(bty='l')
}
@aammd
aammd / catcol.R
Last active December 29, 2015 11:19
a demostration of using catcolwise -- a function that creates a function.
## make a fake data frame
test <- data.frame(foo=gl(n=3,k=2,labels=c("low","med","high")),bar=rep(letters[1:2],3),zoop=runif(6))
## these are all factors
str(test)
## I want them to be characters, not factors, but I am too lazy to run as.character on every categorical factor
library(plyr)
test_char <- catcolwise(as.character)(test)