# Just look at one day from the ozone2
data(ozone2)
out<- MLE.Matern( ozone2$lon.lat,ozone2$y[16,],1.5, ngrid=8)
plot( out$REML.grid)
points( out$pars[2], out$REML, cex=2)
xline( out$pars[2], col="blue", lwd=2)
# to get a finer grid on initial search:
out<- MLE.Matern( ozone2$lon.lat,ozone2$y[16,],1.5,
theta.grid=c(.3,2), ngrid=40)
# simulated data 200 points uniformly distributed
set.seed( 123)
x<- matrix( runif( 2*200), ncol=2)
n<- nrow(x)
rho= 2.0
sigma= .05
theta=.5
Cov.mat<- rho* Matern( rdist(x,x), smoothness=1.0, range=theta)
A<- chol( Cov.mat)
gtrue<- t(A) %*% rnorm(n)
gtrue<- c( gtrue)
err<- rnorm(n)*sigma
y<- gtrue + err
out0<- MLE.Matern( x,y,smoothness=1.0) # the bullet proof version
# the MLEs and -log likelihood at maximum
print( out0$pars)
print( out0$REML)
out<- MLE.Matern.fast( x,y, smoothness=1.0) # for the impatient
# the MLEs:
print( out$pars)
print( out$REML)
# MLE for fixed theta (actually the MLE from out0)
# that uses MLE.objective.fn directly
info<- list( x=x,y=y,smoothness=1.0, ngrid=80)
# the MLEs:
out2<- MLE.objective.fn(log(out0$pars[2]), info, value=FALSE)
print( out2$pars)
# Now back to Midwest ozone pollution ...
# Find the MLEs for ozone data and evaluate the Kriging surface.
data(ozone2)
out<- MLE.Matern.fast( ozone2$lon.lat,ozone2$y[16,],1.5)
#use these parameters to fit surface ....
lambda.MLE<- out$pars[3]/out$pars[1]
out2<- Krig( ozone2$lon.lat,ozone2$y[16,] , Covariance="Matern",
theta=out$pars[2], smoothness=1.5, lambda= lambda.MLE)
surface( out2) # uses default lambda -- which is the right one.
# here is another way to do this where the new lambda is given in
# the predict step
out2<- Krig( ozone2$lon.lat,ozone2$y[16,] , Covariance="Matern",
theta=out$pars[2], smoothness=1.5)
# The default lambda is that found by GCV
# predict on a grid but use the MLE value for lambda:
out.p<- predict.surface(out2, lambda= lambda.MLE)
surface(out.p) # same surface!
# One could also use mKrig with a fixed lambda to compute the surface.
# looping through all the days of the ozone data set.
data( ozone2)
x<- ozone2$lon.lat
y<- ozone2$y
out.pars<- matrix( NA, ncol=3, nrow=89)
for ( k in 1:89){
hold<- MLE.Matern.fast( x,c(y[k,]), 1.5)$pars
cat( "day", k," :", hold, fill=TRUE)
out.pars[k,]<- hold }
Run the code above in your browser using DataLab