Learn R Programming

kzs (version 1.4)

kzs.2d: Spatial Kolmogorov-Zurbenko Spline

Description

The kzs.2d function is a spatial extension of the kzs function for two input variables.

Usage

kzs.2d(y, x, smooth, scale, k = 1, edges = TRUE, plot = TRUE)

Arguments

y
a one-dimensional vector of real values representing the response variable to be smoothed.
x
a two-dimensional matrix of real values containing the input variables X = (X1, X2). Each column represents an input variable.
smooth
a vector of size two that defines the width of the smoothing window along each input variable.
scale
a vector of size two in which each element will define a uniformly spaced scale along its respective input variable.
k
an integer specifying the number of iterations kzs.2d will execute. By default, k = 1.
edges
a logical indicating whether or not to display the outcome data beyond the rectangular range of the two input variables. By default, edges = TRUE.
plot
a logical indicating whether or not to produce a 3-dimensional plot of the kzs.2d outcome. By default, this argument is set to TRUE.

Value

  • a three column data frame of the form (x1, x2, yk):
  • x1the x1 coordinates of a two-dimensional grid.
  • x2the x2 coordinates of a two-dimensional grid.
  • ykthe smoothed response values resulting from k iterations of kzs.2d.

Details

The details for this function are nearly identical to that of kzs, except now extended to three dimensional space. The only difference is that the kzs.2d function averages all y that are contained within a rectangular window made up of sides smooth[1] and smooth[2].

See Also

kzs; For more on the parameter restrictions, see kzs.params

Examples

Run this code
# EXAMPLE - Estimating the Sinc function in the interval (-3pi, 3pi)
#           Load the LATTICE package 


# Gridded data for X = (x1, x2) input variables
x1 <- seq(-3*pi, 3*pi, length = 60)
x2 <- x1			
df <- expand.grid(x1 = x1, x2 = x2)
  
# Apply the Sinc function to the (x1, x2) coordinates
df$z <- sin(sqrt(df$x1^2 + df$x2^2)) / sqrt(df$x1^2 + df$x2^2)
df$z[is.na(df$z)] <- 1

# Any point outside the circle of radius 3pi is set to 0. This provides
# a better picture of the outcome solely for the purposes of this example.
dst <- sqrt((df$x1 - 0)^2 + (df$x2 - 0)^2)
df$dist <- dst	
df$z[df$dist > 3*pi] <- 0

# Add noise to distort the signal
ez <- rnorm(length(df$z), mean = 0, sd = 1) * 1/4    
df$zn <- ez + df$z

### (1) 3D plot of the signal to be estimated by kzs.2d()
wireframe(z ~ x1 * x2, df, main = "Signal to be estimated", drape = TRUE, 
colorkey = TRUE, scales = list(arrows = FALSE))

### (2) 3D plot of the signal buried in noise
wireframe(zn ~ x1 * x2, df, main = "Signal buried in noise", drape = TRUE, 
colorkey = TRUE, scales = list(arrows = FALSE))
  
### (3) Execute kzs.2d()   
# kzs.2d() may take time to run; k = 1 iteration is used here, but k = 2
# will provide a smoother outcome.
sw <- c(1, 1)
sc <- c(0.2, 0.2)
kzs.2d(y = df[,5], x = df[,1:2], smooth = sw, scale = sc, k = 1, edges = TRUE, 
plot = TRUE)

Run the code above in your browser using DataLab