Moving window regression

前端 未结 1 1155
失恋的感觉
失恋的感觉 2021-01-07 16:14

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, on

1条回答
  •  一生所求
    2021-01-07 16:35

    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)
    

    Unfortunately you will have to run this twice to get both the slope and intercept (coefficients(m)[1]).

    0 讨论(0)
提交回复
热议问题