R/overview.R

# This R-package includes all the calculations done in our paper
#
#
# Thanks for reading and any comments or questions can be sent to
#
# erik.solbu@nibio.no
#

# Note: the functions written here depends on a few packages, some for
# calculations and others for data wrangling or figures in the paper.
# Thanks to all the people who wrote these packages and contributes to
# the R-community.

source("R/required_packages.R")
source(file = "R/f_sim_sad.R")
source(file = "R/f_glmm_est.R")
source(file = "R/f_poilog_est.R")

# Basics ####

# In the non-truncated (i.e. we know the number of zeros) case, these
# two methods are equivalent:

n <- rpoilog(100, 1, 2, keep0 = TRUE)

est_poilog <- poilogMLE(n, zTrunc = FALSE)
est_glmm <- glmmTMB(n ~ (1 | species),
                    data = tibble(n = n,
                                  species = 1:length(n)),
                    family = poisson(link = "log"))

est_poilog$par
c(est_glmm$fit$par[1], exp(est_glmm$fit$par[2]))

# But not in the zero-truncated case (unknown number of zeros vs. no zeros):

n <- rpoilog(100, 1, 2, keep0 = FALSE)

est_poilog <- poilogMLE(n, zTrunc = TRUE)
est_glmm <- glmmTMB(n ~ (1 | species),
                    data = tibble(n = n,
                                  species = 1:length(n)),
                    family = truncated_poisson(link = "log"))

est_poilog$par
c(est_glmm$fit$par[1], exp(est_glmm$fit$par[2]))

# Calculations done in the paper ####

## Comparing poilog and zero-adjusted algorithm (Figure 1) ####

system.time(source("R/example_zero_truncation.R")) # takes 3 minutes

## Estimating dynamic SAD under different assumptions (Figure 2) ####

system.time(source("R/example_assumptions.R"))

## Comparing zero-adjusted to poilog estimation (Figure 3) ####

system.time(source("R/example_poilog_glmm_comparison.R"))

## Fish data set (Figure 4) ####

system.time(source("R/example_fish_data_complete.R"))

## Bats data set (Figure 5) ####

system.time(source("R/example_bats_data_complete.R"))
erikbsolbu/dynamicSAD documentation built on Jan. 25, 2021, 5 a.m.