Function library and guide for practicals in Community ecology in R

 

Basic knowledge of R is useful, but not essential, as all functions are available in a ready-made form. All necessary R commands (and there are only a handful of them) are listed in the table below.

Overview of all functions
Logistic growth
May equation
Lotka-Volterra dynamics of two species systems
Disturbance in LV systems
Environment variation in LV systems
Predator-prey dynamics
Resource competition: one resource
Resource competition: two resources
Metapopulation dynamics: one species
Metapopulation dynamics: two species
Dynamics on one and several species in explicit space (cellular automata-like models)
Geometry of species distribution
Species abundance distribution, species-area curve
Neutral model of community dynamics
Optimum reproduction
Parameter estimation from data
Back to syllabus
Old functions in Matlab
Basic R commands
 

 

Overview of all R functions

 

Name
Function
Description

logist

Logistický růst jedné populace s volitelnou lag fází.
logerr
Logistický růst jedné populace se stochastickou variabilitou.

maysingle

Mayova rovnice pro populaci.
parabolasingle
Mayova rovnice pro populaci, stavové zobrazení.

may

Mayova rovnice pro populaci, animace v závislosti na měnícím se r.

lv2_time

Simulace LV dynamiky dvou druhů.

lv2_lines

Isočáry LV dynamiky dvou druhů ve stavovém prostoru.

lv2_plot

Simulace LV dynamiky dvou druhů ve stavovém prostoru s čarami nulového růstu.

lv2_3d

LV dynamika dvou druhů, response surface podílu jednoho druhu k hodnotám alfa.

lv2_3d_divers

LV dynamika dvou druhů, response surface diversity k hodnotám alfa.

lv2_3d_ini

LV dynamika dvou druhů, response surface k počátečním populačním hustotám.

lv2_dist

Simulace LV dynamiky dvou druhů s disturbancí.

lv2_dist_plot

Dynamika dvou druhů s disturbancí, zobrazení ve stavovém prostoru.

lv2_dist_3d

Dynamika dvou druhů s disturbancí, response surface podílu jednoho druhu.

lv2_dist_3d_divers

Dynamika dvou druhů s disturbancí, response surface diversity.

lv2_env

Simulace LV dynamiky dvou druhů s cyklickou variabilitou vnějších podmínek (změna K).

lv2_env_plot

Dynamika dvou druhů s variabilitou vnějších podmínek, zobrazení ve stavovém prostoru.

res1

Kompetice o zdroje: Jeden zdroj a jeden nebo více druhů.

res1_plot_rsp

Kompetice o zdroje: Jeden zdroj a jeden druh ve stavovém grafu s izočárami. Animovaná dynamika.

res1_plot_para

Kompetice o zdroje: Jeden zdroj a jeden druh ve stavovém grafu s izočárami. Měnící se izočáry v závislosti na měnících se parametrech

res1_plot_sp2

Kompetice o zdroje: dva druhy kompetující o jeden zdroj ve stavovém grafu.

res2

Kompetice o zdroje: Dva zdroje a jeden nebo více druhů.

res2_plot

Kompetice o zdroje: Dva zdroje a jeden nebo více druhů, grafické zobrazení.
pp_time Simulace predator-prey dynamiky dvou druhů
pp_lines Isočáry predator-prey dynamiky dvou druhů
pp_plot Simulace predator-prey dynamiky dvou druhů ve stavovém prostoru s čarami nulového růstu.
pp_plot_para Stavový graf predator-prey dynamiky dvou druhů s čarami nulového růstu. Měnící se izočáry v závislosti na měnících se parametrech.
pp_shape Srovnání dvou složek populační dynamiky kořisti - tj. růstu a predace. Funkci lze použít ke zkoumání vlivu jednotlivých parametrů predace na chování kořisti při type III functional response

metapop

Metapopulační dynamika: Jeden druh na systému diskrétních stanovišť - základní Function.
metapop_plot
Metapopulační dynamika: dynamika jednoho druhu na systému diskrétních stanovišť v grafu složek Levinsovy rovnice.

metapop2

Metapopulační dynamika: Dva druhy na systému diskrétních stanovišť - základní Function.

metapop2_plot

Metapopulační dynamika: stavová dynamika dvou druhů na systému diskrétních stanovišť - stavový graf s izočárami a animovanou dynamikou.

ca1

Interakce v explicitním prostoru: Jeden nebo více druhů v prostorově explicitním systému diskrétních stanovišť.

distr

Generování teoretického rozložení druhových četností různých typů.

comm

Generování jednoho konkrétního společenstva (souboru N jedinců) z daného rozložení druhových četností.

SpecArea

Vytvoření species-area křivky.

neutral

Neutrální dynamika metaspolečenstva se speciací.

EvolreproEffort

Evolution of optimum reproduction

PlotFitnessTheta

Plotting fitness response to reproductive effort.

 

 

 

Basic R commands

Problem How to do it Example Comment
Function call name_Function(arguments)

r = 0.1
K = 100
n0 = 1
noise = 0
nostep = 100
logist(r,K,n0,noise, nostep)

or

logist(0.1,100,1,0, 100)

calls function logist (calculates logistic population growth) for intrinsic growth rate 0.1, carrying capacity of 100, initial number of individuals 1, and 100 steps.

The first example first assigns parameter values to variables that are used as function arguments, in the second example the constants are given as function parameters directly.

Function output is written to the console, but some functions need not return anything. Functions may have additional effects (e.g. drawing plots).

NB. Function must be defined before calling.

Define a function from a data file function source()

source("logist.R")

reads in the fuction(s) stored in the file logist.R

This function reads in all functions stored in the given file.

NB. In this practical, most of the functions are stored in their own files.

NB. Identical effect is attained by opening the file with the function body (logist.R in this case), selecting the script and Edit->Run line or selection

Assigning value to a variable

Using =

or<-

a = 3

a <- 3

Variable a gets the new value (if it did not exist, it is defined).

This can be used to assign scalars, vectors, matrices, lists and other data types (the variable knows its own data type)

Assigning function output to a variable

a=name_Function(arguments)

a = logist(0.1,100,1,0, 100)

Output of the function logist is not echoed on the console, but stored into a variable (which can be later used, e.g. for plotting)

Check for value of a variable

name_variable

a

It echoes the value; an error message if the variable does not exist.

Vector definition function c() c(1,2,3,4)  
Access to vector or matrix elements

[]

a[2]

returns the second element in a
Generation sequence of numbers

using :

or function seq()

1:10

seq(1,10,by=2)

 
Matrix definition function cbind() nebo rbind() cbind(1:5,4:8)

All sequences must be identical - matrix must be rectangular.

cbind joins by columns, rbind by rows

There are also other options (e.g. function as.matrix)

Matrix transpose Function t()

a=cbind(1:5,4:8)

t(a)

original matrix has 5 rows and 2 columns, new matrix has 2 rows and 5 columns.

Determine size of a matrix or a vector

Function length() (for vectors) or function dim() (for matrices)

a=cbind(1:5,4:8)

dim(a)

Function length for matrices return the total number of matrix elements which is not very useful

Plot one variable against another variable (simple XY plotting of two variables)

Function plot(x,y), first argument is for the X axis, the second for the Y axis

plot(1:5,(1:5)^2)

If the function has only one argument (e.g. plot((1:5)^2) ), it plots the variable against the index variable (in this case it is equivalent to the expression plot(1:5,(1:5)^2) )

Plot several avriables against one variable (twodimensional plot with several series of Y)

Function matplot(x,y), the first argument is the vector for teh X axis, the second argument is the matrix in which individual columns correspond to series to be plotted on the Y axis

x=1:5
y= (1:5)^2
y1=(1:5)^3
matplot(x,cbind(y,y1))

columns correspond to series to be plotted on the Y axis

length of the X vector and number of rows of the Y matrix must be equal

Access to individual elements of a list

operator $

operator [[]]

listname$elementname

listname[[elementindex]]

Some functions (neutral, pp_time) return a list as the output (e.g. a vector and a number). In such a case they are linked as a "list". Components of a list can be examined using the function names(listname) .
Stop current calculation

Esc

or Misc->Stop current computation

   
List variables in the workarea

Function objects()

or Misc->List objects

objects() no arguments, but brackets are necessary
List files in the working directory Function dir() dir() no arguments, but brackets are necessary
Check for working directoty

Function getwd()

or File->Change dir

getwd()

no arguments, but brackets are necessary
Cahnge working directory

Function setwd()

or File->Change dir

setwd(folder_name)

 

 


 

Logistic population growth

System:

Potentially interesting things:

Functions implementing logistic population growth:

(1) Function logist: Logistc growth of a population with variable lag. The function returns a vector of a length nostep, with time series data on population growth. This has to be plotted using the function plot. Parameters of the function:

(2) Function logerr: Logistic population growth with stochastic variability. Returns a vector of a length nostep, with time series data on population growth. This has to be plotted using the function plot. Parameters of the function:

 

(3) Soubor pro volání funkcí calc_logistic. Příklad volání funkcí.

Back to the syllabus
Back to the function overview

Logistic population growth: May's equation

System:

Možná zajímavá zkoumání:

Function:

(1) Function maysingle: May's equation. Returns a vector of a length nostep, with time series data on population growth. This has to be plotted using the function plot. Parameters of the function:

(2) Function parabolasingle: May's equation in the state space. Function plots a plot. Parameters:

(3) Function may: May's equation with animation: change with the value of r. Function plots a (n animated) plot. Parameters:

(4) Soubor pro volání funkcí calc_may. Příklad volání funkcí.

Back to the syllabus
Back to the function overview

 

Lotka-Volterra dynamics of two species

System:

Lotka-Volterra dynamics of two species: problems to be examined

Functions implementing Lotka-Volterra dynamics:

(1) Function lv2_time: LV dynamics of two species. (basic function) This function can be used to examine temporal dynamics of the system (x axis time, y axis function values). It returns a 2xnostep matrix with dynamics of both species.Function parameters:

(2) Function lv2_lines: Isolines of LV dynamics in the state space. This function calculates values of isolines of both species, and is only an auxiliary function for the next two. Parameters of the function:

(3) Function lv2_plot: Simulation of LV dynamics of two species in teh state space with zero-growth isolines. (two previous functions must be downloaded to make this function run.) This function plots the state space of the system, draws zero-growths isoclines, and system development trajectory (in red). Function parameters:

(4) Function lv2_3d: LV dynamics of two species: response surface to values of alfa (all previous functions must be downloaded to make this function run). This function draws a plot showing how (given fixed values of K, a r and initial sizes) final proportion of species 2 depends on alfa12 a alfa21. Parameters of the function:

(5) Function lv2_3d_divers: LV dynamics of two species: response surface k hodnotám alfa (all previous functions must be downloaded to make this function run). This function draws a plot showing how (given fixed values of K, a r and initial sizes) diversity of the system depends on alfa12 a alfa21. Parameters of the functionare identical as for the lv2_3d function:

(6) Function lv2_3d_ini: LV dynamics of two species: response surface to initial species densities (all previous functions must be downloaded to make this function run). This function draw a plot showing how (for fixed K, alfa and r) proportion of species 2 depends on initial species densities. Function parameters are similar:

(7) Soubor pro volání funkcí calc_LV. Příklad volání funkcí.

Back to the syllabus
Back to the function overview

Vliv disturbance na dynamiku a koexistenci dvou druhů

Systém:

Problémy ke zkoumání

Function s disturbancí v LV systémech:

(1) Function lv2_dist: Simulace LV dynamiky dvou druhů s disturbancí. Tato Function je vhodná pro sledování dynamiky systému v čase (na ose x čas, na ose y hodnoty Function). Function vrací matici o rozměru 2xnostep, obsahující časový vývoj obou druhů. Parametry Function:

(2) Function lv2_dist_plot: Dynamika dvou druhů s disturbancí: zobrazení ve stavovém prostoru. Tato Function kreslí obrázek (případně animovaný) ve stavovém prostoru (isočáry a trajektorie vývoje systému). Parametry Function jsou stejné jako v předchozím případě, a jeden parametr navíc (zda animovat).

(3) Function lv2_dist_3d: Dynamika dvou druhů s disturbancí: response surface. Tato Function kreslí obrázek, který ukazuje, jak (pro daná K, alfa a frekvenci disturbance) závisí podíl druhu 2 na jeho růstové rychlosti a intensitě disturbance. Parametry Function jsou podobné, jako v předchozím případě, ale je jich trochu víc:

(4) Function lv2_dist_3d_divers: Dynamika dvou druhů s disturbancí: response surface. Tato Function kreslí obrázek, který ukazuje, jak (pro daná K, alfa a frekvenci disturbance) závisí diverzita systému na jeho relativní růstové rychlosti jednoho druhu a intensitě disturbance. Parametry Function jsou podobné, jako v předchozím případě:

(5) Soubor pro volání funkcí calc_LV_dist. Příklad volání funkcí.

Vliv variability vnějších podmínek na dynamiku a koexistenci dvou druhů

Systém:

Problémy ke zkoumání:

Function implementující proměnlivost prostředí v LV systémech

(1) Function lv2_env: Simulace LV dynamiky dvou druhů s cyklickou variabilitou vnějších podmínek (změna K). Tato Function je vhodná pro sledování dynamiky systému v čase (na ose x čas, na ose y hodnoty Function). Function vrací matici o rozměru 2xnostep, obsahující časový vývoj obou druhů. Parametry Function (jsou tři, a první dva z nich jsou vektory!):

(2) Function lv2_env_plot: Dynamika dvou druhů s variabilitou vnějších podmínek: zobrazení ve stavovém prostoru. Tato Function kreslí obrázek (případně animovaný) ve stavovém prostoru (isočáry a trajektorie vývoje systému). Parametry Function jsou stejné jako v předchozím případě, a jeden parametr navíc (zda animovat).

(3) Soubor pro volání funkcí calc_LV_env. Příklad volání funkcí.

Back to the syllabus
Back to the function overview

Predator-prey dynamics

System:

Problems to study

Functions implementing predator-prey dynamics:

(1) Function pp_time: PP dynamics. (basic function) This function generates system dynamics with given parameters. Function returns 2xnostep matrix, with temporal dynamics of both species. Function parameters:

(2) Function pp_plot: PP dynamics of two species in the state space with zero growth isoclines (it is necessary to download and save the previous function). This function plots state space of the system, draws zero growth isoclines and system trajectory. Parameters of the function:

(3) Function pp_plot_para: Aanalysis of the effects of one parameter on PP dynamics of two species in state space (it is necessary to download and save the function pp_time). This function plots the state space and draws into it a series of isoclines with one parameter changing. Parameters of the function:

(4) Auxiliary function pp_shape: plots the two components of population dynamics of the prey (the first equation above), i.e. growth and mortality through predation as a function of prey density. It is plotted for a given value of predator density, which must be also given in addition to all other parameters. Prey population density increases if the blue line (natality) is above the green line (predation). The function can be used to examine effects of individual parameters in the case of the type III functional response. Function parameters:

(5) Script for function calls calc_pp. Examples of function calls.

Back to the syllabus
Back to the function overview

Resource competition: one resource

System:

Problems to study:

Functions implementing resource-driven growth of one or several species:

(1) Function res1: One resource and one or several species (basic function). This function is useful for plotting dynamics against time. It returns a matrix with n+1 rows (n is number of species), containing amounts of resource through time and abundances of all species through time. Function has six parameters, the second parameter is a vector (if one species is present) or a matrix (if there are more than one species).

(2) Function res1_plot_rsp: One species and one resource in state space with isoclines. This function plots state space. Only one species can be present. It has six parameters, first of which is a vector.

(3) Function res1_plot_para: One resource and one species in a state space with isoclines. This function plots state space with changing isoclines following changes in one parameter. Only one species can be present. It has eight parameters, first of which is a vector.

(4) Function res1_plot_sp2: Two species in the state space. This function plots a state space of two species. Two species must be present. The function has five marameters, first of which is a 2x4 matrix.

(3) Script for function calls calc_res1. Examples how to call the functions.

Resource competition: two resources

System:

Problems to study:

Functions implementing competition for two resources for two or more species:

(1) Function res2: Two species and one or more species (basic function). This function is useful for plotting dynamics against time. It returns a matrix with n+2 rows (n is number of species), containing amounts of resource through time and abundances of all species through time. Function has seven parameters, the second parameter is a vector (if one species is present) or a matrix (if there are more than one species).

(2) Function res2_plot: two resources and one or more species: graphical. This function plots two plots, one state space of resources (with zero net growth isoclines and system trajectory) and plot against time. It has seven parameters, the second parameter is a vector (if one species is present) or a matrix (if there are more than one species).

(3) Script for function calls calc_res2. Examples.

Back to the syllabus
Back to the function overview

Metapopulation dynamics of one species

System:

Problems to be studied:

Function implementing metapopulation dynamics of one species:

(1) Function metapop: one species in the system of discrete habitats (basic function). This function simulates dynamics of one species. It returns a vector with successive values of proportion of occupied habitats over time. It optionally plots spatial figure. Parameters of the function:

(2) Function metapop_plot: one species in the system of discrete habitats: state space and system dynamicsFunction parameters:

(3) Script for function calls calc_metapop. Examples.

Metapopulation dynamics of two species

System:

Problems to be studied:

Functions implementing metapopulation dynamics of two species:

(1) Function metapop2: two species in the system of discrete habitats. This function implements Levins' s dynamics of two species in discrete habitats. It returns a 2xn matrix with temporal dynamics of both species. It optionally plots a spatial plot. Function parameters:

(2) Function metapop2_plot: State space with zero growth isoclines. This function plots dynamics of two species having on Levins' s dynamics in discrete habitats. Function parameters:

(3) Script for function calls calc_metapop2. Examples.

Zpět na syllabus
Zpět na přehled funkcí

Spatially explicit dynamics of one or several species using a cellular automata approach

System:

Problems to be studied:

Function implementing spatially explicit system using cellular automata:

(1) Function ca1: One or more species in the system of a square lattice with local dynamics. Function returns a list with two elements: (i) abund, a matrix with nospec lines with temporal dynamics of all species, (ii) map, lattice at the end of the simulation. It also can draw spatial plot. Function parameters:

(2) Script for function calls calc_ca. Příklad volání Function.

(3) Example of the file with initial spatial arrangement.

Zpět na syllabus
Zpět na přehled funkcí

Geometry of species distributions 

System:

Problems to be studied:

Functions implementing fractal dimension:

(1) Function fdim: Calculate boxcounting dimension of a given matrix. The function returns a scalar value with the boxcounting dimension of the matrix. Parameters:

(2) Script to calculate boxcounting dimension of geometric shapes and random lattices calc_fdim. Simple examples.

(3) Script to calculate boxcounting dimension of species simulated by cellular automata calc_fdim_ca. Examples of one and two-species systems (some manual work is needed for three species systems).

 

Zpět na syllabus
Zpět na přehled funkcí

Species abundance distributions, species-area relationship and related phenomena 

System:

Problems to be studied:

Theoretical distributions of abundances (SAD):

Communities with finite sizes, sampling and species-area relationship (SAR):

Functions implementing SAD and SAR:

(1) Function distr: Generate theoretical distribution of abundances of different types. . The function returns a vector with cumulative distributions of abundances for the given species pool. Parameters:

(2) Script for function calls calc_distr. Examples.

(3) Function comm: Generate one finite community (a set of N individuals) from the given theoretical abundance distribution. Function returns a vector of absolute abundances (numbers of individuals) of all species in the community and number of species (both elements are linked as a list). Parameters:

(4) Function SpecArea: Species-area relationship (more accurately relationship of species number and number of individuals)

Function successively draws 'noreal'-times a community of sizes sizemin, sizemin*sizeincrease, sizemin*sizeincrease^2, up to nosizes. It returns 2*nosizes matrix with community size and associated mean number of species and plots linear or logarithmic plot of this relationship.

(5) Script for function calls calc_comm_specarea. Examples for comm a SpecArea.

 

Zpět na syllabus
Zpět na přehled funkcí

Neutral model of community dynamics

System:

Problems to be studied:

Function implementing neutral model:

(1) Function neutral. This function models neutral dynamics of a metacommunity (i.e. a set of local communities) with mortality (disturbance), migration, local multiplication and speciation. It draws figures and returns five variables:

NB. These variables are returned as a list. If the output is placed into e.g. a variable a, individual elements of the list can be accessed either by operator $: a$outddend, or using the the operator [[]]: a[[1]]

Parameters of the function:

(2) Script for function calls calc_neutral.

Zpět na syllabus
Zpět na přehled funkcí

 

Simple optimum reproduction

System:

Problems to be studied:

Function implementing optimal life strategy evolution:

(1) Function ReproEffort. This function models evolution of optimum life strategies of a set of species with speciation and extinction due to density dependence. It draws figures and returns two variables:

NB. These variables are returned as a list. If the output is placed into e.g. a variable a, individual elements of the list can be accessed using the the operator [[]]: a[[1]]

Parameters of the function:

(2) Function PlotFitnessTheta. This function plots survival, fecundity and total fitness for individual species (as follows from their definition). It is embedded in the file ReproEffort.R

Parameters of the function:

(3) Script for function calls calc_ReprEffort. Script for function calls.

 

Evolution of optimum reproduction

System:

Problems to be studied:

Function implementing optimal life strategy evolution:

(1) Function EvolReproEffort. This function models evolution of optimum life strategies of a set of species with speciation and extinction due to density dependence. It draws figures and returns two variables:

NB. These variables are returned as a list. If the output is placed into e.g. a variable a, individual elements of the list can be accessed using the the operator [[]]: a[[1]]

Parameters of the function:

(2) Function PlotFitnessTheta. This function plots survival, fecundity and total fitness as a function of reproductive effort. It is embedded in the file EvolReproEffort.R

Parameters of the function:

(3) Script for function calls calc_EvolreproEffort. Script for function calls.

 

Zpět na syllabus
Zpět na přehled funkcí

Fitting models to data

Problems to be studied:

(1) Script for function calls calc_fit. Script for function calls necessary for parameter optimization.

 

Zpět na syllabus
Zpět na přehled funkcí

Na stránku se syllaby

Domovská stránka