######################################################
##### This is an extended example showing how to define
##### spatially varying sigma2 parameter
#######################################################
# Define some useful predict functions.
#######################################################
predict.surfaceGrid<- function(object,x){
interp.surface( object, x)
}
predict.multivariateSurfaceGrid<- function(object,x){
dimZ<- dim( object$z)
L<- dimZ[3]
out<- matrix( NA, nrow= nrow(x), ncol=L)
for ( l in 1:L){
out[,l]<- interp.surface(
list( x=object$x,y=object$y, z=object$z[,,l]) , x)
}
return( out)
}
predict.constantValue<- function(object,x){
n<- length(object$values)
m<- nrow( x)
return( matrix( object$values, nrow=m, ncol=n, byrow=TRUE ) )
}
################################################
##### Non-stationary examples
###############################################
# spatial domain
sDomain<- rbind( c(-1.2,-1.2),
c(1,1))
# we will use this coarse grid to define any
# surfaces of parameters
# (unrelated to the lattice grids and plotting grid!)
# this is larger than the sDomain to accommodate buffer points
# (with larger ranges when NC is small)
gridList<- list( x = seq( -3, 3,,50),
y = seq( -3, 3,,75) )
xPoints<- make.surface.grid( gridList)
fineGrid<- make.surface.grid(
list( x = seq(-1, 1, ,45),
y = seq(-1, 1, ,60)
)
)
##################################################
### end of setup
#################################################
# sigma2 increases across the domain as a function of first coordinate.
sigma2Temp<- .01 + 10* pnorm( xPoints[,1], mean=.25, sd =.3 )
sigma2.object<- as.surface( xPoints, sigma2Temp)
class( sigma2.object)<- "surfaceGrid"
LKinfo<- LKrigSetup( sDomain, NC= 4, nlevel = 3,
a.wght=4.5, nu=1, sigma2.object=sigma2.object)
# simulate a field from this model
set.seed(123)
look<- LKrig.sim( fineGrid, LKinfo)
image.plot( as.surface( fineGrid, look))
xline( .25, col="grey30")
# see also
# temp<- as.surface( fineGrid, look)
# I<- 20
# matplot(temp$x, temp$z, type="l", xlab="x", ylab="GP slice" )
######################################################
##### spatially varying alpha parameters
#######################################################
# the alpha surface at each level will just be the result of
# bi-linear interpolation of values specified on a small grid.
# To keep things identified the alpha weights at
# any grid location are
# normalized to sum to 1.
#
# create a 3 column matrix with (proportional) alpha weights
# at each grid point
#
taper<- pnorm( xPoints[,1], mean = .4, sd=.02)
alphaTemp<- cbind( taper,
rep( 1, length( taper)),
1-taper)
# normalize to sum to one
alphaTemp <- alphaTemp/rowSums(alphaTemp)
# pack as a list
# convert from a vector to the image/list format $x $y $z
# give this object a class so that predict.surfaceGrid is called.
# accumulate these objects in a list
# (yes this is a "list of lists")
alphaObject<- list()
for( k in 1:3){
hold<- as.surface( xPoints, alphaTemp[,k])
class( hold)<- "surfaceGrid"
alphaObject<- c( alphaObject, list( hold))
}
# define the 2-d LatticeKrig model
LKinfo<- LKrigSetup(sDomain, NC = 4, a.wght=4.5,
alpha = c(1,1,1), nlevel = 3,
alphaObject = alphaObject )
# simulate a field
set.seed(123)
look<- LKrig.sim( fineGrid, LKinfo)
image.plot( as.surface( fineGrid, look))
######################################################
##### spatially varying a.wght parameters
##### See above comments and setup
##### for steps that are the same
#######################################################
taper<- pnorm( xPoints[,1] + xPoints[,1],
mean = 0, sd=.15)
a.wghtTemp<- 4.001*taper + 10*(1-taper)
# pack up as a list
# convert from a vector to the image/list format $x $y $z
# give this object a class so that predict.surfaceGrid is called.
# accumulate these objects in a list (yes this is
# a "list of lists")
a.wghtObjectA <- as.surface( xPoints, a.wghtTemp)
class( a.wghtObjectA)<- "surfaceGrid"
# define the 2-d LatticeKrig model
LKinfo2<- LKrigSetup(sDomain, NC = 5, NC.buffer=0,
alpha = c(1, .5, .125), nlevel = 3,
a.wghtObject = a.wghtObjectA)
set.seed(123)
look<- LKrig.sim( fineGrid, LKinfo2)
image.plot( as.surface( fineGrid, look))
##############################################
###### 1-d example
#############################################
xCoarse1<- seq( -.5,1.5,, 40)
y<- pnorm( xCoarse1, mean=.4, sd=.05)*5 + 2.2
a.wghtObject<- Tps(xCoarse1, y, lambda=0)
alphaTemp<-c(.5, .3, .2)
LKinfoTEST<- LKrigSetup( rbind(0,1), NC=10,
LKGeometry="LKInterval",
nlevel=3, alpha=alphaTemp,
a.wghtObject = a.wghtObject,
NC.buffer=2
)
xFine1<- cbind(seq( 0,1,length.out= 200))
set.seed( 123)
look<- LKrig.sim( xFine1, LKinfoTEST, M=5)
matplot( xFine1, look, type="l", lty=1)
##################################################
######## Anisotropy in a.wght
##################################################
#### stationary example
a.wghtM2<- c( rbind( c( 0, 0, -1.5),
c(-.5, 4.5, -.25),
c(-1.5, 0, 0)
)
)
LKinfo3<- LKrigSetup(sDomain, NC = 5,
a.wght= list( a.wghtM2),
alpha = c(1, .5, .125), nlevel = 3,
a.wghtObject = NULL, normalize=TRUE )
set.seed(123)
look<- LKrig.sim( fineGrid, LKinfo3)
image.plot( as.surface( fineGrid, look))
if (FALSE) {
#### Anisotropy varying over space
#### First check that the constant model can be reproduced
a.wghtM2<- c( rbind( c( 0, 0, -1.5),
c(-.5, 4.5, -.5),
c(-1.5, 0, 0)
)
)
a.wghtObject<- list( values=a.wghtM2)
class(a.wghtObject )<- "constantValue"
LKinfo4<- LKrigSetup(sDomain, NC = 5,
alpha = c(1,.5, .125), nlevel = 3,
a.wghtObject = a.wghtObject, normalize=TRUE )
set.seed(123)
look<- LKrig.sim( fineGrid, LKinfo4)
image.plot( as.surface( fineGrid, look) )
###### non-stationary anisotropy
a.wghtA <- c( rbind( c( 0, 0, -2),
c( 0, 4.5, 0),
c(-2, 0, 0)
)
)
a.wghtB <- c( rbind( c( -2, 0, 0),
c( 0, 4.5, 0),
c( 0, 0, -2)
)
)
# Now create multivariate prediction object.
gridList<- attributes( xPoints)$grid.list
m1<- length(gridList$x)
m2<- length(gridList$y)
z<- array( NA, c( m1,m2,9))
alpha<- (xPoints[,1] + 1 )/2
alpha<- ifelse( alpha <= 0, 0, alpha)
alpha<- ifelse( alpha >= 1, 1, alpha)
# A for loop over the 9 pixels
for(j in 1:9) {
# linear combination of two a.wght matrices
#
zTemp<- a.wghtA[j] * (1-alpha) + a.wghtB[j]*(alpha)
# coerce into an image format
z[,,j]<- as.surface( xPoints, zTemp)$z
}
a.wghtObject<- list( x= gridList$x, y= gridList$y, z=z )
class( a.wghtObject)<- "multivariateSurfaceGrid"
LKinfo5<- LKrigSetup(sDomain, NC = 25, NC.buffer=0,
alpha = c(1,.5),
nlevel = 2,
a.wghtObject = a.wghtObject )
set.seed(122)
fineGrid<- make.surface.grid(
list( x = seq(-1, 1, ,150),
y = seq(-1, 1, ,180)
)
)
look<- LKrig.sim( fineGrid, LKinfo5)
image.plot( as.surface( fineGrid, look), col = terrain.colors(256) )
}
Run the code above in your browser using DataLab