Skip to contents

Get estimates of ZOI from response curves

Usage

zoi_from_curve(
  x,
  percentage = 0.95,
  curve = c("median", "mean"),
  ci = TRUE,
  type = c("linear", "exp")[1],
  mean_col_name = "mean",
  median_col_name = "quantile:0.5",
  ci_col_name = c("quantile:0.025", "quantile:0.975")
)

Examples

#---
# fit a bag to be tested

# load packages
library(glmnet)

# load data
data("reindeer_rsf")
# rename it just for convenience
dat <- reindeer_rsf

# formula initial structure
f <- use ~ private_cabins_XXX + public_cabins_high_XXX +
  NORUTreclass +
  # poly(norway_pca_klima_axis1, 2, raw = TRUE) +
  # poly(norway_pca_klima_axis2, 2, raw = TRUE) +
  norway_pca_klima_axis1 + norway_pca_klima_axis1_sq +
  norway_pca_klima_axis2 + norway_pca_klima_axis2_sq +
  norway_pca_klima_axis3 + norway_pca_klima_axis4

# add ZOI terms to the formula
zois <- c(100, 250, 500, 1000, 2500, 5000, 10000, 20000)
f <- add_zoi_formula(f, zoi_radius = zois, pattern = "XXX",
                     type = c("cumulative_exp_decay"),
                     separator = "_", predictor_table = TRUE)$formula

# sampling - random sampling
set.seed(1234)
samples <- create_resamples(y = dat$use,
                            p = c(0.2, 0.2, 0.2),
                            times = 10,
                            colH0 = NULL)
#> [1] "Starting random sampling..."

# fit multiple models
fittedl <- bag_fit_net_logit(f,
                             data = dat,
                             samples = samples,
                             standardize = "internal", # glmnet does the standardization of covariates
                             metric = "AUC",
                             method = "AdaptiveLasso",
                             parallel = "mclapply",
                             mc.cores = 2)
#> Error in `[.data.frame`(data, , all_covars): undefined columns selected

# bag models in a single object
bag_object <- bag_models(fittedl, dat, score_threshold = 0.7)
#> Error: object 'fittedl' not found

# df
dfvar = data.frame(private_cabins = 1e3*seq(0.2, 20, length.out = 100))

#----
# 1 feature
# prediction
pred <- predict(x = bag_object,
                newdata = dfvar,
                data = dat,
                type = "linear",
                wq_probs = c(0.025, 0.5, 0.975),
                zoi = TRUE,
                n_features = 1,
                baseline = "zero")
#> Error: object 'bag_object' not found

# df with prediction
x <- cbind(dfvar, pred)
#> Error: object 'pred' not found
(tab <- zoi_from_curve(x))
#> Error: object 'x' not found
# (tab_exp <- zoi_from_curve(x, type = "exp"))

# plot

# plot lines
plot(dfvar[,1], pred$`quantile:0.025`, type = "l", col = "grey", xlim = c(0, 5000))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'y' in selecting a method for function 'plot': object 'pred' not found
lines(dfvar[,1], pred$`quantile:0.975`, col = "grey")
#> Error: object 'pred' not found
lines(dfvar[,1], pred$mean, col = "red")
#> Error: object 'pred' not found
lines(dfvar[,1], pred$`quantile:0.5`, col = "black")
#> Error: object 'pred' not found
# plot zoi radius + ci
points(tab[2,1], tab[3,1], pch = 19, cex = 1.5)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'points': object 'tab' not found
lines(tab[2,3:4], tab[3,3:4], lwd = 2)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'lines': object 'tab' not found
# plot max effect size
points(0, tab[1,1], pch = 19, cex = 1.5)
#> Error: object 'tab' not found
lines(c(0, 0), tab[1,3:4], lwd = 2)
#> Error: object 'tab' not found

#----
# 30 features - ZOI radius dos not change
# prediction
pred <- predict(x = bag_object,
                newdata = dfvar,
                data = dat,
                type = "linear",
                wq_probs = c(0.025, 0.5, 0.975),
                zoi = TRUE,
                n_features = 30,
                baseline = "zero")
#> Error: object 'bag_object' not found

# df with prediction
x <- cbind(dfvar, pred)
#> Error: object 'pred' not found
(tab <- zoi_from_curve(x))
#> Error: object 'x' not found
# (tab_exp <- zoi_from_curve(x, type = "exp"))

# plot

# plot lines
plot(dfvar[,1], pred$`quantile:0.025`, type = "l", col = "grey", xlim = c(0, 5000))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'y' in selecting a method for function 'plot': object 'pred' not found
lines(dfvar[,1], pred$`quantile:0.975`, col = "grey")
#> Error: object 'pred' not found
lines(dfvar[,1], pred$mean, col = "red")
#> Error: object 'pred' not found
lines(dfvar[,1], pred$`quantile:0.5`, col = "black")
#> Error: object 'pred' not found
# plot zoi radius + ci
points(tab[2,1], tab[3,1], pch = 19, cex = 1.5)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'points': object 'tab' not found
lines(tab[2,3:4], tab[3,3:4], lwd = 2)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'lines': object 'tab' not found
# plot max effect size
points(0, tab[1,1], pch = 19, cex = 1.5)
#> Error: object 'tab' not found
lines(c(0, 0), tab[1,3:4], lwd = 2)
#> Error: object 'tab' not found

#----
# 1 feature - exponential
# prediction
pred <- predict(x = bag_object,
                newdata = dfvar,
                data = dat,
                type = "linear",
                wq_probs = c(0.025, 0.5, 0.975),
                zoi = TRUE,
                n_features = 1,
                baseline = "zero")
#> Error: object 'bag_object' not found

# df with prediction
x <- cbind(dfvar, pred)
#> Error: object 'pred' not found
(tab <- zoi_from_curve(x, type = "exp"))
#> Error: object 'x' not found

# plot

# plot lines
plot(dfvar[,1], exp(pred$`quantile:0.025`), type = "l", col = "grey", xlim = c(0, 5000), ylim = c(0, 1))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'y' in selecting a method for function 'plot': object 'pred' not found
lines(dfvar[,1], exp(pred$`quantile:0.975`), col = "grey")
#> Error: object 'pred' not found
lines(dfvar[,1], exp(pred$mean), col = "red")
#> Error: object 'pred' not found
lines(dfvar[,1], exp(pred$`quantile:0.5`), col = "black")
#> Error: object 'pred' not found
# plot zoi radius + ci
points(tab[2,1], tab[3,1], pch = 19, cex = 1.5)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'points': object 'tab' not found
lines(tab[2,3:4], tab[3,3:4], lwd = 2)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'lines': object 'tab' not found
# plot max effect size
points(0, tab[1,1], pch = 19, cex = 1.5)
#> Error: object 'tab' not found
lines(c(0, 0), tab[1,3:4], lwd = 2)
#> Error: object 'tab' not found

#----
# 30 features - exponential
# prediction
pred <- predict(x = bag_object,
                newdata = dfvar,
                data = dat,
                type = "linear",
                wq_probs = c(0.025, 0.5, 0.975),
                zoi = TRUE,
                n_features = 30,
                baseline = "zero")
#> Error: object 'bag_object' not found

# df with prediction
x <- cbind(dfvar, pred)
#> Error: object 'pred' not found
(tab <- zoi_from_curve(x, type = "exp"))
#> Error: object 'x' not found

# plot

# plot lines
plot(dfvar[,1], exp(pred$`quantile:0.025`), type = "l", col = "grey", xlim = c(0, 10000), ylim = c(0, 1))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'y' in selecting a method for function 'plot': object 'pred' not found
lines(dfvar[,1], exp(pred$`quantile:0.975`), col = "grey")
#> Error: object 'pred' not found
lines(dfvar[,1], exp(pred$mean), col = "red")
#> Error: object 'pred' not found
lines(dfvar[,1], exp(pred$`quantile:0.5`), col = "black")
#> Error: object 'pred' not found
# plot zoi radius + ci
points(tab[2,1], tab[3,1], pch = 19, cex = 1.5)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'points': object 'tab' not found
lines(tab[2,3:4], tab[3,3:4], lwd = 2)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'lines': object 'tab' not found
# plot max effect size
points(0, tab[1,1], pch = 19, cex = 1.5)
#> Error: object 'tab' not found
lines(c(0, 0), tab[1,3:4], lwd = 2)
#> Error: object 'tab' not found