Skip to content

raster::predict example doesn't work?

2 messages · Bede-Fazekas Ákos, Gonzalez-Mirelis, Genoveva

#
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:
#
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:
_______________________________________________
R-sig-ecology mailing list
R-sig-ecology at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology