# Appendix S1. R code to perform nonlinear beta regression. # Adair, EC, Laliberté, E and SE Hobbie. Estimating litter decomposition # rate in single-pool models using nonlinear beta regression. In review. # September 9, 2011 # Tested under R 2.13.1 # New function to generate random data from a beta distribution rbeta2 <- function(n, mu, phi, ...) { # Smithson and Verkuilen (2006) parameterization shape1 <- mu * phi shape2 <- (1 - mu) * phi rbeta(n, shape1, shape2, ...) } # New probability density function for beta distribution dbeta2 <- function(x,mu,phi,...) { # Smithson and Verkuilen (2006) parameterization shape1 <- mu*phi shape2 <- (1-mu)*phi dbeta(x,shape1,shape2,...) } # Generate decomposition data # time (days) time <- c(30, 60, 100, 150, 200, 250, 300, 350, 400, 500, 600, 700, 1000, 1500) # number of replicates reps <- 5 # full time vector with reps time <- rep(time, reps) # k = decomposition rate, with phi = 5 k <- 0.002 # Proportion of litter mass remaining propmass <- rbeta2(n = length(time), mu = exp(-k * time), phi = 5) # Plot data plot(propmass ~ time) # Suppose some values were = 0 and some were greater or equal to 1 # as sometimes occur in real decomposition data propmass2 <- propmass propmass2[sample(1:length(time), 5)] <- 0 propmass2[sample(1:length(time), 5)] <- c(1, 1.02, 1.06, 1.01, 1.11) # For beta regression, need to transform data to ]0, 1[ interval # Smithson and and Verkuilen (2006) transformation could be used sv2006 <- function(x, s = 0.5) { n <- length(x) x.max <- max(x) # compress between 0 and 1 # we need to divide by range # using theoretical minimum = 0 if (x.max > 1) x <- x / x.max # then convert data to if (any(x >= 1) | any(x <= 0) ) x <- (x * (n - 1) + s ) / n return(x) } # Apply SV transformation to propmass2 propmass.SV <- sv2006(propmass2) # Merge vectors into data frame (using the original propmass) xydat <- data.frame(x = time, y = propmass) # Get parameter estimates using maximul likelihood estimation # load bbmle library library(bbmle) # tested with version 1.0.3 # Refer to Bolker (2008) Ecological Models and Data in R # Chapter 7 for more details fit <- mle2(y ~ dbeta2(mu = exp(-k * x), phi = phi), data = xydat, start = list(k = 0.002, phi = 5), control = list(parscale = c(k = 0.001, phi = 1) ), method = "L-BFGS-B", lower = c(k = 0.00001, phi = 0.00001)) # Summary, including tests of coefficients summary(fit) # Extract parameters # k is the litter decomposition rate # phi is the precision parameter of the beta distribution params <- coef(fit)