In this preamble, we load the gstlearn library, clean the workspace.
rm(list=ls())
library(gstlearn)
library(ggplot2)
library(ggpubr)
Then we download the data base dat.
dlfile = "https://soft.minesparis.psl.eu/gstlearn/data/Scotland/Scotland_Temperatures.NF"
fileNF = "Scotland_Temperatures.NF"
download.file(dlfile, fileNF)
dat = Db_createFromNF(fileNF)
Calculate the experimental variogram vario2dir (in 2 directions)
varioParamMulti = VarioParam_createMultiple(ndir=2, npas=15, dpas=15.)
vario2dir = Vario(varioParamMulti, dat)
err = vario2dir$compute()
Calculate the fitted model fitmodOK (add the Universality Condition)
fitmodOK = Model()
types = ECov_fromKeys(c("NUGGET","EXPONENTIAL","GAUSSIAN"))
err = fitmodOK$fit(vario2dir,types=types)
err = fitmodOK$addDrift(Drift1())
ggplot() + plot.varmod(vario2dir, fitmodOK)
Define the Unique Neighborhood unique.neigh:
unique.neigh = NeighUnique()
Get the extension of the Data:
dat$getExtremas()
## [[1]]
## [1] 78.2 460.7
##
## [[2]]
## [1] 530.4 1208.9
Create the Target file grid:
grid = DbGrid_create(x0=c(65,535),dx=c(4.94, 4.96),nx=c(81,137))
dbfmt = DbStringFormat_createFromFlags(flag_resume=FALSE, flag_vars=TRUE,
flag_extend=TRUE, flag_stats=FALSE,
flag_array=FALSE)
grid$display(dbfmt)
##
## Data Base Grid Characteristics
## ==============================
##
## Data Base Extension
## -------------------
## Coor #1 - Min = 65.000 - Max = 460.200 - Ext = 395.2
## Coor #2 - Min = 535.000 - Max = 1209.560 - Ext = 674.56
##
## Variables
## ---------
## Column = 0 - Name = rank - Locator = NA
## Column = 1 - Name = x1 - Locator = x1
## Column = 2 - Name = x2 - Locator = x2
## NULL
err = kriging(dbin=dat, dbout=grid, model=fitmodOK, neigh=unique.neigh,
flag_est=TRUE, flag_std=TRUE, flag_varz=FALSE,
namconv=NamingConvention("OK"))
p = ggDefaultGeographic()
p = p + plot.grid(grid, legend.name.raster="°C", zlim=c(0,8))
## Warning in geom_tile(data = df, mapping = aes(x = x, y = y, fill = data), :
## Ignoring unknown parameters: `zlim`
p = p + plot.decoration(title="Ordinary Kriging over whole Grid")
ggPrint(p)
dlfile = "https://soft.minesparis.psl.eu/gstlearn/data/Scotland/Scotland_Elevations.NF"
fileNF = "Scotland_Elevations.NF"
download.file(dlfile, fileNF)
grid = DbGrid_createFromNF(fileNF)
grid$display(dbfmt)
##
## Data Base Grid Characteristics
## ==============================
##
## Data Base Extension
## -------------------
## Coor #1 - Min = 65.000 - Max = 455.123 - Ext = 390.123
## Coor #2 - Min = 535.000 - Max = 1200.109 - Ext = 665.109
##
## Variables
## ---------
## Column = 0 - Name = Longitude - Locator = x1
## Column = 1 - Name = Latitude - Locator = x2
## Column = 2 - Name = Elevation - Locator = f1
## Column = 3 - Name = inshore - Locator = sel
## NULL
The output grid now contains the selection inshore. Estimation is restricted to the active cells only.
err = kriging(dbin=dat, dbout=grid, model=fitmodOK, neigh=unique.neigh,
flag_est=TRUE, flag_std=TRUE, flag_varz=FALSE,
namconv=NamingConvention("OK"))
p = ggDefaultGeographic()
p = p + plot.grid(grid, "OK*estim", legend.name.raster="°C", zlim=c(0.,8.))
## Warning in geom_tile(data = df, mapping = aes(x = x, y = y, fill = data), :
## Ignoring unknown parameters: `zlim`
p = p + plot.point(dat,name_size="January_temp",sizmax=3,color='black')
p = p + plot.decoration(title="Estimation by Ordinary Kriging")
ggPrint(p)
p = ggDefaultGeographic()
p = p + plot.grid(grid,"OK*stdev", legend.name.raster="°C", zlim=c(0.,1.))
## Warning in geom_tile(data = df, mapping = aes(x = x, y = y, fill = data), :
## Ignoring unknown parameters: `zlim`
p = p + plot.point(dat,name_size="January_temp",sizmax=3,color='black')
p = p + plot.decoration(title="St. dev. by Ordinary Kriging")
ggPrint(p)
The Model fitmodOK is first duplicated into fitmodSK. Then the Universality Condition is deleted.
fitmodSK = fitmodOK$clone()
err = fitmodSK$delDrift(rank=0)
err = fitmodSK$setMean(mean=20.)
Simple Kriging is performed
err = kriging(dbin=dat, dbout=grid, model=fitmodSK, neigh=unique.neigh,
flag_est=TRUE, flag_std=TRUE,
namconv=NamingConvention("SK"))
p = ggDefaultGeographic()
p = p + plot.grid(grid,"SK*estim",legend.name.raster="°C", zlim=c(0.,8.))
## Warning in geom_tile(data = df, mapping = aes(x = x, y = y, fill = data), :
## Ignoring unknown parameters: `zlim`
p = p + plot.point(dat,name_size="January_temp",sizmax=3,color='black')
p = p + plot.decoration(title="Estimation by Simple Kriging")
ggPrint(p)