Evaluate a grid of half-window width combinations to use for weighted regression
winsrch_grid(dat_in, ...)
# S3 method for default
winsrch_grid(dat_in, grid_in = NULL, ...)
input data object to use with weighted regression
arguments passed to or from other methods
optional input matrix of half-window widths created with createsrch
, a default search grid is used if no input
A data frame of the search grid with associated errors for each cross-validation result. Errors for each grid row are averages of all errors for each fold used in cross-validation.
Processing time can be reduced by setting up a parallel backend, as in the examples. Note that this is not effective for small k-values (e.g., < 4) because each fold is sent to a processor, whereas the window width combinations in grid_in
are evaluated in sequence.
This function should only be used to view the error surface associated with finite combinations of window-width combinations. A faster function to identify the optimal window widths is provided by winsrch_optim
.
if (FALSE) {
##
# setup parallel backend
library(doParallel)
ncores <- detectCores() - 2
registerDoParallel(cores = ncores)
# run search function using default search grid - takes a while
res <- winsrch_grid(tidobjmean)
# view the error surface
library(ggplot2)
ggplot(res, aes(x = factor(mos), y = factor(yrs), fill = err)) +
geom_tile() +
facet_wrap(~ flo) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0,0)) +
scale_fill_gradientn(colours = gradcols())
# optimal combo
res[which.min(res$err), ]
##
# create a custom search grid, e.g. years only
grid_in <- createsrch(mos = 1, yrs = seq(1, 10), flo = 1)
res <- winsrch_grid(tidobjmean, grid_in)
}