Difference between revisions of "Diversity index"

From Testiwiki
Jump to: navigation, search
(Rationale)
(Rationale)
 
(One intermediate revision by the same user not shown)
Line 157: Line 157:
 
}
 
}
  
objects.store(qD, qDa, diversity, diversity.table)
+
##################### Data management function
  
cat("Functions qD, qDa, diversity, and diversity.table stored.\n")
+
longfo <- function(
 +
spraw, # Species data.frame in wide format
 +
trapraw, # Trap data.frame
 +
id.vars = character() # Column names used as identifiers (not species mearurements)
 +
) {
 +
a <- merge(trapraw, spraw) # Assumed that the only common column is the Trap identifier.
 +
 
 +
a$StartDay <- as.POSIXct(paste(a$StartYear, a$StartMonth, a$StartDay, sep = "-"), tz = "UTC")
 +
a$EndDay <- as.POSIXct(paste(a$EndYear, a$EndMonth, a$EndDay, sep = "-"), tz = "UTC")
 +
a$NumDays <- as.numeric(a$EndDay - a$StartDay)
 +
a <- a[ , !colnames(a) %in% c("StartYear", "StartMonth", "EndYear", "EndMonth")]
 +
 +
id.vars <- colnames(a)[colnames(a) %in% union(id.vars, c("Trap", "StartDay", "EndDay", "NumDays", "TrapID", "SeqInTrap",
 +
"SampleCode", "SoilType", "Region", "Latitude"))] # The typical id.vars used.
 +
 
 +
a <- melt(a,
 +
  id.vars = id.vars,
 +
  variable.name = "Species",
 +
  value.name = "Individuals"
 +
)
 +
 
 +
return(a)
 +
}
 +
 
 +
########## THIS FUNCTION CREATES A SAMPLE OF INDIVIDUAL SAMPLING PERIODS
 +
########## AND THEN AGGREGATES ACROSS THE GROUPS.
 +
 
 +
prepare <- function(
 +
dat, # data.frame with the data
 +
samp = TRUE, # do we sample or take rows in order?
 +
lens = c(0, 1, 2, 3, 5, 7, 10, 14, 20, 30, 50, 70, 100, 99999999), # Numbers of lengths in samples
 +
reps = 2, # how many repetitions?
 +
cols = c("TrapID", "Species"), # Column names used for grouping
 +
verbose = FALSE # Verbose returns a list with out as well as a.
 +
) {
 +
 
 +
cols <- colnames(dat)[colnames(dat) %in% cols]
 +
groups <- aggregate(dat$Individuals, by = dat[cols], FUN = length)
 +
 +
# This code makes a list of lengths that is used for index j to avoid overwhelmingly many samples for large groups.
 +
# First, possible lengths of a sample are determined by len. Second, the number of observations in each group
 +
# determine how far you go in the list.
 +
lengths <- list()
 +
for(i in 1:length(groups$x)) lengths[[i]] <- c(lens[1:(findInterval(groups$x[[i]], lens) - 1)], groups$x[[i]])[-1]
 +
 
 +
if(!samp) reps <- 1
 +
 
 +
a <- data.frame() # Sampled data.frame going to aggregation
 +
for(i in 1:nrow(groups)) {
 +
for(j in lengths[[i]]) {
 +
for(k in 1:reps) {
 +
 +
if(samp) ro <- sample(groups$x[i], j, replace = FALSE) else ro <- 1:j
 +
 
 +
if(j != 0) {a <- rbind(a, data.frame(
 +
Len = j,
 +
Reps = k,
 +
merge(dat, groups[i , colnames(groups) != "x"])[ro , ]
 +
))}
 +
}
 +
}
 +
}
 +
 
 +
if(verbose) {
 +
print(head(a))
 +
print(nrow(a))
 +
}
 +
 
 +
##### Create new variable Spec which is the first occurrence of a species in a group.
 +
cols <- union(cols, c("Len", "Reps"))
 +
 
 +
a <- a[order(a$StartDay) , ] # Order by start day.
 +
a$Spec <- 0
 +
rownames(a) <- 1:nrow(a)
 +
a$Spec[as.numeric(rownames(unique(subset(a, Individuals > 0, cols))))] <- 1
 +
 
 +
##### AGGREGATION FROM THE SAMPLED PERIODS
 +
 
 +
out <- aggregate(a[colnames(a) %in% c("Individuals", "Spec", "NumDays")], by = a[cols], FUN = sum)
 +
temp <- aggregate(a[colnames(a) %in% c("Region")], by = a[cols], FUN = function(x) x[1])
 +
out <- cbind(out, temp[!colnames(temp) %in% cols]) # Remove redundant columns before cbind.
 +
temp <- aggregate(a[colnames(a) %in% c("SoilType", "TrapID")], by = a[cols], FUN = function(x) paste(x, collapse = " "))
 +
out <- cbind(out, temp[!colnames(temp) %in% cols])
 +
temp <- aggregate(a[colnames(a) %in% c("StartDay")], by = a[cols], FUN = min)
 +
out <- cbind(out, temp[!colnames(temp) %in% cols])
 +
temp <- aggregate(a[colnames(a) %in% c("EndDay", "Len", "Reps")], by = a[cols], FUN = max)
 +
out <- cbind(out, temp[!colnames(temp) %in% cols])
 +
 
 +
out$SamplingGap <- as.numeric(out$EndDay - out$StartDay - out$NumDays)
 +
 
 +
if(verbose) return(list(out, a)) else return(out)
 +
}
 +
 
 +
objects.store(qD, qDa, diversity, diversity.table, longfo, prepare)
 +
 
 +
cat("Functions qD, qDa, diversity, and diversity.table, longfo, prepare stored.\n")
  
 
</rcode>
 
</rcode>
Line 215: Line 310:
 
{{hidden|
 
{{hidden|
 
<pre>
 
<pre>
 +
library(reshape2)
 +
library(ggplot2)
 +
library(OpasnetUtils)
 +
library(vegan)
 +
 
# Sp accumulation ichno simple
 
# Sp accumulation ichno simple
 
# version 2014-11-09 by Hanna Tuomisto
 
# version 2014-11-09 by Hanna Tuomisto
Line 262: Line 362:
 
trapraw <- read.csv(path, header=TRUE, stringsAsFactors = FALSE)
 
trapraw <- read.csv(path, header=TRUE, stringsAsFactors = FALSE)
 
trapname <- gsub(".csv", "", basename(path), ignore.case=TRUE)
 
trapname <- gsub(".csv", "", basename(path), ignore.case=TRUE)
 +
 +
###### ALLA OLEVA HOITUISI MERGELLÄ. JOS HALUTAAN, VOI LISÄTÄ SKIRPTIN JOKA KERTOO MITÄ TIPUTETTIIN.
 +
a <- merge(spraw, trapraw, by = "TrapID")
  
 
# eliminate traps that are only found in one of the data files
 
# eliminate traps that are only found in one of the data files
Line 301: Line 404:
  
 
#### Calculate catch for different sampling periods and traps
 
#### Calculate catch for different sampling periods and traps
 +
#### ALLA OLEVAN VOISI KORVATA POSIXct:llä
  
 
# convert start and end date to sequential day
 
# convert start and end date to sequential day
Line 321: Line 425:
 
data_tr <- merge(traptable[,-4], data_tr)   
 
data_tr <- merge(traptable[,-4], data_tr)   
 
# fix start and end days
 
# fix start and end days
data_tr$StartDay <- aggregate(data$StartDay, by=list(data$TrapID), FUN=min, simplify=TRUE)[,2]
+
data_tr$StartDay <- aggregate(data$StartDay, by=data["TrapID"], FUN=min, simplify=TRUE)[,2]
 
data_tr$EndDay <- aggregate(data$EndDay, by=list(data$TrapID), FUN=max, simplify=TRUE)[,2]
 
data_tr$EndDay <- aggregate(data$EndDay, by=list(data$TrapID), FUN=max, simplify=TRUE)[,2]
 
data_tr$SamplingGap <- data_tr$EndDay - data_tr$StartDay - data_tr$NumDays
 
data_tr$SamplingGap <- data_tr$EndDay - data_tr$StartDay - data_tr$NumDays
 +
 +
#### Code below can be used after data is managed with functions longfo and prepare.
 +
 +
temp <- aggregate(a$Indiv_cum, INDEX = a[c("Time_cum", "Trap")], FUN = sum)
 +
temp2 <- aggregate(a$Spec_cum, INDEX = a[c("Time_cum", "Trap")], FUN = sum)
 +
 +
ggplot(subset(temp, Trap == "a"), aes(x = Time_cum, y = Freq, colour = Trap))+geom_step()
 +
 +
ggplot(a,aes(x=Cum,color=Trap, group = Trap)) +
 +
  stat_bin(data=a, aes(y=cumsum(..count..)),geom="step")+
 +
  stat_bin(data=subset(a,Trap=="b"),aes(y=cumsum(..count..)),geom="step")
 +
 +
ggplot(a, aes(x=Cum, y = cumsum(Individuals))) + geom_line()
  
 
# run data aggregation within and across traps
 
# run data aggregation within and across traps
Line 391: Line 508:
 
renyiD <- renyi(to_est[,8:ncol(to_est)], scales=c(0,1,2), hill=TRUE)
 
renyiD <- renyi(to_est[,8:ncol(to_est)], scales=c(0,1,2), hill=TRUE)
 
colnames(renyiD) <- paste("Div.q", colnames(renyiD), sep="")
 
colnames(renyiD) <- paste("Div.q", colnames(renyiD), sep="")
estSdiv <- cbind("Region"=to_est$Region, "MTM"=to_est$NumDays/30, "StartDay"=to_est$StartDay,  
+
estSdiv <- cbind(
                "Individuals"=rowSums(to_est[,8:ncol(to_est)]), estS[,-(4:5)],  
+
Region = to_est$Region,
                estSadd, renyiD, "TrapID"=to_est$TrapID, "SoilType"=to_est$SoilType)
+
MTM = to_est$NumDays/30,
 +
StartDay = to_est$StartDay,  
 +
Individuals = rowSums(to_est[,8:ncol(to_est)]),
 +
estS[,-(4:5)],  
 +
estSadd,  
 +
renyiD,
 +
TrapID = to_est$TrapID,
 +
SoilType = to_est$SoilType
 +
)
 
colnames(estSdiv)[which(colnames(estSdiv)=="S.obs")] <- "Species"
 
colnames(estSdiv)[which(colnames(estSdiv)=="S.obs")] <- "Species"
 
write.csv(estSdiv, file=paste(to_estname, "_S_est_D.csv", sep=""))
 
write.csv(estSdiv, file=paste(to_estname, "_S_est_D.csv", sep=""))

Latest revision as of 18:09, 9 February 2015



Question

How to calculate diversity indices?

Answer

Use the function diversity to calculate the most common indices. These parameters are used:

  • amount: the number (or proportion) of individuals of a species in a transect.
  • species: an identifier for a species
  • transect: an identifier for a transect
  • q: exponent for the diversity calculation


Amount, species, and transect are vectors (i.e. ordered sets of values). The parameters can be given to the function in several different ways. This is hopefully practical for the user.

  • Amount, species, and transect must be of same length.
  • If parameter names are not used (e.g. diversity(param1, param2, param3)), it is assumed that they are given in this order: amount, species, transect, q.
  • If individual data is given using species identifiers, it the parameter name must be given: diversity(species = param1).
  • The following default values are used for parameters:
    • Amount: 1 for each species, i.e. each species is equally abundant.
    • Species: 1, 2, 3, ... n, where n = number of rows in data, i.e. each row is a different species.
    • Transect: 1, i.e. there is only one transect.
    • q: q.wiki. You MUST define an object q.wiki before using diversity, otherwise alpha diversities will be calculated wrong. q.wiki can be a single number or a vector of numbers.


There are several ways to upload your data so that you can use the diversity function.

  • Upload your data to Opasnet Base and call for the data using op_baseGetData function.
  • Upload a CSV file to Opasnet using Special:Upload and call for the data using opasnet.data function.
  • Use Example 1 code below to enter your data. The data must be in format c(4,6,2,5,7,4,3,9) where the values are either
    • identifiers of the species 1,2,3... in which the individuals belong (one entry per individual), or
    • abundancies of species, i.e. proportions or amounts of individuals belonging to each species among the whole population (one entry per species).

Rationale

Actual function diversity

Initiate functions

+ Show code

The function diversity produces a list where the first, second, and third element are the gamma, the alpha, and transect-specific gamma diversities, respectively.

Function diversity.table produces a data.frame of several diversity indices.

Examples

Example 1 to use function

Give your data in R format or leave empty for example data:

Is your data individual data or group abundancies?:

+ Show code

Example 2

Select your data:

Which q values you want to calculate.:
0
0.5
1
2
3
6

+ Show code

The data should be given in R format as a list of values in parenthesis, beginning with c:

c(3,5,3,5,2,1,3,3,4,2) or equivalently c(0.1,0.2,0.4,0.1,0.2)

where the values are either

  • identifiers of the species 1,2,3... in which the individuals belong (one entry per individual), or
  • abundancies of species, i.e. proportions of individuals belonging to each species among the whole population (one entry per species).

Calculations


Diversity indices are thoroughly described in Wikipedia.

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>