我正在尝试安装 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
最佳答案
以下是一些使用 Raster* 数据的回归示例(来自 ?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)
一 block 砖(或一堆)中的值与另一 block 砖的回归
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)
一 block 砖(或一堆)中的值与“时间”的回归
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)
关于r - 大型栅格数据集的像素级回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37452955/