D1D2                 package:sfsmisc                 R Documentation

_N_u_m_e_r_i_c_a_l _D_e_r_i_v_a_t_i_v_e_s _o_f (_x,_y) _D_a_t_a _v_i_a _S_m_o_o_t_h_i_n_g _S_p_l_i_n_e_s

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

     Compute numerical derivatives of f() given observations '(x,y)',
     using cubic smoothing splines with GCV, see 'smooth.spline'.  In
     other words, estimate f'() and/or f''() for the model

                  Y_i = f(x_i) + E_i,   i = 1,... n,

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

     D1D2(x, y, xout = x, spar.offset = 0.1384, deriv = 1:2, spl.spar = NULL)

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

     x,y: numeric vectors of same length, supposedly from a model 'y ~
          f(x)'.

    xout: abscissa values at which to evaluate the derivatives.

spar.offset: numeric fudge added to the smoothing parameter, see
          'spl.par' below.

   deriv: integer in '1:2' indicating which derivatives are to be
          computed.

spl.spar: direct smoothing parameter for 'smooth.spline'. If it is
          'NULL' (as per default), the smoothing parameter used will be
          'spar.offset + sp$spar', where 'sp$spar' is the GCV estimated
          smoothing parameter, see 'smooth.spline'.

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

     It is well known that for derivative estimation, the optimal
     smoothing parameter is larger (more smoothing) than for the
     function itself. 'spar.offset' is really just a _fudge_ offset
     added to the smoothing parameter. Note that in R's implementation
     of 'smooth.spline', 'spar' is really on the loglambda scale.

     When 'deriv = 1:2' (as per default), both derivatives are
     estimated with the _same_ smoothing parameter which is suboptimal
     for the single functions individually.  Another possibility is to
     call 'D1D2(*, deriv = k)' twice with 'k = 1' and 'k = 2' and use a
     _larger_ smoothing parameter for the second derivative.

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

     a list with several components, 

       x: the abscissae values at which the derivative(s) are
          evaluated.

      D1: if 'deriv' contains 1, estimated values of f'(x_i) where x_i
          are the values from 'xout'.

      D2: if 'deriv' contains 2, estimated values of f''(x_i).

    spar: the *s*moothing *par*ameter used in the (final)
          'smooth.spline' call.

      df: the equivalent *d*egrees of *f*reedom in that 'smooth.spline'
          call.

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

     Martin Maechler, in 1992 (for S).

_S_e_e _A_l_s_o:

     'D2ss' which calls 'smooth.spline' twice, first on 'y', then on
     the f'(x_i) values; 'smooth.spline' on which it relies completely.

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

      set.seed(8840)
      x <- runif(100, 0,10)
      y <- sin(x) + rnorm(100)/4

      op <- par(mfrow = c(2,1))
      plot(x,y)
      lines(ss <- smooth.spline(x,y), col = 4)
      str(ss[c("df", "spar")])
      if(is.R()) plot(cos, 0, 10, ylim = c(-1.5,1.5), lwd=2) else { # Splus
        xx <- seq(0,10, len=201); plot(xx, cos(xx), type = 'l', ylim = c(-1.5,1.5))
      }
      title(expression("Estimating f'() :  " * frac(d,dx) * sin(x) == cos(x)))
      offs <- c(-0.1, 0, 0.1, 0.2, 0.3)
      i <- 1
      for(off in offs) {
        d12 <- D1D2(x,y, spar.offset = off)
        lines(d12$x, d12$D1, col = i <- i+1)
      }
      legend(2,1.6, c("true cos()",paste("sp.off. = ", format(offs))), lwd=1,
             col = 1:(1+length(offs)), cex = 0.8, bg = NA)
      par(op)

