predict.smooth.spline         package:stats         R Documentation

_P_r_e_d_i_c_t _f_r_o_m _S_m_o_o_t_h_i_n_g _S_p_l_i_n_e _F_i_t

_D_e_s_c_r_i_p_t_i_o_n:

     Predict a smoothing spline fit at new points, return the
     derivative if desired. The predicted fit is linear beyond the
     original data.

_U_s_a_g_e:

     ## S3 method for class 'smooth.spline':
     predict(object, x, deriv = 0, ...)

_A_r_g_u_m_e_n_t_s:

  object: a fit from 'smooth.spline'.

       x: the new values of x.

   deriv: integer; the order of the derivative required.

     ...: further arguments passed to or from other methods.

_V_a_l_u_e:

     A list with components 

       x: The input 'x'.

       y: The fitted values or derivatives at 'x'.

_S_e_e _A_l_s_o:

     'smooth.spline'

_E_x_a_m_p_l_e_s:

     require(graphics)

     attach(cars)
     cars.spl <- smooth.spline(speed, dist, df=6.4)


     ## "Proof" that the derivatives are okay, by comparing with approximation
     diff.quot <- function(x,y) {
       ## Difference quotient (central differences where available)
       n <- length(x); i1 <- 1:2; i2 <- (n-1):n
       c(diff(y[i1]) / diff(x[i1]), (y[-i1] - y[-i2]) / (x[-i1] - x[-i2]),
         diff(y[i2]) / diff(x[i2]))
     }

     xx <- unique(sort(c(seq(0,30, by = .2), kn <- unique(speed))))
     i.kn <- match(kn, xx)# indices of knots within xx
     op <- par(mfrow = c(2,2))
     plot(speed, dist, xlim = range(xx), main = "Smooth.spline & derivatives")
     lines(pp <- predict(cars.spl, xx), col = "red")
     points(kn, pp$y[i.kn], pch = 3, col="dark red")
     mtext("s(x)", col = "red")
     for(d in 1:3){
       n <- length(pp$x)
       plot(pp$x, diff.quot(pp$x,pp$y), type = 'l', xlab="x", ylab="",
            col = "blue", col.main = "red",
            main= paste("s",paste(rep("'",d), collapse=""),"(x)", sep=""))
       mtext("Difference quotient approx.(last)", col = "blue")
       lines(pp <- predict(cars.spl, xx, deriv = d), col = "red")

       points(kn, pp$y[i.kn], pch = 3, col="dark red")
       abline(h=0, lty = 3, col = "gray")
     }
     detach(); par(op)

