wapply                package:gplots                R Documentation

_C_o_m_p_u_t_e _t_h_e _V_a_l_u_e _o_f _a _F_u_n_c_t_i_o_n _O_v_e_r _a _L_o_c_a_l _R_e_g_i_o_n _O_f _A_n _X-_Y _P_l_o_t

_D_e_s_c_r_i_p_t_i_o_n:

     This function applies the specified function to the sets of y
     values that are defined by overlapping "windows" in the
     x-dimension. For example, setting 'fun=mean' returns local means,
     while setting 'fun=function(x) sqrt(var(x))' returns local
     estimates of the standard deviation.

_U_s_a_g_e:

     wapply(x, y, fun=mean, method="range", width, n=50, drop.na=TRUE,
            pts, ...)

_A_r_g_u_m_e_n_t_s:

       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 x-neighborhood.  One of
          "width","nobs","range", or "fraction".  See details.

   width: width of an x-neighborhood. 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 'NA' be omitted
          from the return value. Defaults to true. 

     pts: 'x' locations at which to compute the local mean when using
          the "width" or "range" methods. Ignored otherwise.

     ...: arguments to be passed to 'fun' 

_D_e_t_a_i_l_s:

     Two basic techniques are available for determining what points
     fall within the same x-neighborhood.  The first technique uses a
     window with a fixed width in the x-dimension and is is selected by
     setting 'method="width"' or 'method="range"'.  For
     'method="width"' the 'width' argument is an absolute distance in
     the x-dimension.  For 'method="range"', the width is expressed as
     a fraction of the x-range.  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 x-values 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.

_V_a_l_u_e:

     Returns a list with components  

      x : x location'

      y : Result of applying 'fun' to the window about each x location

_A_u_t_h_o_r(_s):

     Gregory R. Warnes gregory.r.warnes@pfizer.com

_E_x_a_m_p_l_e_s:

     #show local mean and inner 2-sd 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 2-sd 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")

