  Dr. Mark Gardener Community Ecology: Analytical Methods Using R and Excel

# Writer's Bloc

On this page you can find out about my latest writing project. I'll post updates on progress, tables of contents and also some of the R scripts (and possibly Excel spreadsheets) I am developing in support of the new book. I'll try to keep the material reasonably up to date.

The Writer's Bloc homepage contains a table of contents and an index of the pages that contain custom R commands and R scripts.

Community Ecology: analytical Methods Using R and Excel

Chapter 7. Diversity – Species Richness

Estimating species richness from a community data set and plotting the log-linear model

function: sr_est()

Top

## Chapter 7. Diversity – Species Richness

### Species accumulation and estimating species numbers

Species accumulation curves can be used to compare the diversity properties of communities. The simplest form of species accumulation curve is derived by plotting the species richness against the number of samples. However, most commonly you use a randomisation method where you repeatedly subsample your data and then find the mean species area curve. You can use this approach to estimate the species richness of a sample (i.e. extrapolate for unseen species).

The specaccum() command in the vegan package can create a species accumulation curve that you can then use with a log-linear model to estimate species richness. I worte a custom function to do this and also set the result to a custom class, "srest", which can be used with print(), summary() and plot() commands.

```## Species Richness Log estimator
## Mark Gardener 2012
## www.dataanalytics.org.uk```
```sr_est <- function(accum) { # start function

# accum = vector of richness accumulation,
#         or community data frame (cols = spp, rows = samples),
#         or result of specaccum() command

if(inherits(accum, c("data.frame", "matrix")) == TRUE) { # what to do if regular data
require(vegan)
accum <- specaccum(accum, method = "collector")\$richness
}```
``` if(inherits(accum, "specaccum") == TRUE) { # what to do if specaccum
accum <- accum\$richness
}```
`ns <- accum # make vector of new species, begin with first value`
```for(i in 2:length(accum)) {        # start loop
ns[i] <- accum[i] - accum[i-1] # add new species to vector
} # end loop```
`names(ns) <- NULL # ensure no names are carried over from previous result`
```      csl <- log(accum, base = 10) # log of cumulative species
nsl.lm <- lm(ns ~ csl)          # linear model of new spp against log of accum spp
m <- coef(nsl.lm)       # the slope
c <- coef(nsl.lm)       # the intercept
cm <- abs(c/m)
names(cm) <- NULL                  # remove names
sr.est <- 10^cm                 # estimated species richness```
```## Make result object
## data frame for plotting (cum species, new species, log of cum spp)

sre <- data.frame(cum.spp = accum, new.spp = ns, log.cs = csl)```
```## model result
model <- c(c, m)                 # the linear model
names(model) <- c("Intercept", "Slope") # names of items```
```## Final estimate of species richness
sp.est <- sr.est
names(sp.est) <- "Estimated Species Richness"```
```## the complete result object
result <- list(data.model = sre, coefficients = model, species.estimate = sp.est)
class(result) <- "srest" # the class will be used for plot, print and summary commands```
```## Store result
return(result) # save all results```
`} # end function`
```## Plot method for species richness estimator
## Mark Gardener 2012
## www.dataanalytics.org.uk```
```plot.srest <- function(srest, col.points = "black",
col.line = "black", lwd.line = 1,
lwd.points = 1, ...)               {

# srest = result of species richness estimation from sr_est```
```  plot(new.spp ~ log.cs, data = srest\$data.model,
col = col.points, lwd = lwd.points, ...)

abline(srest\$coefficients, col = col.line, lwd = lwd.line, ...)```
```} # end of plot subroutine

## Print method for species richness estimator
## Mark Gardener 2012
## www.dataanalytics.org.uk    ```
```print.srest <- function(srest, digits = getOption("digits")) { # start function

# srest = result of species richness estimation via sr_est```
`print(srest\$species.estimate, digits = digits)`
`} # end of print routine`
```## Summary method for species richness estimator
## Mark Gardener 2012
## www.dataanalytics.org.uk```
```summary.srest <- function(srest, digits = getOption("digits")) { # start function

# srest = result of species richness estimator via sr_est```
```  cat("Model Data:\n")
print(srest\$data.model, digits = digits)
cat("\nModel coefficients:\n")
print(srest\$coefficients, digits = digits)
cat("\n")
print(srest\$species.estimate, digits = digits)```
`} # end function`
```## END
```

You can use the sr_est() command on several kinds of object.

• A community data object as a data.frame or matrix
• A vector of species richness accumulation
• A result from the specaccum() command (class "specaccum")

The main result is the estimated species richness. The summary() command will show the cumulative number of species, the number of new species and the log of the new species. You also get a summary of the linear model. The print() command simply shows the estimated species richness. The plot() command makes a scatter plot and adds a line of best-fit.

Top

Providing training for:

• Ecology
• Data analysis
• Statistics
• R The statistical programming language
• Data management
• Data mining     