View source: R/bag_plot_response.R
plot_response | R Documentation |
This function takes a bag of models (x
) and a set of new data (dfvar
) with variation for one or more
specific predictor variables to predict and plot the predictions from the bag. One can either plot only the
mean or (weighted) median response for specific preditor variables, and possibly also the
confidence interval, computed from the weighted quantiles of the prediction. All other variables
are kept constant, as defined by the baseline
parameter.
plot_response(
x,
dfvar,
data,
type = c("linear", "exponential", "logit", "cloglog")[1],
zoi_shape = c("exp_decay", "gaussian_decay", "linear_decay", "threshold_decay")[1],
which_cumulative = "cumulative",
ci = TRUE,
indiv_pred = FALSE,
wq_probs = c(0.025, 0.5, 0.975),
baseline = c("median", "mean", "zero")[1],
zoi = FALSE,
zoi_vals = c(100, 250, 500, 1000, 2500, 5000, 10000),
type_feature = c("point", "line", "area")[1],
type_feature_recompute = FALSE,
zoi_limit = 0.05,
resolution = 100,
line_value = 1,
ggplot = T,
plot_mean = TRUE,
plot_median = TRUE,
n_features = 1,
normalize = c(FALSE, "mean", "median", "ci")[1],
logx = FALSE,
ylim = NULL,
y_lab = "Relative Selection Strength",
col_ci = "grey",
col_indiv = "grey",
col_mean = "black",
col_median = "red",
linewidth_indiv = 1.2,
linewidth_mean = 1.2,
linewidth_median = 1.2,
alpha_ci = 0.5,
alpha_indiv = 0.3
)
x |
|
dfvar |
|
data |
|
type |
|
zoi_shape |
|
ci |
Should variation or confidence intervals be plotted? |
wq_probs |
|
The function plot_response
uses the predict
to produce the predictions.
predict()
#---
# fit a bag to be tested
# load packages
library(glmnet)
library(ggplot2)
# 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 +
trails_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)
# 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)
# bag models in a single object
bag_object <- bag_models(fittedl, dat, score_threshold = 0.7)
#---
# plot predictions for non-ZOI variables
# plot for PCA1
dfvar <- data.frame(norway_pca_klima_axis1 = seq(min(bag_object$data_summary$norway_pca_klima_axis1),
max(bag_object$data_summary$norway_pca_klima_axis1),
length.out = 100))
dfvar$norway_pca_klima_axis1_sq = dfvar$norway_pca_klima_axis1**2
# plot mean response in linear scale with weighted interquartile range
plot_response(bag_object,
dfvar = dfvar,
data = dat,
wq_probs = c(0.25, 0.5, 0.75),
plot_median = FALSE) # remove median, plot only weighted mean
# plot median response in exponential scale with weighted interquartile range
plot_response(bag_object,
dfvar = dfvar,
data = dat,
type = "exp",
wq_probs = c(0.25, 0.5, 0.75),
plot_mean = FALSE) # remove mean, plot only weighted median
#---
# plot predictions for ZOI variables
# plot for private cabins
# define newdata based only on the distances from the source (public cabins)
dfvar = data.frame(private_cabins = 1e3*seq(0.2, 20, length.out = 100))
# plot mean response in exponential scale, with individual lines, x in log scale
# in exponential scale, relative selection strength = 1 corresponse to no effect
# prediction for for 1 cabin only
plot_response(bag_object,
dfvar = dfvar,
data = dat,
type = "exp",
zoi = TRUE,
ci = FALSE,
indiv_pred = TRUE,
logx = TRUE,
ylim = ylim(0, 2))
# prediction for for 10 cabins located at the origin
plot_response(bag_object,
dfvar = dfvar,
data = dat,
type = "exp",
zoi = TRUE,
n_features = 10,
ci = FALSE,
indiv_pred = TRUE,
logx = TRUE,
ylim = ylim(0, 2))
#---
# plot predictions for linear ZOI variables
# plot for tourist trails
# define newdata based only on the distances from the source (public cabins)
dfvar = data.frame(trails = 1e3*seq(0.2, 20, length.out = 100))
# plot mean response in exponential scale, with individual lines, x in log scale
# in exponential scale, relative selection strength = 1 corresponse to no effect
# prediction for for 1 cabin only
plot_response(bag_object,
dfvar = dfvar,
data = dat,
type = "exp",
zoi = TRUE,
ci = FALSE,
indiv_pred = TRUE,
logx = TRUE,
ylim = ylim(0, 2))
# prediction for for 10 cabins located at the origin
plot_response(bag_object,
dfvar = dfvar,
data = dat,
type = "exp",
zoi = TRUE,
n_features = 10,
ci = FALSE,
indiv_pred = TRUE,
logx = TRUE,
ylim = ylim(0, 2))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.