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

时间:2022-01-08 21:24:19

I am trying to fit a sine curve to a datasets and derived the coefficients. As this is my first time dealing with rasterstack, I am not sure how to print the coefficients. Does it possible to represent the coeff in term of raster layer?

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

Thanks in advance.

提前致谢。

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

#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