# Load ozone data set
data(ozone2)
x<-ozone2$lon.lat
y<- ozone2$y[16,]
# Find location that are not 'NA'.
# (LKrig is not set up to handle missing observations.)
good <- !is.na( y)
x<- x[good,]
y<- y[good]
LKinfo<- LKrigSetup( x,NC=20,nlevel=1, alpha=1, lambda= .3 , a.wght=5)
# BTW lambda is close to MLE
# What does the LatticeKrig covariance function look like?
# set up LKinfo object
# NC=10 sets the grid for the first level of basis functions
# NC^2 = 100 grid points in first level if square domain.
# given four levels the number of basis functions
# = 10^2 + 19^2 +37^2 + 73^2 = 5329
# effective range scales as roughly kappa where a.wght = 4 + kappa^2
# or exponential decreasing marginal variances for the components.
NC<- 10
nlevel <- 4
a.wght <- 4 + 1/(.5)^2
alpha<- 1/2^(0:(nlevel-1))
LKinfo2<- LKrigSetup( cbind( c( -1,1), c(-1,1)), NC=NC,
nlevel=nlevel, a.wght=a.wght,alpha=alpha)
# evaluate covariance along the horizontal line through
# midpoint of region -- (0,0) in this case.
look<- LKrig.cov.plot( LKinfo2)
# a plot of the covariance function in x and y with respect to (0,0)
set.panel(2,1)
plot(look$u[,1], look$cov[,1], type="l")
title("X transect")
plot(look$u[,2], look$cov[,2], type="l")
title("Y transect")
set.panel(1,1)
#
#
if (FALSE) {
# full 2-d view of the covariance (this example follows the code
# in LKrig.cov.plot)
x2<- cbind( 0,0)
x1<- make.surface.grid( list(x=seq( -1,1,,40), y=seq( -1,1,,40)))
look<- LKrig.cov( x1,x2, LKinfo2)
contour( as.surface( x1, look))
# Note nearly circular contours.
# of course plot(look[,80/2]) should look like plot above.
#
}
if (FALSE) {
#Some correlation functions from different models
set.panel(2,1)
# a selection of ranges:
hold<- matrix( NA, nrow=150, ncol=4)
kappa<- seq( .25,1,,4)
x2<- cbind( 0,0)
x1<- cbind( seq(-1,1,,150), rep( 0,150))
for( k in 1:4){
LKtemp<- LKrigSetup( cbind( c( -1,1), c(-1,1)), NC=NC,
nlevel=nlevel,
a.wght= 4 + 1/(kappa[k]^2),
alpha=alpha)
hold[,k]<- LKrig.cov( x1,x2, LKinfo=LKtemp)
}
matplot( x1[,1], hold, type="l", lty=1, col=rainbow(5), pch=16 )
# a selection of smoothness parameters
ktemp<- .5 # fix range
alpha.power<- seq( 1,4,,4)
LKtemp<- LKinfo2
for( k in 1:4){
LKtemp<- LKrigSetup( cbind( c( -1,1), c(-1,1)), NC=NC,
nlevel=nlevel,
a.wght= 4 + 1/(ktemp^2),
alpha=alpha^alpha.power[k])
hold[,k]<- LKrig.cov( x1,x2, LKinfo=LKtemp)
}
matplot( x1[,1], hold, type="l", lty=1, col=rainbow(5) )
set.panel()
}
if (FALSE) {
# generating a basis on the domain [-1,1] by [-1,1] with 1 level
# Default number of buffer points are added to each side.
LKinfo<- LKrigSetup(cbind( c(-1,1), c(-1,1)), NC=6,
nlevel=1, a.wght=4.5,alpha=1, NC.buffer=0 )
# evaluate the basis functions on a grid to look at them
xg<- make.surface.grid( list(x=seq(-1,1,,50), y= seq(-1,1,,50)))
PHI<- LKrig.basis( xg,LKinfo)
dim(PHI) # should be 2500=50^2 by 36=6^2
# plot the 9th basis function as.surface is a handy function to
# reformat the vector as an image object
# using the grid information in an attribute of the grid points
set.panel(1,3)
image.plot(as.surface(xg, PHI[,9]))
points( make.surface.grid( LKrigLatticeCenters(LKinfo, 1)) , col="grey", cex=.5)
title("A radial basis function")
# compare to the tensor product basis type
LKinfo2<- LKrigSetup(cbind( c(-1,1), c(-1,1)), NC=6,
nlevel=1, a.wght=4.5,alpha=1, NC.buffer=0,
BasisType="Tensor" )
PHI2<- LKrig.basis( xg,LKinfo2)
image.plot(as.surface(xg, PHI2[,9]))
points( make.surface.grid( LKrigLatticeCenters(LKinfo, 1)), col="grey", cex=.5)
title("Tensor product basis function")
image.plot(as.surface(xg, PHI[,9] - PHI2[,9]))
points( make.surface.grid( LKrigLatticeCenters(LKinfo, 1)), col="grey", cex=.5)
title(" Radial - Tensor for 9th basis function")
set.panel()
}
#
# example of basis function indexing
#
if (FALSE) {
# generating a basis on the domain [-1,1]X[-1,1] with 3 levels
# note that there are no buffering grid points.
set.panel(3,2)
LKinfo<-LKrigSetup(cbind( c(-1,1), c(-1,1)), NC=6,
a.wght=5, alpha=c(1,.5,.25), nlevel=3,
NC.buffer=0)
# evaluate the basis functions on a grid to look at them
xtemp<- seq(-1,1,,40)
xg<- make.surface.grid( list(x=xtemp, y= xtemp) )
PHI<- LKrig.basis( xg,LKinfo)
# coerce to dense matrix format to make plotting easier.
PHI<- spam2full(PHI)
# first tenth, and last basis function in each resolution level
# basis functions centers are added
set.panel(3,3)
for( j in 1:3){
id1<- LKinfo$latticeInfo$offset[j]+ 1
id2<- LKinfo$latticeInfo$offset[j]+ 10
idlast<- LKinfo$latticeInfo$offset[j] +
LKinfo$latticeInfo$mx[j,1]*LKinfo$latticeInfo$mx[j,2]
centers<- make.surface.grid(LKrigLatticeCenters(LKinfo, j) )
image.plot( as.surface(xg, PHI[,id1]))
points( centers, cex=.2, col="grey")
image.plot(as.surface(xg, PHI[,id2]))
points( centers, cex=.2, col="grey")
image.plot( as.surface(xg, PHI[,idlast]))
points( centers, cex=.2, col="grey")
}
set.panel()
}
if (FALSE) {
# examining the stationarity of covariance model
temp.fun<-
function( NC.buffer=0, NC=4, a.wght=4.01){
LKinfo<- LKrigSetup(cbind( c(-1,1), c(-1,1)),nlevel=1, alpha=1,
a.wght=a.wght, NC=NC,
NC.buffer=NC.buffer,
choleskyMemory=list(nnzR=2e6))
cov1y<- cov1x<- cov0x<- cov0y<- matrix( NA, nrow=200, ncol=20)
cov1dx<- cov1dy<- cov0dx<- cov0dy<- matrix( NA, nrow=200, ncol=20)
cgrid<- seq( 0,1,,20)
for( k in 1:20){
hold<- LKrig.cov.plot( LKinfo,
center=rbind( c(cgrid[k], cgrid[k])), NP=200)
cov1x[,k] <- hold$cov[,1]
cov1y[,k] <- hold$cov[,2]
cov1dx[,k] <- hold$d[,1]
cov1dy[,k] <- hold$d[,2]
hold<- LKrig.cov.plot( LKinfo,
center=rbind( c(cgrid[k],0) ), NP=200)
cov0x[,k] <- hold$cov[,1]
cov0y[,k] <- hold$cov[,2]
cov0dx[,k] <- hold$d[,1]
cov0dy[,k] <- hold$d[,2]
}
matplot( cov1dx, cov1x, type="l", col= rainbow(20),
xlab="", ylab="correlation")
mtext( side=1, line=-1, text="diagonal X")
title( paste( " buffer=",NC.buffer), cex=.5)
matplot( cov1dy, cov1y, type="l", col= rainbow(20),
xlab="", ylab="")
mtext( side=1, line=-1, text="diagonal Y")
matplot(cov0dx, cov0x, type="l", col= rainbow(20),
xlab="", ylab="")
mtext( side=1, line=-1, text="middle X")
matplot( cov0dy, cov0y, type="l", col= rainbow(20),
xlab="", ylab="")
mtext( side=1, line=-1, text="middle Y")
title( paste( NC, a.wght), cex=.5)
}
set.panel(3,4)
par(mar=c(3,4,1,0), oma=c(1,1,1,1))
temp.fun( NC.buffer=5, NC=4, a.wght=4.05)
temp.fun( NC.buffer=5, NC=16, a.wght=4.05)
temp.fun( NC.buffer=5, NC=64, a.wght=4.05)
set.panel(4,4)
par(mar=c(3,4,1,0), oma=c(1,1,1,1))
temp.fun( NC.buffer=0, NC=8)
temp.fun( NC.buffer=2, NC=8)
temp.fun( NC.buffer=4, NC=8)
# this next one takes a while
temp.fun( NC.buffer=8, NC=8)
# stationary == curves should all line up!
}
Run the code above in your browser using DataLab