| regress | R Documentation |
Run a regression model for each cell of a SpatRaster. The independent variable can be defined either by:
a numeric vector of length nlyr(y) – one covariate that varies across the layers but is constant across cells (e.g. time);
a SpatRaster of the same number of layers as y – one covariate that varies both across layers and across cells;
a data.frame with one row per layer of y and one column per predictor – multiple covariates that vary across layers but are constant across cells (e.g. an experimental design like NPK fertilizer levels).
## S4 method for signature 'SpatRaster,numeric'
regress(y, x, formula=y~x, na.rm=FALSE, cores=1, filename="", overwrite=FALSE, ...)
## S4 method for signature 'SpatRaster,SpatRaster'
regress(y, x, formula=y~x, na.rm=FALSE, cores=1, filename="", overwrite=FALSE, ...)
## S4 method for signature 'SpatRaster,data.frame'
regress(y, x, formula=NULL, na.rm=FALSE, cores=1, filename="", overwrite=FALSE, ...)
y |
SpatRaster. The dependent variable. Each layer is one observation per cell. |
x |
The independent variable(s):
|
formula |
regression formula. For the |
na.rm |
logical. If |
cores |
positive integer. If |
filename |
character. Output filename |
overwrite |
logical. If |
... |
list with named options for writing files as in |
SpatRaster. One layer per regression coefficient, named after the corresponding column of model.matrix(formula, x) (e.g. (Intercept), the names of the predictors, and any interaction or polynomial terms).
s <- rast(system.file("ex/logo.tif", package="terra"))
x <- regress(s, 1:nlyr(s))
# data.frame method: a small NPK fertilizer experiment.
# Suppose `y` has one layer per (N, P, K) treatment.
## Not run:
NPK <- data.frame(
N = c(0, 0, 0, 50, 50, 50, 100, 100, 100),
P = c(0, 25, 50, 0, 25, 50, 0, 25, 50),
K = c(0, 0, 0, 10, 10, 10, 20, 20, 20)
)
y <- rast(ncol=2, nrow=2, nlyr=nrow(NPK))
values(y) <- rep(1:nrow(NPK) * 200, each=4)
# Default formula y ~ N + P + K:
fit <- regress(y, NPK)
# Custom formula with interactions and a quadratic term:
fit2 <- regress(y, NPK, formula = yield ~ N + P + K + I(N^2) + N:P)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.