R/krige.prevR.r
krige-ANY-prevR-method.Rd
These functions execute a spatial interpolation of a variable of the slot
rings
of an object of class prevR. The method krige()
implements the ordinary kriging technique. The method idw()
executes
an inverse distance weighting interpolation.
# S4 method for ANY,prevR
krige(
formula,
locations,
N = NULL,
R = Inf,
model = NULL,
nb.cells = 100,
cell.size = NULL,
fit = "auto",
keep.variance = FALSE,
show.variogram = FALSE,
...
)
# S4 method for ANY,prevR
idw(
formula,
locations,
N = NULL,
R = Inf,
nb.cells = 100,
cell.size = NULL,
idp = 2,
...
)
variable(s) to interpolate (see details).
object of class prevR.
integer or list of integers corresponding to the rings to use.
integer or list of integers corresponding to the rings to use.
a variogram model returned by the function gstat::vgm()
.
number of cells on the longest side of the studied area
(unused if cell.size
is defined).
size of each cell (in the unit of the projection).
"auto"
for using a variogram automatically fitted from the data,
only if model
is not defined (NULL
).
return variance of estimates?
plot the variogram?
additional arguments transmitted to gstat::krige()
or gstat::idw()
.
inverse distance weighting power (see gstat::idw()
).
Object of class sf::sf.
The name of estimated surfaces depends on the name of the interpolated
variable, N and R (for example: r.radius.N300.RInf).
If you ask the function to return variance (keep.variance=TRUE
),
corresponding surfaces names will have the suffix .var.
formula
specifies the variable(s) to interpolate. Only variables
available in the slot rings
of locations
could be used. Possible values
are "r.pos"
, "r.n"
, "r.prev"
, "r.radius"
, "r.clusters"
, "r.wpos"
,
"r.wn"
or "r.wprev"
. Variables could be specified with a character
string or a formula (example: list(r.pos ~ 1, r.prev ~ 1}
. Only formula
like variable.name ~ 1
are accepted. For more complex interpolations,
use directly functions gstat::krige()
and gstat::idw()
from gstat.
N
and R
determine the rings to use for the interpolation. If they are
not defined, surfaces will be estimated for each available couples (N,R).
Several interpolations could be simultaneously calculated if several
variables and/or several values of N and R are defined.
A suggested value of N could be computed with Noptim()
.
In the case of an ordinary kriging, the method krige()
from prevR
will try to fit automatically a exponential variogram to the sample variogram
(fit = "auto"
). You can also specify directly the variogram to use with
the parameter model
.
Interpolations are calculated on a spatial grid obtained with
make.grid.prevR()
.
Results could be plotted with sf::plot()
or with ggplot2
using ggplot2::geom_sf()
. See examples.
prevR provides several continuous color palettes (see prevR.colors).
Results could be turned into a stars raster using
stars::st_rasterize()
.
To export to ASCII grid, rasterize the results with stars::st_rasterize()
,
convert to SpatRast
with terra::rast()
, extract the desired layer with
[[]]
and then use terra::writeRaster()
. See examples.
Larmarange Joseph, Vallo Roselyne, Yaro Seydou, Msellati Philippe and Meda Nicolas (2011) "Methods for mapping regional trends of HIV prevalence from Demographic and Health Surveys (DHS)", Cybergeo: European Journal of Geography, no 558, https://journals.openedition.org/cybergeo/24606, DOI: 10.4000/cybergeo.24606.
if (FALSE) {
dhs <- rings(fdhs, N = c(100,200,300,400,500))
radius.N300 <- krige('r.radius', dhs, N = 300, nb.cells = 50)
prev.krige <- krige(r.wprev ~ 1, dhs, N = c(100, 300, 500))
plot(prev.krige, lty = 0)
library(ggplot2)
ggplot(prev.krige) +
aes(fill = r.wprev.N300.RInf) +
geom_sf(colour = "transparent") +
scale_fill_gradientn(colors = prevR.colors.red()) +
theme_prevR_light()
# Export r.wprev.N300.RInf surface in ASCII Grid
r <- terra::rast(stars::st_rasterize(prev.krige))
# writeRaster(r[[2]], "wprev.N300.asc")
}