我试图将Firth的惩罚最大似然估计值与logistf package中实施的逻辑回归拟合到一组栅格,但是我很难用栅格来实现它数字向量。让我们来说明问题:
SendError
现在,我想使用logistf来拟合模型(我有统计学原因,这里没有重现),使用来自raster包的calc。这就是神秘生活的地方。
问题公式的基本原理是每个栅格单元都遵循glm公式:
library(raster)
library(logistf)
# Creating 2 rasters to reproduce what I'm facing
r <- raster(nrow=10, ncol=10)
# binary response raster-variable with 9 bands/layers
s1 <- lapply(1:9, function(i) setValues(r, sample(0:1,ncell(r),replace = T)))
s1 <- stack(s1)
# one explanatory raster-variable
val <- sample(0:60,ncell(r),replace = T)
s2 <- raster(nrow=10, ncol=10,vals=val)
plot(s1)
plot(s2)
# a second explanatory variable as a numeric vector: Nine values just as s1 has 9 bands/layers (not a coincidence)
exp_2 <- c(27.00,30.02,31.07,32.72,33.73,35.12,35.65,36.06,38.32)
...依此类推s1 ~ 27.00 + corresponding cell in s2 + 27.00:corresponding cell in s2
s1 ~ 30.02 + corresponding cell in s2 + 30.02:corresponding cell in s2
s1 ~ 31.07 + corresponding cell in s2 + 31.07:corresponding cell in s2
中的所有9个值。
我试过这样的事情:
exp_2
但显然它不会起作用。可能是因为此数据集中的不同维度/层数。我收到了错误消息
fun <- function(x) { logistf(x ~ exp_2 + s2 + exp_2:s2)$coefficients }
coefs <- calc(s1,fun)
任何想法都会受到赞赏。想法?