[R-sig-Geo] Unable to use factors in raster::predict
Gonzalez-Mirelis, Genoveva
genovev@@gonz@lez-mireli@ @ending from hi@no
Tue Nov 20 10:22:38 CET 2018
Dear list,
I would like to use the 'predict' function in the 'raster' package in an implementation of species distribution modelling with a couple of factor variables. My case can be set up exactly as the cforest example listed in the help file. Unfortunately, I cannot get the example to work: it throws an error because of the unused argument 'factors', which I need. Here is the code I am referring to:
# create a RasterStack or RasterBrick with with a set of predictor layers
logo <- brick(system.file("external/rlogo.grd", package="raster"))
names(logo)
# 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(pa=xy[,1], extract(logo, xy[,2:3])))
# cforest (other Random Forest implementation) example with factors argument
v$red <- as.factor(round(v$red/100))
logo$red <- round(logo[[1]]/100)
library(party)
m <- cforest(pa~., control=cforest_unbiased(mtry=3), data=v)
f <- list(levels(v$red))
names(f) <- 'red'
pc <- predict( m,logo, OOB=TRUE, factors=f)
Error in RET using prediction_weights(newdata = newdata, mincriterion = mincriterion, :
unused argument (factors = f)
Note that I needed to change the order in the arguments in the last line. If I run it the way it was in the example, like this:
pc <- predict(logo, m, OOB=TRUE, factors=f)
then the error is the following:
Error in v[cells, ] <- predv :
number of items to replace is not a multiple of replacement length
Also note that if I run the line without the 'factors' argument the resulting value (r) is a matrix, and not a raster object.
I am using Package 'raster' version 2.6-7 on Windows 7 (where this worked fine in the past).
Many thanks for any help,
Genoveva
Genoveva Gonzalez Mirelis, Scientist
Institute of Marine Research
Nordnesgaten 50
5005 Bergen, Norway
Phone number +47 55238510
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list