我试图拟合一个glm
模型,其中y, x1 + x2....xn
是rasterStack
对象中的层。我尝试将光栅堆栈转换为dataframe
对象,但得到了一个向量大小错误,如下所示。相反,我想尝试用光栅层作为输入来拟合回归模型,而不必在文件大小和内存错误的情况下将这些层转换成数据帧。这是可能的吗?你将如何配置它?
我想用行来拟合的模型是自然的:m1<-glm(y1~x1 + x2, family=binomial(), data=layers)
,但是我没有谈到这一点,因为我不能将数据转换成一个数据来进行模型拟合。
dat<-as.data.frame(stack(layers[c(y1,x1,x2)]))
Error: cannot allocate vector of size 40GB
发布于 2016-05-30 10:16:27
下面是使用Raster* data (来自?calc)的一些回归示例:
创建示例数据
r <- raster(nrow=10, ncol=10)
s1 <- lapply(1:12, function(i) setValues(r, rnorm(ncell(r), i, 3)))
s2 <- lapply(1:12, function(i) setValues(r, rnorm(ncell(r), i, 3)))
s1 <- stack(s1)
s2 <- stack(s2)
将一砖(或一叠)中的值与另一砖(或堆叠)中的值回归
s <- stack(s1, s2)
# s1 and s2 have 12 layers; coefficients[2] is the slope
fun <- function(x) { lm(x[1:12] ~ x[13:24])$coefficients[2] }
x1 <- calc(s, fun)
用“时间”对一块砖(或堆栈)中的值进行回归
time <- 1:nlayers(s)
fun <- function(x) { lm(x ~ time)$coefficients[2] }
x2 <- calc(s, fun)
获取多个层,例如斜率和拦截
fun <- function(x) { lm(x ~ time)$coefficients }
x3 <- calc(s, fun)
在某些情况下,一种更快(超过100倍)的方法是直接使用线性代数并预先计算一些常数。
# add 1 for a model with an intercept
X <- cbind(1, time)
# pre-computing constant part of least squares
invXtX <- solve(t(X) %*% X) %*% t(X)
## much reduced regression model; [2] is to get the slope
quickfun <- function(y) (invXtX %*% y)[2]
x4 <- calc(s, quickfun)
https://stackoverflow.com/questions/37452955
复制相似问题