[R-sig-Geo] raster::predict coercing factors
Tyler D. Rudolph
tylerdrudolph at gmail.com
Tue Oct 18 22:06:01 CEST 2016
I have used a data set consisting of continuous and categorical variables
to build a statistical (glmer) model. I now wish to produce a spatial map
of model predictions given landscape conditions where the values of all
independent variables are known and introduced to the raster::predict
function as a raster stack via the newdata argument . Variables 12 to 29
should appear as factors and indeed check out that way:
all(sapply(12:29, function(i) is.factor(newdata.stack[[i]])))
[1] TRUE
However, when I run the function it invariably tells me these variables are
not factors:
predict(obj=newdata.stack, model=mymodel, allow.new.levels=TRUE,
type="response")
Error in `contrasts<-`(`*tmp*`, value = contrasts.arg[[nn]]) :
contrasts apply only to factors
In addition: There were 12 warnings (use warnings() to see them)
There is subsequently one warning for every categorical variable in the
model formula (the remaining 7 were excluded from this model. The first
warning is about ignoring unused variables.). E.g.:
5: In model.frame.default(delete.response(Terms), newdata, ... :
variable 'opine_0_14_nat' is not a factor
This is a problem I have never encountered before during this kind of
operation. It seems the factor class attributed to those categorical raster
layers is somehow coerced to something else once the call is passed. That
or there is something else I am overlooking?
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list