# R/bootEst.R In overlap: Estimates of Coefficient of Overlapping for Animal Activity Patterns

#### Documented in bootEst

```# Calculate a series of estimates of overlap based on bootstrap samples.

# Parallel processing added in v. 0.2.7.9002 (2017-05-22)

# Function to determine the number of iterations for each core
# ============================================================
min <- iterations %/% cores
xtra <- iterations - min*cores
return( c(rep(min+1, xtra), rep(min, cores - xtra)) )
}

# Function to do bootstraps on a single core
# ==========================================
#   dataList is a list with the matrices for the 2 species
bootEstSerial <- function(dataList, kmax, adjust, n.grid, type) {
nboot <- min(sapply(dataList, ncol))
if(type == "all") {
bsamp <- matrix(NA, nboot, 3)
} else {
bsamp <- matrix(NA, nboot, 1)
}

for(i in 1:nboot)
bsamp[i, ] <- overlapEst(dataList[[1]][, i], dataList[[2]][, i],
return(bsamp)
}

# Main function
# =======================================
bootEst <-
function(Amat, Bmat, kmax=3, adjust=c(0.8, 1, 4), n.grid=128,
type=c("all", "Dhat1", "Dhat4", "Dhat5"), cores=1) {
type <- match.arg(type)

# Deal with the parallel stuff:
if(is.na(cores))
cores <- parallel::detectCores() - 1
if(cores > 1) {
cl <- makeCluster(cores) ; on.exit(stopCluster(cl))
clusterEvalQ(cl, library(overlap))
# split the matrices into chunks for each worker:
nboot <- min(ncol(Amat), ncol(Bmat))
dataList <- vector("list", cores)
for(i in 1:cores)
dataList[[i]] <- list(Amat[, workerID==i], Bmat[, workerID==i])

# Run the thing
resList <- parLapply(cl, dataList, bootEstSerial,
bsamp <- do.call(rbind, resList)
} else {
bsamp <- bootEstSerial(list(Amat, Bmat),
}
if(ncol(bsamp) == 1 && type != "all")  # simple, fast
return(as.vector(bsamp))
if(ncol(bsamp) < 3 && type == "all") { # messy! Must be NAs in 'adjust'
stopifnot(ncol(bsamp) == length(nonNA))
out <- matrix(NA, nboot, 3)
for(i in 1:ncol(bsamp))
out[, nonNA[i]] <- bsamp[, i]
} else {
out <- bsamp
}
colnames(out) <- c("Dhat1", "Dhat4", "Dhat5")
return(out)
}
```

## Try the overlap package in your browser

Any scripts or data that you put into this service are public.

overlap documentation built on Nov. 18, 2023, 5:09 p.m.