RFoptions(seed=0) ## *ANY* simulation will have the random seed 0; set
## RFoptions(seed=NA) to make them all random again
RFoptions(storing=TRUE)
str(RFoptions())
############################################################
## ##
## use of exactness ##
## ##
############################################################
x <- seq(0, 1, if (interactive()) 1/30 else 0.5)
model <- RMgauss()
for (exactness in c(NA, FALSE, TRUE)) {
readline(paste("exactness: `", exactness, "'; press return"))
z <- RFsimulate(model, x, x, grid=TRUE, exactness=exactness,
stationary_only=NA, storing=TRUE)
print(RFgetModelInfo(which="internal")$internal$name)
}
\dontrun{
#############################################################
## The following gives a tiny example on the advantage of ##
## local.dependent=TRUE (and dependent=TRUE) if in a ##
## study most of the time is spent with simulating the ##
## Gaussian random fields. Here, the covariance at a pair ##
## of points is estimated for n independentent repetitions ##
## and 2*n locally dependent dependent repetitions . ##
## To get the precision, the procedure is repeated m times.##
#############################################################
# In the example below, local.dependent speeds up the simulation
# by about factor 16 at the price of an increased variance of
# factor 1.5
x <- seq(0, 1, len=10)
y <- seq(0, 1, len=10)
grid.size <- c(length(x), length(y))
meth <- RPcirculant
model <- RMexp(var=1.1, Aniso=matrix(nc=2, c(2,0.1,1.5,1)))
#(RMmodel(matrix(c(1, -1), ncol=2), model=model))
m <- if (interactive()) 100 else 2
n <- if (interactive()) 100 else 10
# using local.dependent=FALSE (which is the default)
c1 <- numeric(m)
time <- unix.time(
for (i in 1:m) {
cat("", i)
z <- RFsimulate(meth(model), x, y, grid=TRUE, n=n, pch="",
dependent=FALSE, spConform=FALSE, trials=5)
c1[i] <- cov(z[1,length(y), ], z[length(x), 1, ])
}) # many times slower than with local.dependent=TRUE below
Print(time, mean(c1), sd(c1))
# using local.dependent=TRUE...
c2 <- numeric(m)
time <- unix.time(
for (i in 1:m) {
cat("", i)
z <- RFsimulate(meth(model), x, y, grid=TRUE, n=2 * n, pch="",
dependent=TRUE, spConform=FALSE, trials=5)
c2[i] <- cov(z[1,length(y),], z[length(x), 1 , ])
})
Print(time, mean(c2), sd(c2))
# the sd is samller (using more locally dependent realisations)
## but it is (much) faster! Note that for n=n2 instead of n=2 * n,
## the value of sd(c2) would be larger due to the local dependencies
## in the realisations.
}
\dontrun{
############################################################
## ##
## use of points and center ##
## ##
############################################################
## The following example shows that the same realisation
## can be obtained on different grid geometries (or point
## configurations, i.e. grid, non-grid) using TBM3 (or TBM2)
x1 <- seq(-150,150,1)
y1 <- seq(-15, 15, 1)
x2 <- seq(-50, 50, 1)
model <- RPtbm(RMexp(scale=10))
RFoptions(storing=TRUE)
mar <- c(2.2, 2.2, 0.1, 0.1)
points <- 700
###### simulation of a random field on long thin stripe
z1 <- RFsimulate(model, x1, y1, grid=TRUE, center=0, seed=0,
points=points, storing=TRUE, spConform=FALSE)
do.call(getOption("device"), list(height=1.55, width=12))
par(mar=mar)
image(x1, y1, z1, col=rainbow(100))
polygon(range(x2)[c(1,2,2,1)], range(y1)[c(1,1,2,2)],
border="red", lwd=3)
###### definition of a random field on a square of shorter diagonal
z2 <- RFsimulate(model, x2, x2, grid=TRUE, register=1, seed=0,
center=0, points=points, spConform=FALSE)
do.call(getOption("device"), list(height=4.3, width=4.3))
par(mar=mar)
image(x2, x2, z2, zlim=range(z1), col=rainbow(100))
polygon(range(x2)[c(1,2,2,1)], range(y1)[c(1,1,2,2)],
border="red", lwd=3)
tbm.points <- RFgetModelInfo(RFsimulate)kkey$loc$totpts
Print(tbm.points, empty.lines=0) # number of points on the line
}
FinalizeExample()
Run the code above in your browser using DataLab