Skip to contents

Fits a Quantile Regression Forest (Meinshausen 2006) on the currently labeled soil dataset, using ranger::ranger(..., quantreg = TRUE). The resulting object carries the data, model, and an initial history entry and is the value on which al_query(), al_update() and al_loop() operate.

Usage

al_fit(labeled, target, covariates, coords = NULL, num.trees = 500L, ...)

Arguments

labeled

Data frame with observed target + covariates (+ optional coordinates).

target

Character, name of the target column.

covariates

Character vector of covariate column names.

coords

Optional length-2 character vector naming the x/y coordinate columns. Required by the "cost" query strategy.

num.trees

Integer, number of trees (default 500). Because the forest is fit with quantreg = TRUE, ranger requires every observation to be out-of-bag in at least 10 trees; al_fit() rejects a value too small to make that reliable for nrow(labeled) rows (about 88 trees for 30 rows, rising slowly with n) rather than letting ranger fail on some random seeds.

...

Additional arguments forwarded to ranger::ranger(). Note seed: ranger draws its own random seed when none is given, so without it every call grows a different forest and reports a different out-of-bag RMSE.

Value

A edaphos_al_model object.

References

Meinshausen N (2006). Quantile Regression Forests. Journal of Machine Learning Research 7, 983-999.

Examples

# \donttest{
  if (requireNamespace("sp", quietly = TRUE)) {
    data(meuse, package = "sp")
    d <- stats::na.omit(meuse[1:30, ])   # 29 rows survive na.omit()

    # `seed` is forwarded to ranger::ranger(). Without it the 500-tree
    # forest -- and the "last RMSE" printed below -- differs on every
    # run; with it, repeated calls print the same number.
    m <- al_fit(
      labeled    = d,
      target     = "lead",
      covariates = c("dist", "elev"),
      coords     = c("x", "y"),
      seed       = 42
    )
    print(m)

    # That RMSE is not evidence of a useful model: it is an out-of-bag
    # estimate from 29 points and 2 covariates. Compare it with the RMSE
    # of simply predicting the mean of the 29 labeled values.
    print(c(oob       = m$history[[1]]$rmse_oob,
            mean_only = sqrt(mean((d$lead - mean(d$lead))^2))))
    # A model worth acting on needs hundreds of labeled sites and
    # spatially blocked validation -- run al_loop() to add points.
  }
#> <edaphos_al_model>
#>   target     : lead 
#>   covariates : dist, elev 
#>   coords     : x, y 
#>   n labeled  : 29 
#>   iterations : 0 
#>   last RMSE  : 42.11 
#>       oob mean_only 
#>  42.11348  66.54924 
# }