Some R codes

Here are some of the R codes and packages I have been using in my research. These are simplified summaries to help you start applying them to your own data.  Let me know if you would like more explanations of their usage and interpretation.

If you don’t have R installed yet, go to, and then download also RStudio for easier visualisation and organisation of your R scripts and data.

Calculate Functional Diversity:

Functional Distance (as in Walker et al. 1999, Ecosystems):

# Calculate the functional distance between every two species,
# where species are characterized by a set of traits

fDist <- dist(traits, "euclidean")

Functional Dipsersion (as in Laliberté & Legendre 2010, Ecology)

# Calculate a measure of functional dominance ("functional dispersion")
# for each sample unit composed of an abundance matrix and a trait matrix
# that characterizes the species
fDisp <- dbFD(traits, abundance)

# Access the results of Functional Dispersion

# Access the Community-Weighted Mean trait values (CWM)

Statistical analyses:

Structural Equation Modelling

response ~ predictor1 + predictor2 + predictor3
predictor1 ~ predictor2 + predictor3

fit <- sem(model, data = data)

summary(fit, fit.measures=TRUE, rsquare=TRUE)

# Remeber to check out residuals and unexplained covariances:
inspect(fit, "")

library(piecewiseSEM) # for SEM using Mixed Effect Models

Mixed Effect Models

model1 <- lme(response ~ predictor1 * predictor2, random =~ 1|random,
correlation = yourCorrelationStructure, data)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s