Hi unmarked users,
which says that the predict function can't handle raster stacks that include factors. There doesn't seem to be anything more recent so I'm wondering if this has changed?
I tried 'un-rasterizing' my raster stack as suggested by Chris Sutherland in that post but am still getting the same error message
Error: ncol(coefficients) == length(obj@estimates) is not TRUE
Here is some of my code to give you an idea of where I might be going wrong here:
height and NDVI are standardized continuous variables while Cover and Soil are factors with various levels
#best model
fm <- occu(~CL ~height + NDVI + Cover + Soil, data=umf)
#resampling rasters to a common extent and projection
ndvi.raster <- resample(ndvi.raster , s, method='ngb')
height.raster<- resample(height.raster, s, method='ngb')
cover.raster<- resample(cover.raster, s, method='ngb')
soil.raster <- resample(soil.raster , s, method='ngb')
#standardizing continuous rasters using original data
ndvi.s <-(ndvi.raster-NDVI.mean)/NDVI.sd)
height.s <-(height.raster-height.mean)/
height.sd)
#stacking all rasters together
ef<-stack(ndvi.s, height.s, cover.raster, soil.raster)
names(ef)<-c("NDVI", "height", "Cover", "Soil")
#taking the anti-logit scale and puts in a binary scale
(beta <- coef(fm, type="state"))
logit.psi <- beta[1] + beta[2]*height.s + beta[3]*ndvi.s
psi <- exp(logit.psi)/(1 + exp(logit.psi))
plot(spplot(psi, col=terrain.colors(100)))
print(spplot(psi, col.regions=terrain.colors(100)))
E.psi <- predict(fm, type="state", newdata=ef)
Error: ncol(coefficients) == length(obj@estimates) is not TRUE
# I tried what Chris Sutherland suggested here and converted my raster stack into a dataframe
efDF<-as.data.frame(values(ef))
E.psi <- predict(fm, type="state", newdata=efDF)
Error: ncol(coefficients) == length(obj@estimates) is not TRUE
#tried something different and got the same error
efDF<-as.data.frame(ef, stringsAsFactors=T)
E.psi <- predict(fm, type="state", newdata=efDF)
Error: ncol(coefficients) == length(obj@estimates) is not TRUE
I appreciate any advice you all may have. Thanks so much for all of your help.
Amy