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 :
'Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases.'
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))
But when I stack layers with
allData = stack(MK_trend.EVI.sig_Only,stack.dtr,stack.wet,stack.pre,stack.vap,stack.tmp,stack.pet)
and try calc function
# 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)
}
}
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.