Dear list,
I posted this question recently on another list so I apologize for any cross-posting. Still no solution.
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. Furthermore, I would like to set this up exactly as the cforest example listed in the help file. Unfortunately, I cannot get the example to work!
# 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(logo, m, OOB=TRUE, factors=f)
# Error in v[cells, ] <- predv :
# number of items to replace is not a multiple of replacement length
# If you change the order of the first two arguments (I read this somewhere) then the error changes, like this:
pc <- predict(m, logo, OOB=TRUE, factors=f)
# Error in RET at prediction_weights(newdata = newdata, mincriterion = mincriterion, :
# unused argument (factors = f)
# Lastly, if I run the line without the 'factors' argument
pc <- predict(m, logo, OOB=TRUE)
# Then I get no errors, but I don't understand the result. It's a vector of 40 values (predictions?)
I am using Package 'raster' version 2.6-7 in RStudio 1.1.453, running off a server. I have tried the on several other computers, though, and the error persisted.
Many thanks for any help,
Genoveva
Genoveva Gonzalez Mirelis, Scientist
Institute of Marine Research
Nordnesgaten 50
5005 Bergen, Norway
Phone number +47 55238510
Genoveva Gonzalez Mirelis, Scientist
Institute of Marine Research
Nordnesgaten 50
5005 Bergen, Norway
Phone number +47 55238510
raster::predict example doesn't work?
3 messages · Gonzalez-Mirelis, Genoveva, Bede-Fazekas Ákos
Dear Genoveva, All the examples in the help file of package 'party' (https://cran.r-project.org/web/packages/party/party.pdf) use the function predict() from package 'stats' instead of package 'raster'. But converting the RasterStack to data.frame, changing the column 'red' from numeric to factor, using stats::predict(..., newdata), and create a new layer of the RasterStack can solve the problem. logo_df <- as.data.frame(logo) logo_df$red <- factor(logo_df$red, levels = levels(v$red)) pc <- stats::predict(m, OOB = TRUE, newdata = logo_df) logo$pc <- pc HTH, ?kos Bede-Fazekas Hungarian Academy of Sciences 2018.12.04. 15:52 keltez?ssel, Gonzalez-Mirelis, Genoveva ?rta:
Dear list,
I posted this question recently on another list so I apologize for any cross-posting. Still no solution.
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. Furthermore, I would like to set this up exactly as the cforest example listed in the help file. Unfortunately, I cannot get the example to work!
# 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(logo, m, OOB=TRUE, factors=f)
# Error in v[cells, ] <- predv :
# number of items to replace is not a multiple of replacement length
# If you change the order of the first two arguments (I read this somewhere) then the error changes, like this:
pc <- predict(m, logo, OOB=TRUE, factors=f)
# Error in RET at prediction_weights(newdata = newdata, mincriterion = mincriterion, :
# unused argument (factors = f)
# Lastly, if I run the line without the 'factors' argument
pc <- predict(m, logo, OOB=TRUE)
# Then I get no errors, but I don't understand the result. It's a vector of 40 values (predictions?)
I am using Package 'raster' version 2.6-7 in RStudio 1.1.453, running off a server. I have tried the on several other computers, though, and the error persisted.
Many thanks for any help,
Genoveva
Genoveva Gonzalez Mirelis, Scientist
Institute of Marine Research
Nordnesgaten 50
5005 Bergen, Norway
Phone number +47 55238510
Genoveva Gonzalez Mirelis, Scientist
Institute of Marine Research
Nordnesgaten 50
5005 Bergen, Norway
Phone number +47 55238510
[[alternative HTML version deleted]]
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Thank you ?kos, your solution does work. One caveat, though: the 'predict' function from 'stats' returns a matrix, whereas that from the 'raster' package returns a raster object of identical extent and resolution as the supplied predictor stack, which is very handy. If no other solutions arise I will just convert that matrix to a raster as follows: r <- raster(ncol=ncol(logo), nrow=nrow(logo)) extent(r) <- extent(logo) values(r) <- pc -----Original Message----- From: R-sig-ecology <r-sig-ecology-bounces at r-project.org> On Behalf Of Bede-Fazekas ?kos Sent: 4. desember 2018 16:50 To: r-sig-ecology at r-project.org; R-sig-geo <r-sig-geo at r-project.org> Subject: Re: [R-sig-eco] raster::predict example doesn't work? Dear Genoveva, All the examples in the help file of package 'party' (https://cran.r-project.org/web/packages/party/party.pdf) use the function predict() from package 'stats' instead of package 'raster'. But converting the RasterStack to data.frame, changing the column 'red' from numeric to factor, using stats::predict(..., newdata), and create a new layer of the RasterStack can solve the problem. logo_df <- as.data.frame(logo) logo_df$red <- factor(logo_df$red, levels = levels(v$red)) pc <- stats::predict(m, OOB = TRUE, newdata = logo_df) logo$pc <- pc HTH, ?kos Bede-Fazekas Hungarian Academy of Sciences 2018.12.04. 15:52 keltez?ssel, Gonzalez-Mirelis, Genoveva ?rta:
Dear list,
I posted this question recently on another list so I apologize for any cross-posting. Still no solution.
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. Furthermore, I would like to set this up exactly as the cforest example listed in the help file. Unfortunately, I cannot get the example to work!
# 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(logo, m, OOB=TRUE, factors=f)
# Error in v[cells, ] <- predv :
# number of items to replace is not a multiple of replacement length
# If you change the order of the first two arguments (I read this somewhere) then the error changes, like this:
pc <- predict(m, logo, OOB=TRUE, factors=f)
# Error in RET at prediction_weights(newdata = newdata, mincriterion = mincriterion, :
# unused argument (factors = f)
# Lastly, if I run the line without the 'factors' argument
pc <- predict(m, logo, OOB=TRUE)
# Then I get no errors, but I don't understand the result. It's a
vector of 40 values (predictions?)
I am using Package 'raster' version 2.6-7 in RStudio 1.1.453, running off a server. I have tried the on several other computers, though, and the error persisted.
Many thanks for any help,
Genoveva
Genoveva Gonzalez Mirelis, Scientist
Institute of Marine Research
Nordnesgaten 50
5005 Bergen, Norway
Phone number +47 55238510
Genoveva Gonzalez Mirelis, Scientist
Institute of Marine Research
Nordnesgaten 50
5005 Bergen, Norway
Phone number +47 55238510
[[alternative HTML version deleted]]
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology