Blog Archives

Quantitative link strength for APE cophyloplot

November 17, 2009
By
Quantitative link strength for APE cophyloplot

Just add a third column with link strength to the association matrix plotCophylo2 <- function (x, y, assoc = assoc, use.edge.length = use.edge.length, space = space, length.line = length.line, gap = gap, type = type, return = return, col = col, show.tip.label = show.tip.label, font = font) { if(ncol(assoc)==2) { assoc <- cbind(assoc,rep(1,nrow(assoc))) } res

Read more »

Welsh test by permutations

November 1, 2009
By
Welsh test by permutations

WelshPerm <- function(response,variable,nperm=999,...){ base <- oneway.test(response~variable,...) base.p <- base$p.value base.W <- base$statistic count <- 1 # Permutation loop for(i in 1:nperm){ SAMPLE <- sample(response) we...

Read more »

Sorting a matrix/data.frame on a column

October 30, 2009
By
Sorting a matrix/data.frame on a column

mat.sort <- function(mat,n) { mat),] <- mat return(mat) } a <- matrix(rnorm(100),ncol=10) mat.sort(a,1)

Read more »

Decimal log scale on a plot

October 30, 2009
By
Decimal log scale on a plot

R only does natural (neperian) log scales by default, and this is lame. Here is a simple code to do decimal log scale, pretty much a requirement for scientists… The force(x/y)lim options works for natural and log scales (for the later case, you need to specify the power of 10 that you want : c(-2,2)

Read more »

MultBar : Advanced multiple barplot with SEM

October 26, 2009
By
MultBar : Advanced multiple barplot with SEM

Producing this kind of graphs (below) in R can be a pain in the a*s. Here is a simple code that requires that data are presented in lists (see the example below). multbar <- function(list.of.lists,...,condnames=0,pal=colorRampPalette(c('grey','cornsilk')),seriesnames=0,legendpos='topleft',legh=TRUE,do.pty='s') { par(pty=do.pty,mgp=c(1.9,0.8,0),oma=c(0,0,0,0),mar=c(4,3,2,1),bg='transparent',bty='o',tck=0.02,yaxs='i') NofList <- length(list.of.lists) NofSubList <- length(list.of.lists]) if(condnames==0){condnames=c(1:NofList)} if(seriesnames==0){seriesnames=c(1:NofSubList)} dim.mat.treat <- (NofList+1)*NofSubList pos <- c(1:dim.mat.treat) pos <- matrix(pos,nrow=(NofList+1)) nbreaks

Read more »

GenEstim : A simple genetic algorithm for parameters estimation

October 18, 2009
By
GenEstim : A simple genetic algorithm for parameters estimation

The GenEstim function presented here uses a very simple genetic algorithm to estimate parameters. The function returns the best estimated set of parameters ($estim), the AIC ($information) at each generation, and the cost of the best model ($bestcost) at each generation. Results of running the program with a logistic function : Logis = function(x,p) p]/(1+p]*exp(-p]*x))

Read more »

Filled contour with log-log scale

October 15, 2009
By
Filled contour with log-log scale

A quick workaround to have a filled.contour plot with natural log10-log10 scale (instead of the default natural log scale) plotmat <- function(mat,main='',factor='M',MeasuredResponse='Coexistence') { X <- as.numeric(rownames(mat)) Y <- as.numeric(colnames(mat)) if(factor=='C') { Y <- Y/0.16 } rownames(mat) <- as.numeric(X) colnames(mat) <- as.numeric(Y) colorFun <- colorRampPalette(c("black","darkblue","blue","green", "orange",'yellow',"red","darkred",'white')) lX <- log(X, 10) lY <- log(Y, 10) pretty.X.at <-

Read more »

Colouring a 3D plot according to z-values

May 3, 2009
By
Colouring a 3D plot according to z-values

Here is a script that colors a 3D plot according to the z value (height) of each point. persp.withcol <- function(x,y,z,pal,nb.col,...,xlg=TRUE,ylg=TRUE) { colnames(z) <- y rownames(z) <- x nrz <- nrow(z) ncz <- ncol(z) color <- pal(nb.col) zfacet <- z + z + z + z facetcol <- cut(zfacet, nb.col) par(xlog=xlg,ylog=ylg)

Read more »

An algorithm to find local extrema in a vector

May 3, 2009
By
An algorithm to find local extrema in a vector

I spend some time looking for an algorithm to find local extrema in a vector (time series). The solution I used is to “walk” through the vector by step larger than 1, in order to retain only one value even when the values are very noisy (see the picture at the end of the post).

Read more »