#2-d example
# fitting a surface to ozone
# measurements. Range parameter is 10 (in miles)
fit <- Krig(ozone$x, ozone$y, exp.cov, theta=10)
summary( fit) # summary of fit
plot(fit) # diagnostic plots of fit
surface( fit, type="C") # look at the surface
out.p<- predict.surface.se( fit)
image(out.p)
# predict at data
predict( fit)
# predict on a grid ( grid chosen here by defaults)
out<- predict.surface( fit)
persp( out)
# predict at arbitrary points (10,-10) and (20, 15)
xnew<- rbind( c( 10, -10), c( 20, 15))
predict( fit, xnew)
# standard errors of prediction based on covariance model.
predict.se( fit, xnew)
#
# Roll your own: using a user defined Gaussian covariance
#
test.cov <- function(x1,x2,theta){exp(-(rdist(x1,x2)/theta)**2)}
# use this and put in quadratic polynomial fixed function
fit.flame<- Krig(flame$x, flame$y, test.cov, m=3, theta=.5)
#
# note how range parameter is passed to Krig.
# BTW: GCV indicates an interpolating model (nugget variance is zero)
#
# take a look ...
surface(fit.flame, type="I")
#
# Thin plate spline fit to ozone data using generalized variance
# function
# p=2 is the power in the radial basis function
# If m is the degree of derivative in penalty then p=2m-d
# where d is the dimension of x. p must be greater than 0.
# In the example below p = 2*2 - 2 = 2
# See also the Fields function Tps
out<- Krig( ozone$x, ozone$y, rad.cov, m=2, p=2)
# correlation model example
# fit krig surface using a mean and sd function to standardize
# first get stats from 1987 summer Midwest O3 data set
# Compare the function Tps to the call to Krig given above
# fit tps surfaces to the mean and sd points.
# (a shortcut is being taken here just using the lon/lat coordinates)
data(ozone2)
stats.o3<- stats( ozone2$y)
mean.o3<- Tps( ozone2$lon.lat, c( stats.o3[2,]))
sd.o3<- Tps( ozone2$lon.lat, c( stats.o3[3,]))
# Now use these to fit particular day ( day 16)
y16<- ozone2$y[16,] # there are some missing values!
good<- !is.na( y16)
fit<- Krig( ozone2$lon.lat[good,], y16[good],exp.earth.cov, theta=353,
mean.obj=mean.o3, sd.obj=sd.o3)
Run the code above in your browser using DataLab