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.
if (FALSE) {
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)
}
Run the code above in your browser using DataLab