Learn R Programming

SpatialML (version 1.8.2)

predict.grf: Predict Method for a Geographically Weighted Random Forest

Description

Generates predictions for new observations using a fitted grf object. For each new observation the local random forest fitted at the geographically nearest training location is used. The user can blend local and global predictions through the local.w and global.w weights.

Usage

# S3 method for grf
predict(object, new.data, x.var.name, y.var.name,
                      local.w = 1, global.w = 0, ...)

Value

A numeric vector of predictions of length nrow(new.data).

Arguments

object

an object of class "grf" created by grf with forests = TRUE.

new.data

a data frame containing the predictors required by the model formula and the X and Y coordinates of the new observations.

x.var.name

the name of the column in new.data that contains the X coordinate.

y.var.name

the name of the column in new.data that contains the Y coordinate.

local.w

numeric weight applied to the local model prediction. Default is 1.

global.w

numeric weight applied to the global model prediction. Default is 0.

...

further arguments passed to the ranger predict method (e.g. num.threads).

Author

Stamatis Kalogirou stamatis.science@gmail.com, Stefanos Georganos stefanos.georganos@kau.se

Details

For every row i in new.data the function computes the Euclidean distance between (new.data[i, x.var.name], new.data[i, y.var.name]) and every training location in object$Locations, picks the local random forest fitted at the nearest training location, and combines its prediction with the prediction of the global random forest using the weights local.w and global.w.

References

Georganos, S., Grippa, T., Niang Gadiaga, A., Linard, C., Lennert, M., Vanhuysse, S., Mboga, N., Wolff, E., Kalogirou, S. (2019) Geographical Random Forests: A Spatial Extension of the Random Forest Algorithm to Address Spatial Heterogeneity in Remote Sensing and Population Modelling. Geocarto International, tools:::Rd_expr_doi("10.1080/10106049.2019.1595177").

See Also

grf

Examples

Run this code
  # \donttest{
    set.seed(1)
    RDF <- random.test.data(8, 8, 3)
    Coords <- RDF[, 4:5]
    m <- grf(dep ~ X1 + X2, dframe = RDF, bw = 12,
             kernel = "adaptive", coords = Coords,
             ntree = 100, mtry = 1, nthreads = 1,
             print.results = FALSE, progress = FALSE)

    set.seed(2)
    RDF.Test <- random.test.data(2, 2, 3)
    predict(m, RDF.Test, x.var.name = "X", y.var.name = "Y",
            local.w = 1, global.w = 0)
  # }
  if (FALSE) {
    ## Real-world demonstration on the Greek Income dataset.
    ## Not run by R CMD check because fitting a full GRF on 325
    ## municipalities exceeds the few-second example budget.
    data(Income)
    Coords <- Income[, 1:2]

    m <- grf(Income01 ~ UnemrT01 + PrSect01, dframe = Income, bw = 60,
             kernel = "adaptive", coords = Coords)

    set.seed(123)
    x <- runif(20, min = 142498, max = 1001578)
    y <- runif(20, min = 3855768, max = 4606754)
    u <- runif(20, min = 5,      max = 50)
    p <- runif(20, min = 0,      max = 100)
    f <- runif(20, min = 2,      max = 30)
    df2 <- data.frame(X = x, Y = y, UnemrT01 = u, PrSect01 = p, Foreig01 = f)

    predict(m, df2, x.var.name = "X", y.var.name = "Y",
            local.w = 1, global.w = 0)
  }

Run the code above in your browser using DataLab