Compute the Value of a Function Over a Local Region Of An XY Plot
Description
This function applies the specified function to the sets of y values
that are defined by overlapping "windows" in the xdimension. For
example, setting fun=mean
returns local means, while setting
fun=function(x) sqrt(var(x))
returns local estimates of
the standard deviation.
Usage
1 2 
Arguments
x 
vector of x values for (x,y) pairs 
y 
vector of y values for (x,y) pairs 
fun 
function to be applied 
method 
method of defining an xneighborhood. One of "width","nobs","range", or "fraction". See details. 
width 
width of an xneighborhood. See details. 
n 
Number of equally spaced points at which to compute local estimates. See details. 
drop.na 
should points which result in missing values 
pts 

... 
arguments to be passed to 
Details
Two basic techniques are available for determining what points fall
within the same xneighborhood. The first technique uses a window with
a fixed width in the xdimension and is is selected by
setting method="width"
or method="range"
. For
method="width"
the width
argument is an absolute
distance in the xdimension. For method="range"
, the width is
expressed as a fraction of the xrange. In both cases, pts
specifies the points at which evaluation of fun
occurs. When
pts
is omitted, n
x values equally spaced along the x
range are used.
The second technique uses windows containing k neighboring points. The
(x,y) pairs are sorted by the xvalues and the nearest k/2 points with
higher x values and the k/2 nearest points with lower x values are
included in the window. When method="nobs"
, k equals
width
(actually 2*floor(width
/2) ). When
method="fraction"
, width
specifies what fraction of the
total number of points should be included. The actual number of points
included in each window will be floor(n*frac/2)*2. Regardless of the
value of pts
, the function fun
will be evaluated at all
x locations.
Value
Returns a list with components
x 
x location' 
y 
Result of applying 
Author(s)
Gregory R. Warnes greg@warnes.net
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29  #show local mean and inner 2sd interval to help diagnose changing mean
#or variance structure
x < 1:1000
y < rnorm(1000, mean=1, sd=1 + x/1000 )
plot(x,y)
lines(wapply(x,y,mean),col="red")
CL < function(x,sd) mean(x)+sd*sqrt(var(x))
lines(wapply(x,y,CL,sd= 1),col="blue")
lines(wapply(x,y,CL,sd=1),col="blue")
lines(wapply(x,y,CL,sd= 2),col="green")
lines(wapply(x,y,CL,sd=2),col="green")
#show local mean and inner 2sd interval to help diagnose changing mean
#or variance structure
x < 1:1000
y < rnorm(1000, mean=x/1000, sd=1)
plot(x,y)
lines(wapply(x,y,mean),col="red")
CL < function(x,sd) mean(x)+sd*sqrt(var(x))
lines(wapply(x,y,CL,sd= 1,method="fraction",width=1/20),col="blue")
lines(wapply(x,y,CL,sd=1,method="fraction",width=1/20),col="blue")
lines(wapply(x,y,CL,sd= 2,method="nobs",width=250),col="green")
lines(wapply(x,y,CL,sd=2,method="nobs",width=250),col="green")
