fd_pocc: Functional richness, eveness and diversity from the FD...

View source: R/functionaldiversity.R

fd_poccR Documentation

Functional richness, eveness and diversity from the FD package

Description

Functional richness, eveness and diversity from the FD package

Usage

fd_pocc(traits, pocc, nsim = 100)

Arguments

traits

A data frame with each species a row, each column a trait. Row names must match column names of pocc

pocc

An array of occupancy probabiliies. Each row is a site (can be used as a draw), each column a species.

nsim

The number of simulation of occupancy to perform for each site.

Details

Occupancy of each species between each site is considered independent. The occupancy simulation of each species converge relatively slowly for species with very low or high probability. I am not sure what effect this has on the summary metrics.

Examples

fit <- readRDS("../Experiments/7_4_modelrefinement/fittedmodels/7_4_13_model_2lv_e13.rds")
fit <- translatefit(fit)
Xocc <- unstandardise.designmatprocess(fit$XoccProcess, fit$data$Xocc[1:2, , drop = FALSE])
fitsub <- supplant_new_data(fit, Xocc)
pocc <- poccupancy_margotherspecies.jsodm_lv(fitsub)[,,"median"]
pocc2_all <- poccupy(fit, lvvfromposterior = FALSE, margLV = FALSE)
pocc2 <- pocc2_all[1, , , drop = TRUE]
pocc2 <- t(pocc2)
pocc <- pocc2
#pocc2 <- aperm(pocc2, perm = c(3, 1, 2)) 
#dim(pocc2) <- c(500 * 2, 60) #going iterating through row goes through draws, then through sites


traits_raw <- readRDS("../sflddata/private/data/raw/bird_traits.rds")
traits_raw <- traits_raw[, c("CommonName", 
                             primary_diet = "MFScore", 
                             foraging_substrate = "FMScore", 
                             feeding_aggregation = "SSScore", 
                             nesting_aggregation = "BScore", 
                             seasonal_movement = "MScore", 
                             body_size = "CubeRootBodyWeight")]
# convert body size into an ordinal value (rather than continuous)
traits_raw$BodySizeScore <- traits_raw$CubeRootBodyWeight %>% cut(c(0, c(20, 90)^(1/3), max(traits_raw$CubeRootBodyWeight)), 
                                                          ordered_result = TRUE,
                                                          labels = 1:3) %>% 
  as.character() %>% as.numeric()
traits_raw <- traits_raw[, colnames(traits_raw) != "CubeRootBodyWeight"]
species <- colnames(pocc)
stopifnot(setequal(intersect(traits_raw$CommonName, species), species))
traits <- traits_raw[traits_raw$CommonName %in% species, ]
traits <- traits[complete.cases(traits), ] #remove incomplete rows
stopifnot(0 == sum(duplicated(traits$CommonName))) #stop if species are duplicated (i.e. common name used for species with different functionality)
stopifnot(setequal(species, traits$CommonName)) #stop if after the above cleaning, some species are not present in the traits data

rownames(traits) <- traits$CommonName
traits <- traits[, colnames(traits) != "CommonName"]
system.time(fd_av <- fd_pocc(traits, pocc, nsim = 1)) 
#27 seconds for 100 simulations, 60 birds and 2 sites.
#132 seconds for 500 simulations, 60 birds and 2 sites.
#27 seconds for 500 draws, 60 birds and 1 site. At this speed all 2000 insample sites would take 20 hours.
summary(fd_av$E)


sustainablefarms/msod documentation built on March 6, 2023, 7:17 a.m.