Calculate values for a moving-window by comparing the value in one layers with the values in one to many other layers. A typical case is the computation of the coefficients for a focal linear regression model.

# S4 method for SpatRaster
focalReg(x, w=3, fun="ols", ..., fillvalue=NA, filename="", overwrite=FALSE, wopt=list())



SpatRaster with at least two layers. The first is the "Y" (dependent) variable and the remainder are the "X" (independent) variables


numeric or matrix to define the focal window. The window an be defined as one (for a square) or two numbers (row, col); or with an odd-sized weights matrix. See the Details section in focal. Note that if a matrix with numbers other than zero or one are used, the values are used as weights. For this to work, fun must have an argument weights


a function with at least two arguments (one for each layer). There is a built-in function "ols" for both the weighted and unweighted Ordinary Least Square regression. This function has an additionals argument na.rm=FALSE and intercept=TRUE


additional arguments for fun


numeric. The value of the cells in the virtual rows and columns outside of the raster


character. Output filename


logical. If TRUE, filename is overwritten


additional arguments for writing files as in writeRaster




r <- rast(ncols=10, nrows=10, ext(0, 10, 0, 10))
values(r) <- 1:ncell(r)
x <- c(r, init(r, runif) * r)
f <- focalReg(x, 3)