Skip to content
Prev 22626 / 29559 Next

Raster Prediction with factors in model

Mark,
I had not considered the case where the constant is a factor. I need
to fix that. Here is a script that accomplishes what you want, I think

# known presence and absence points
p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85,
              66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38,31,
              22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)

a <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
              99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
              37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)

# extract values for points
xy <- rbind(cbind(1, p), cbind(0, a))
v <- data.frame(cbind(xy[,1], extract(logo, xy[,2:3])))
colnames(v)[1] <- 'pa'

#Convert blue layer to a factor
## RH I would not use TRUE/FALSE but an integer representation instead
v$blue <- factor((v$blue < 200) + 0)

#build a model, here an example with glm
model <- glm(formula=pa~., data=v)

#Setup prediction objects
pred.b <- logo
pred.b[[3]] <- (pred.b[[3]] < 200) + 0
# or all TRUE?  pred.b[[3]] <- 1
names(pred.b) <- names(logo)

#predict to a raster
r1 <- predict(pred.b, model, progress='text')


You say you want to use a gam, but the example is a glm. With gam
there might be additional issues. Please let me know if that is the
case.

Robert
On Mon, Apr 20, 2015 at 3:47 AM, Mark Payne <markpayneatwork at gmail.com> wrote: