| Title: | Model-Based Boosting with Gaussian Process Spatial Effects |
|---|---|
| Description: | Componentwise gradient boosting for point-referenced spatial data, extending 'mboost'. In every boosting iteration, a Gaussian process spatial effect is re-estimated by (restricted) maximum likelihood with 'spmodel', and the base-learners are fitted to the residual after removing it. Predictions at new locations are obtained by kriging. A threshold on the spatial share of the residual variance allows the spatial effect to be dropped when it is negligible. Includes spatial and random cross-validation for choosing the number of boosting iterations. |
| Authors: | Lars Knieper [aut, cre] |
| Maintainer: | Lars Knieper <[email protected]> |
| License: | GPL-2 |
| Version: | 0.2.0 |
| Built: | 2026-12-02 21:14:57 UTC |
| Source: | https://github.com/cran/spmboost |
Builds a fold matrix for spat_cvrisk(): spatial folds via
spatialsample, or ordinary random k-fold CV via mboost::cv().
## S3 method for class 'spat_cv_boost' autoplot(object, ...) spat_cv( data, k = 10, coords = NULL, cv_type = c("spatial", "block", "buffered", "loo", "random"), crs = NA, loo_group = NULL, radius = NULL, buffer = NULL, ... )## S3 method for class 'spat_cv_boost' autoplot(object, ...) spat_cv( data, k = 10, coords = NULL, cv_type = c("spatial", "block", "buffered", "loo", "random"), crs = NA, loo_group = NULL, radius = NULL, buffer = NULL, ... )
object |
A fold matrix of class |
... |
Further arguments passed to the spatialsample function
(used for |
data |
A |
k |
Number of folds. |
coords |
Character vector of length 2: names of the x- and
y-coordinate columns in |
cv_type |
Type of cross-validation:
|
crs |
Coordinate reference system used when converting a
|
loo_group |
Name of the grouping column for |
radius, buffer
|
For |
A numeric N x k matrix of class "spat_cv_boost", one column per
fold, with 1 = training and 0 = test observation (and -1 =
excluded, for the buffer of cv_type = "buffered"). Every observation
is in exactly one test fold, except for "buffered" with radius > 0,
where test folds can overlap. For the spatial types, the underlying
spatialsample object is attached as attribute "cv_object", which
autoplot() uses to plot the folds (not available for "random").
set.seed(1) dat <- data.frame(sx = runif(200), sy = runif(200), y = rnorm(200)) # spatial clustering CV and ordinary random CV, 5 folds each f_sp <- spat_cv(dat, k = 5, coords = c("sx", "sy")) f_rd <- spat_cv(dat, k = 5, cv_type = "random") colSums(f_sp == 0) # test-fold sizes # buffered CV: points within 0.1 of a test fold are left out of training f_bf <- spat_cv(dat, k = 5, coords = c("sx", "sy"), cv_type = "buffered", radius = 0.05, buffer = 0.1) table(f_bf[, 1]) # -1 = excluded, 0 = test, 1 = training # plot the spatial folds spatialsample::autoplot(f_sp)set.seed(1) dat <- data.frame(sx = runif(200), sy = runif(200), y = rnorm(200)) # spatial clustering CV and ordinary random CV, 5 folds each f_sp <- spat_cv(dat, k = 5, coords = c("sx", "sy")) f_rd <- spat_cv(dat, k = 5, cv_type = "random") colSums(f_sp == 0) # test-fold sizes # buffered CV: points within 0.1 of a test fold are left out of training f_bf <- spat_cv(dat, k = 5, coords = c("sx", "sy"), cv_type = "buffered", radius = 0.05, buffer = 0.1) table(f_bf[, 1]) # -1 = excluded, 0 = test, 1 = training # plot the spatial folds spatialsample::autoplot(f_sp)
Returns the spatial part of a fitted spmboost() model for one boosting
iteration: the fitted spatial effect at the training locations, the
estimated spatial covariance parameters, and whether the spatial effect was
included in that iteration (see spat_threshold in spmboost()).
extract_spatial(object, iteration = mstop(object))extract_spatial(object, iteration = mstop(object))
object |
A fitted model of class |
iteration |
Boosting iteration to extract, between 1 and
|
For the spatial effect at new locations use
predict(object, newdata, spatial = "only"), which krigs it.
A list with elements
fittedNumeric vector (one value per training observation) of
the fitted spatial effect at the training locations. All
zeros if the spatial effect was not included in this iteration.
SpatCovEstimated spatial covariance parameters from
spmodel::splm() (class of the covariance type): de (partial sill,
), ie (nugget, ), range, plus
rotate/scale (anisotropy) and extra where applicable. Both de
and ie are variances. Reported even if the spatial effect was not
included.
shareSpatial share of the residual variance,
.
includedLogical: was the spatial effect included in this
iteration, i.e. share >= spat_threshold?
spmboost(), predict.spmboost()
set.seed(1) n <- 100 dat <- data.frame(sx = runif(n), sy = runif(n), x1 = runif(n)) D <- as.matrix(dist(dat[, c("sx", "sy")])) dat$y <- sin(2 * pi * dat$x1) + drop(t(chol(exp(-D / 0.3))) %*% rnorm(n)) + rnorm(n, sd = 0.3) mod <- spmboost(y ~ bbs(x1), data = dat, spcov_type = "exponential", xcoord = "sx", ycoord = "sy", control = boost_control(mstop = 20)) sp <- extract_spatial(mod) sp$SpatCov # partial sill (de), nugget (ie), range sp$share # spatial share of the residual variance # estimated partial sill along the boosting path plot(sapply(1:20, function(m) extract_spatial(mod, m)$SpatCov[["de"]]), type = "l", xlab = "iteration", ylab = "partial sill")set.seed(1) n <- 100 dat <- data.frame(sx = runif(n), sy = runif(n), x1 = runif(n)) D <- as.matrix(dist(dat[, c("sx", "sy")])) dat$y <- sin(2 * pi * dat$x1) + drop(t(chol(exp(-D / 0.3))) %*% rnorm(n)) + rnorm(n, sd = 0.3) mod <- spmboost(y ~ bbs(x1), data = dat, spcov_type = "exponential", xcoord = "sx", ycoord = "sy", control = boost_control(mstop = 20)) sp <- extract_spatial(mod) sp$SpatCov # partial sill (de), nugget (ie), range sp$share # spatial share of the residual variance # estimated partial sill along the boosting path plot(sapply(1:20, function(m) extract_spatial(mod, m)$SpatCov[["de"]]), type = "l", xlab = "iteration", ylab = "partial sill")
In-sample fitted values on the response scale, including the spatial
effect, i.e. predict(object, type = "response").
## S3 method for class 'spmboost' fitted(object, spatial = TRUE, ...)## S3 method for class 'spmboost' fitted(object, spatial = TRUE, ...)
object |
A fitted model of class |
spatial |
Include the spatial effect ( |
... |
Further arguments passed to |
Numeric vector of fitted values, one per training observation.
Predicts the additive predictor of an spmboost() model, by default
including the spatial effect, which is kriged to new locations.
## S3 method for class 'spmboost' predict( object, newdata = NULL, spatial = TRUE, type = c("link", "response", "class"), which = NULL, aggregate = c("sum", "cumsum", "none"), local = NULL, ... )## S3 method for class 'spmboost' predict( object, newdata = NULL, spatial = TRUE, type = c("link", "response", "class"), which = NULL, aggregate = c("sum", "cumsum", "none"), local = NULL, ... )
object |
A fitted model of class |
newdata |
Optional |
spatial |
What to do with the spatial effect: |
type |
Scale of the predictions: |
which |
Optional subset of base-learners (names or indices). Then,
as in |
aggregate |
|
local |
Optional integer |
... |
Further arguments passed to |
The predictor at iteration is
with the offset plus all base-learner contributions,
the intercept of the spatial model and
the spatial effect, kriged from the training
locations with that iteration's covariance parameters. If the spatial
effect was excluded in an iteration (see spat_threshold in
spmboost()), .
A numeric vector (or a matrix for aggregate = "cumsum" and
"none", as in mboost).
set.seed(1) n <- 100 dat <- data.frame(sx = runif(n), sy = runif(n), x1 = runif(n)) D <- as.matrix(dist(dat[, c("sx", "sy")])) dat$y <- sin(2 * pi * dat$x1) + drop(t(chol(exp(-D / 0.3))) %*% rnorm(n)) + rnorm(n, sd = 0.3) mod <- spmboost(y ~ bbs(x1), data = dat, spcov_type = "exponential", xcoord = "sx", ycoord = "sy", control = boost_control(mstop = 20)) new <- data.frame(sx = c(0.2, 0.8), sy = c(0.5, 0.5), x1 = 0.5) predict(mod, newdata = new) # with spatial effect predict(mod, newdata = new, spatial = FALSE) # without predict(mod, newdata = new, spatial = "only") # spatial effect alone dim(predict(mod, newdata = new, aggregate = "cumsum"))set.seed(1) n <- 100 dat <- data.frame(sx = runif(n), sy = runif(n), x1 = runif(n)) D <- as.matrix(dist(dat[, c("sx", "sy")])) dat$y <- sin(2 * pi * dat$x1) + drop(t(chol(exp(-D / 0.3))) %*% rnorm(n)) + rnorm(n, sd = 0.3) mod <- spmboost(y ~ bbs(x1), data = dat, spcov_type = "exponential", xcoord = "sx", ycoord = "sy", control = boost_control(mstop = 20)) new <- data.frame(sx = c(0.2, 0.8), sy = c(0.5, 0.5), x1 = 0.5) predict(mod, newdata = new) # with spatial effect predict(mod, newdata = new, spatial = FALSE) # without predict(mod, newdata = new, spatial = "only") # spatial effect alone dim(predict(mod, newdata = new, aggregate = "cumsum"))
mstop() returns the CV-optimal number of boosting iterations; plot()
shows the out-of-sample risk of every fold (grey), their average (black)
and the optimal iteration (dashed line).
## S3 method for class 'spat_cv' mstop(object, ...) ## S3 method for class 'spat_cv' plot(x, main = "CV Risks", ...)## S3 method for class 'spat_cv' mstop(object, ...) ## S3 method for class 'spat_cv' plot(x, main = "CV Risks", ...)
object, x
|
An object of class |
... |
Not used ( |
main |
Plot title. |
mstop(): the CV-optimal number of iterations. plot(): called
for its side effect.
For every fold, refits spmboost() on the training part with the settings
of object, predicts the test part for every iteration 1, ..., mstop(object),
and evaluates the out-of-sample risk. The iteration with the lowest average
risk is the CV-optimal mstop.
spat_cvrisk(object, folds, cores = 1, include_spatial = FALSE)spat_cvrisk(object, folds, cores = 1, include_spatial = FALSE)
object |
A fitted model of class |
folds |
Fold matrix, typically from |
cores |
Number of cores; folds are run in parallel via
|
include_spatial |
Logical. If |
Each refit uses the same boost_formula, spcov_type,
coordinates, estmethod, spat_threshold, spmodel_control, offset,
family, control and baselearner as object. Arguments that were
passed to spmboost() via ... or weights are not stored and
therefore not used in the refits.
The risk per iteration is the squared error for the Gaussian family and half the AIC (i.e. the negative log-likelihood) otherwise, divided by the size of the test fold, so folds of different sizes are comparable.
mboost::cvrisk() cannot be used for spmboost models.
An object of class "spat_cv" with elements
cv_risksmstop x k matrix of out-of-sample risks per
iteration and fold.
avg_risksAverage risk per iteration (row means of
cv_risks).
m_optCV-optimal number of iterations, also returned by
mstop().
foldsThe fold matrix used.
plot() shows the risk paths of all folds and their average.
set.seed(1) n <- 60 dat <- data.frame(sx = runif(n), sy = runif(n), x1 = runif(n)) D <- as.matrix(dist(dat[, c("sx", "sy")])) dat$y <- sin(2 * pi * dat$x1) + drop(t(chol(exp(-D / 0.3))) %*% rnorm(n)) + rnorm(n, sd = 0.3) mod <- spmboost(y ~ bbs(x1), data = dat, spcov_type = "exponential", xcoord = "sx", ycoord = "sy", control = boost_control(mstop = 20)) folds <- spat_cv(dat, k = 3, cv_type = "random") cvr <- spat_cvrisk(mod, folds, include_spatial = TRUE) mstop(cvr) plot(cvr) # set the model to the CV-optimal number of iterations # (same result as refitting with mstop = mstop(cvr)) mod <- mod[mstop(cvr)]set.seed(1) n <- 60 dat <- data.frame(sx = runif(n), sy = runif(n), x1 = runif(n)) D <- as.matrix(dist(dat[, c("sx", "sy")])) dat$y <- sin(2 * pi * dat$x1) + drop(t(chol(exp(-D / 0.3))) %*% rnorm(n)) + rnorm(n, sd = 0.3) mod <- spmboost(y ~ bbs(x1), data = dat, spcov_type = "exponential", xcoord = "sx", ycoord = "sy", control = boost_control(mstop = 20)) folds <- spat_cv(dat, k = 3, cv_type = "random") cvr <- spat_cvrisk(mod, folds, include_spatial = TRUE) mstop(cvr) plot(cvr) # set the model to the CV-optimal number of iterations # (same result as refitting with mstop = mstop(cvr)) mod <- mod[mstop(cvr)]
Fits a componentwise gradient boosting model (via mboost::mboost()) in
which a Gaussian-process spatial effect is re-estimated in every boosting
iteration by spmodel::splm().
spmboost( boost_formula, data = list(), spcov_type = "gaussian", xcoord = NULL, ycoord = NULL, estmethod = "reml", spat_threshold = 0, spmodel_control = list(), na.action = na.omit, weights = NULL, offset = NULL, family = gaussian(), control = boost_control(), oobweights = NULL, baselearner = c("bbs", "bols", "btree", "bss", "bns"), verbose = FALSE, ... )spmboost( boost_formula, data = list(), spcov_type = "gaussian", xcoord = NULL, ycoord = NULL, estmethod = "reml", spat_threshold = 0, spmodel_control = list(), na.action = na.omit, weights = NULL, offset = NULL, family = gaussian(), control = boost_control(), oobweights = NULL, baselearner = c("bbs", "bols", "btree", "bss", "bns"), verbose = FALSE, ... )
boost_formula |
Formula for the boosted (non-spatial) part, as in
|
data |
A |
spcov_type |
Spatial covariance type passed to |
xcoord, ycoord
|
Names of the x- and y-coordinate columns in |
estmethod |
Estimation method for the spatial covariance parameters,
passed to |
spat_threshold |
Threshold |
spmodel_control |
Named list of further arguments for
|
na.action, weights, offset, control, oobweights, baselearner, ...
|
Arguments passed to |
family |
Response distribution. Currently only |
verbose |
Logical; if |
An object of class c("spmboost", "mboost"). Besides the usual
mboost methods, see predict.spmboost() for predictions including the
kriged spatial effect and extract_spatial() for the spatial fit.
spat_cv() and spat_cvrisk() for choosing mstop,
extract_spatial()
set.seed(1) n <- 100 dat <- data.frame(sx = runif(n), sy = runif(n), x1 = runif(n), x2 = rnorm(n)) # spatial effect: Gaussian process with exponential covariance D <- as.matrix(dist(dat[, c("sx", "sy")])) s <- drop(t(chol(exp(-D / 0.3))) %*% rnorm(n)) dat$y <- 1 + sin(2 * pi * dat$x1) + 0.5 * dat$x2 + s + rnorm(n, sd = 0.3) mod <- spmboost(y ~ bbs(x1) + bols(x2), data = dat, spcov_type = "exponential", xcoord = "sx", ycoord = "sy", control = boost_control(mstop = 30)) # estimated covariance parameters (de = partial sill, ie = nugget) and # spatial share of the residual variance extract_spatial(mod)[c("SpatCov", "share")] # selected base-learners and partial effect of x1 table(selected(mod)) plot(mod, which = "x1") # predictions at new locations: full predictor and spatial effect only new <- data.frame(sx = c(0.2, 0.8), sy = c(0.5, 0.5), x1 = 0.5, x2 = 0) predict(mod, newdata = new) predict(mod, newdata = new, spatial = "only")set.seed(1) n <- 100 dat <- data.frame(sx = runif(n), sy = runif(n), x1 = runif(n), x2 = rnorm(n)) # spatial effect: Gaussian process with exponential covariance D <- as.matrix(dist(dat[, c("sx", "sy")])) s <- drop(t(chol(exp(-D / 0.3))) %*% rnorm(n)) dat$y <- 1 + sin(2 * pi * dat$x1) + 0.5 * dat$x2 + s + rnorm(n, sd = 0.3) mod <- spmboost(y ~ bbs(x1) + bols(x2), data = dat, spcov_type = "exponential", xcoord = "sx", ycoord = "sy", control = boost_control(mstop = 30)) # estimated covariance parameters (de = partial sill, ie = nugget) and # spatial share of the residual variance extract_spatial(mod)[c("SpatCov", "share")] # selected base-learners and partial effect of x1 table(selected(mod)) plot(mod, which = "x1") # predictions at new locations: full predictor and spatial effect only new <- data.frame(sx = c(0.2, 0.8), sy = c(0.5, 0.5), x1 = 0.5, x2 = 0) predict(mod, newdata = new) predict(mod, newdata = new, spatial = "only")