无法有趣地计算R在栅格堆栈上的逐像素回归 [英] Can't Calculate pixel-wise regression in R on raster stack with fun
问题描述
我正在处理栅格,并且我有一个具有7n层的RasterStack.我想使用下面的公式来计算逐像素回归.我试图使用 raster :: calc
来做到这一点,但是我的函数失败并显示消息:
I am working with rasters and I've a RasterStack with 7n layers. I would like to calculate pixel-wise regression, using formula beneath. I was trying to do it with raster::calc
, but my function failed with message :
'lm.fit(x,y,offset = offset,singular.ok = singular.ok,中的错误...):0(非NA)案件.'
'Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases.'
但是所有栅格都可以,并且包含数字(不仅是NA),我可以绘制它,我可以用公式计算一般线性回归
But all rasters are OK, and contain numbers (not only NAs), I can plot it, and I can calculate general linear regression with formula
cr.sig=lm (raster::as.array(MK_trend.EVI.sig_Only) ~ raster::as.array(stack.pet)+raster::as.array(stack.tmp)+raster::as.array(stack.vap)+raster::as.array(stack.pre)+raster::as.array(stack.wet)+raster::as.array(stack.dtr))
但是当我堆叠层时
allData = stack(MK_trend.EVI.sig_Only,stack.dtr,stack.wet,stack.pre,stack.vap,stack.tmp,stack.pet)
并尝试计算功能
# Regression Function, R2
lmFun=function(x){
x1=as.vector(x);
if (is.na(x1[1])){
NA
} else {
m = lm(x1[1] ~ x1[2]+x1[3]+x1[4]+x1[5]+x1[6]+x1[7])
return(summary(m)$r.squared)
}
}
我看到了错误消息.
我在R和编程方面还很陌生,所以,也许有一些愚蠢的错误?为了使处理工作正常进行,我将不胜感激.
I see the error message.
I am pretty new in R and progranning, so, maybe, there is some silly mistake?
I would appreciate any hint in order to make the processing work.
推荐答案
您可以使用 calc
进行像素级(局部)回归,但是您的公式似乎暗示您需要其他东西(全局模型).
You can use calc
for pixel-wise (local) regression, but your formula seems to suggest you want something else (a global model).
如果回归是像素级的,则每个单元格将具有相等数量的x和y值,并且可以使用 calc
.有关示例,请参见?calc
.
If the regression were pixel wise, you would have an equal number of x and y values for each cell, and you can use calc
. See ?calc
for examples.
相反,每个单元格具有1 y(独立)和6 x(因变量)变量值.这表明您需要一个全局模型.为此,您可以执行以下操作:
Instead you have 1 y (independent) and 6 x (dependent) variable values for each cell. This suggests you want a global model. For that, you can do something like this:
library(raster)
# example data
r <- raster(nrow=10, ncol=10)
set.seed(0)
s <- stack(lapply(1:7, function(i) setValues(r, rnorm(ncell(r), i, 3))))
x <- values(s)
# model
m <- lm(layer.1 ~ ., data=data.frame(x))
# prediction
p <- predict(s, m)
这需要将所有值加载到内存中.如果您不能这样做,则可以进行大量常规采样.参见 sampleRegular
This requires loading all values into memory. If you can not do that, you could take a large regular sample. See sampleRegular
并说明为什么您的方法行不通:
And to show why your approach does not work:
testFun=function(x1){
lm(x1[1] ~ x1[2]+x1[3]+x1[4]+x1[5]+x1[6]+x1[7])
}
# first cell
v <- s[1]
v
# layer.1 layer.2 layer.3 layer.4 layer.5 layer.6 layer.7
#[1,] 4.788863 4.345578 -0.137153 3.626695 3.829971 4.120895 1.936597
m <- testFun(v)
m
#Call:
#lm(formula = x1[1] ~ x1[2] + x1[3] + x1[4] + x1[5] + x1[6] + x1[7])
#Coefficients:
#(Intercept) x1[2] x1[3] x1[4] x1[5] x1[6] x1[7]
# 4.789 NA NA NA NA NA NA
summary(m)$r.squared
# 0
即使我没有收到您报告的错误消息(但所有R ^ 2值均为零).
Even though I do not get the error message you report (but all R^2 values are zero).
这篇关于无法有趣地计算R在栅格堆栈上的逐像素回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!