Sorry not to make a pull request but it is such a quick fix I figure it is not worth the overhead.
cue_rate2 <- function (species = NULL, model = NULL, od = NULL, tssr = NULL,
pairwise = FALSE, quantiles = NULL, samples = 1000)
{
rem_vcv_list <- NULL
rm(rem_vcv_list)
napops:::check_data_exists()
if (!is.null(species)) {
napops:::check_valid_species(species = species, mod = "rem")
}
if (!is.null(model)) {
napops:::check_valid_model(model = model, mod = "rem")
}
if (pairwise) {
if (length(od) != length(tssr)) {
stop("Pairwise set to TRUE but OD and TSSR are not the same length.")
}
}
sp_covars <- covariates_removal(species = species)
if (!is.null(od)) {
od_range <- range(sp_covars$OD)
if (any(od < od_range[1]) || any(od > od_range[2])) {
warning(paste0("You are providing some OD values that are outside the training values of [",
od_range[1], ",", od_range[2], "] for species ",
species))
}
}
if (!is.null(tssr)) {
tssr_range <- range(sp_covars$TSSR)
if (any(tssr < tssr_range[1]) || any(tssr > tssr_range[2])) {
warning(paste0("You are providing some TSSR values that are outside the training values of [",
tssr_range[1], ",", tssr_range[2], "] for species ",
species))
}
}
if (isFALSE(pairwise)) {
tssr_values <- rep(tssr, each = length(od))
sim_data <- data.frame(Intercept = rep(1, times = length(tssr_values)),
TSSR = tssr_values, OD = rep(od, length(tssr)))
}
else {
sim_data <- data.frame(Intercept = rep(1, times = length(tssr)),
TSSR = tssr, OD = od)
}
design <- sim_data
tssr_median <- stats::median(sp_covars$TSSR)
design$TSSR <- (design$TSSR - tssr_median)/24
design$TSSR2 <- design$TSSR^2
od_sp_median <- stats::median(sp_covars$OD)
design$OD <- (design$OD - od_sp_median)/365
design$OD2 <- design$OD^2
design <- design[, c("Intercept", "TSSR", "TSSR2", "OD",
"OD2")]
coefficients <- coef_removal(species = species, model = model)
coefficients <- as.numeric(coefficients[, c("Intercept",
"TSSR", "TSSR2", "OD", "OD2")])
if (is.null(quantiles)) {
coefficients[which(is.na(coefficients))] <- 0
phi <- exp(as.matrix(design) %*% coefficients)
sim_data$CR_est <- phi
sim_data <- sim_data[, c("TSSR", "OD", "CR_est")]
return(sim_data)
}
else {
load(paste0(rappdirs::app_dir(appname = "napops")$data(),
"/rem_vcv.rda"))
vcv <- rem_vcv_list[[model]][[species]]
bootstrap_df <- bootstrap(vcv = vcv, coefficients = coefficients,
design = design, quantiles = quantiles, samples = samples,
model = "rem")
return(cbind(sim_data[, c("TSSR", "OD")], bootstrap_df))
}
}
bird_obs <- data.frame(species = c("AMRO", "AMGO", "BCCH", "SCTA"),
od = seq(85, 180, by = 5),
tssr = seq(85, 180, by = 5)/90)
cue_rate(species = bird_obs$species[1],
model = "best",
od = bird_obs$od,
tssr = bird_obs$tssr,
pairwise = TRUE)
bm <- bench::mark(old = cue_rate(species = bird_obs$species[1],
model = "best",
od = bird_obs$od,
tssr = bird_obs$tssr,
pairwise = TRUE),
new = cue_rate2(species = bird_obs$species[1],
model = "best",
od = bird_obs$od,
tssr = bird_obs$tssr,
pairwise = TRUE),
filter_gc = FALSE, min_iterations = 10,
relative = TRUE)
cue_ratehas 3 calls tocovariates_removalbut only really needs one. Replacing the later 2 calls with thesp_covarsobject created from the first call leads to >2.5x faster execution.Sorry not to make a pull request but it is such a quick fix I figure it is not worth the overhead.
Thanks