# first convert precip data to the 128X128 discretized image format ( with
# missing values to indicate where data is not observed)
#
out<- as.image( precip$y, x= precip$x, nrow=128, ncol=128)
# out$z is the image matrix
dx<- out$x[2]- out$x[1]
dy<- out$y[2] - out$y[1]
#
look<- image.smooth( out$z, dx=dx, dy=dy, theta= .25)
# grid scale in degree and so kernel bandwidth is .25 degrees.
image.plot( x= out$x, y=out$y, z= look)
points( precip$x)
# to save on computation, decrease the padding with zeroes
look<- image.smooth( out$z, dx=dx, dy=dy, theta= .25, Mwidth=32,Nwidth=32)
# the range of these data is ~ 10 and so 32*( 10/128) = 2.5
# about 10 standard deviations of the normal kernel so there is still
# lots of room for padding
# a minimal choice might be Mwidth = 4* (.25/dx) 4 SD for the normal
#
# creating weighting object outside the call
# this is useful when one wants to smooth different data sets but on the
# same grid with the same kernel function
#
wght<- image.smooth.setup( nrow=128, ncol=128, dx=dx, dy=dy, theta= .25,
Mwidth=32, Nwidth=32)
#
# random fields from smoothing white noise with this filter.
#
look<- image.smooth( matrix( rnorm(128**2), 128,128), wght)
# take a look: image.plot( look)
Run the code above in your browser using DataLab