移动窗口回归 [英] Moving window regression
问题描述
我想在代表Landsat数据的Band3和Band4的两个栅格堆栈的每个像素上执行移动窗口回归.结果应该是另外两个堆栈,一个代表拦截,另一个代表回归的斜率. 因此,堆栈"B3"和堆栈"B4"的第1层将导致堆栈拦截"和堆栈"slope"的第1层.堆栈B3和堆栈B4的第2层产生第2层,.....,依此类推.
I want to perform a moving window regression on every pixel of two raster stacks representing Band3 and Band4 of Landsat data. The result should be two additional stacks, one representing the Intercept and the other one representing the slope of the regression. So layer 1 of stack "B3" and stack "B4" result in layer 1 of stack "intercept" and stack "slope". Layer 2 of stack B3 and stack B4 result in layer 2,.... and so on.
我已经使用了gwr
函数,但是想保留在光栅包中.
我以某种方式知道必须包含focal
才能设置我的移动窗口(应该为3x3像素)以及某种线性模型,例如:lm(as.matrix(b3)~as.matrix(b4))
,尽管我不认为这会以像素为单位来获取值...
I already came along the gwr
function, but want to stay in the raster package.
I somehow know that focal
must be included in order to set my moving window (which should be 3x3 pixels) and somehow a linear model like: lm(as.matrix(b3)~as.matrix(b4))
although I don't think that this gets me the values pixelwise...
代替栅格堆栈,也可以采用逐层方法. (因此,不一定必须是Band3的栅格堆栈.
Instead of a rasterstack a layer by layer approach is also possible. (So it must not necessarily be a rasterstack of Band3.
有人有胶水如何在R中编程吗?
Has anyone a glue how to program this in R?
推荐答案
这是一种方法,改编自?raster :: localFun
Here is one approach, adapted from ?raster::localFun
set.seed(0)
b <- stack(system.file("external/rlogo.grd", package="raster"))
x <- flip(b[[2]], 'y') + runif(ncell(b))
y <- b[[1]] + runif(ncell(b))
# local regression:
rfun <- function(x, y, ...) {
d <- na.omit(data.frame(x, y))
if (nrow(d) < 3) return(NA)
m <- lm(y~x, data=d)
# return slope
coefficients(m)[2]
}
ff <- localFun(x, y, fun=rfun)
plot(ff)
不幸的是,您必须运行两次才能获得斜率和截距(coefficients(m)[1]
).
Unfortunately you will have to run this twice to get both the slope and intercept (coefficients(m)[1]
).
这篇关于移动窗口回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!