2

我正在尝试将正弦曲线拟合到数据集并导出系数。由于这是我第一次处理 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)

提前致谢。

4

1 回答 1

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
于 2013-11-12T22:50:20.900 回答