# \donttest{
# load data:
library(gstat)
data(meuse)
coordinates(meuse) = ~x+y
data(meuse.grid)
coordinates(meuse.grid) = ~x+y
gridded(meuse.grid) = TRUE
predGrid = meuse.grid
# estimate variograms (OK/UK):
vfitOK = fit.variogram(variogram(zinc~1, meuse), vgm(1, "Exp", 300, 1))
vfitUK = fit.variogram(variogram(zinc~x+y, meuse), vgm(1, "Exp", 300, 1))
vfitRK = fit.variogram(variogram(zinc~dist+ffreq+soil, meuse), vgm(1, "Exp", 300, 1))
# study area of interest:
bb = bbox(predGrid)
boun = SpatialPoints(data.frame(x=c(bb[1,1],bb[1,2],bb[1,2],bb[1,1],bb[1,1]),
y=c(bb[2,1],bb[2,1],bb[2,2],bb[2,2],bb[2,1])))
Srl = Polygons(list(Polygon(boun)),ID = as.character(1))
candidates = SpatialPolygonsDataFrame(SpatialPolygons(list(Srl)),
data = data.frame(ID=1))
# add 20 more points assuming OK model (SSA method):
optimOK <- optimizeNetwork(meuse, meuse.grid, candidates = candidates,
method= "ssa", action= "add", nDiff = 20, model = vfitOK, criterion="MUKV",
nr_iterations=10000, nmax=40)
# add 20 more points assuming UK model (SSA method):
optimUK <- optimizeNetwork(meuse, meuse.grid, candidates = candidates,
method = "ssa", action = "add", nDiff = 20, model=vfitUK, criterion="MUKV",
nr_iterations = 10000, nmax = 40, formulaString = zinc~x+y)
# add 20 more points with auxiliary variables (SSA method):
optimRK <- optimizeNetwork(meuse, meuse.grid, candidates=candidates,
method="ssa", action="add", nDiff=4, model=vfitRK, criterion="MUKV",
nr_iterations=10000, formula=zinc~dist+ffreq+soil, nmax=200)
# add optimally 20 stations from current network with method "spcov"
# (spatial coverage method)
optimSC = optimizeNetwork(meuse, meuse.grid, candidates, method = "spcov",
action = "add", nDiff = 10, model = model, criterion = "MUKV", plotOptim = TRUE,
nGridCells = 10000,nTry = 100 )
# delete optimally 10 stations from current network with method "manual"
if (interactive()) optimMAN = optimizeNetwork(meuse, meuse.grid, candidates, method = "manual",
action = "del", nDiff = 10, model = model, criterion = "MUKV", plotOptim = TRUE )
# comparison of results with ordinary kriging variogram, otherwise add formulaString
# ssa method, assuming ordinary kriging
calculateMukv(optimOK, predGrid, vfitOK)
# ssa method, using spatial location as covariates
calculateMukv(optimUK, predGrid, vfitUK, zinc~x+y)
# ssa method, using other variables as covariates
calculateMukv(optimRK, predGrid, vfitRK, zinc~dist+ffreq+soil)
# spcov method
calculateMukv(optimSC, predGrid, vfitOK)
# 10 stations manually deleted
if (interactive()) calculateMukv(optimMAN, predGrid, vfitOK, zinc~1)
# }
Run the code above in your browser using DataLab