如何从R中的rasterstack中提取回归系数?

时间:2013-11-12 22:05:32

标签: r raster r-raster

我正在尝试将正弦曲线拟合到数据集并导出系数。由于这是我第一次处理rasterstack,我不知道如何打印系数。是否可以用栅格图层表示系数?

library(raster)
r <- raster(nrow=5, ncol=5) 
s <- stack( sapply(1:20, function(i) setValues(r, rnorm(ncell(r), i, 3) )) ) 
s[1] <- NA 

time <- 1:nlayers(s) 

fun1 <- function(x) {
if (is.na(x[1])) {
NA
} else {
xcost<-cos(2*pi*time/24)
xsine<-sin(2*pi*time/24)
m = lm(x~xcost+xsine)
m$coefficients[2]
}
}

e1 <- calc(s, fun1)

提前致谢。

1 个答案:

答案 0 :(得分:1)

> e1[1,1]

NA 
> e1[1,2]

-1.810707 
> as.matrix(e1)
           [,1]      [,2]      [,3]      [,4]       [,5]
[1,]         NA -1.810707 -1.619409 -1.862829 -0.8928249
[2,] -4.4466952 -2.763915 -1.095858 -1.513916 -3.2462229
[3,] -0.5758164 -5.258762 -3.567696 -3.051799 -2.6290404
[4,] -2.9026886 -3.929019 -1.832168 -1.308834 -3.4590392
[5,] -3.6900736 -1.223912 -4.581699 -2.188620 -3.8804321