\Header{predict.smooth.spline}{Predict from Smoothing Spline Fit}
\keyword{smooth}{predict.smooth.spline}
\begin{Description}\relax
Predict a smoothing spline fit at new points, return the derivative if
desired. The predicted fit is linear beyond the original data.\end{Description}
\begin{Usage}
\begin{verbatim}
predict.smooth.spline(object, x, deriv = 0)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{object}] a fit from \code{smooth.spline}.
\item[\code{x}] the new values of x.
\item[\code{deriv}] integer; the order of the derivative required.
\end{ldescription}
\end{Arguments}
\begin{Value}
A list with components
\begin{ldescription}
\item[\code{x}] The input \code{x}.
\item[\code{y}] The fitted values or derivatives at \code{x}.
\end{ldescription}
\end{Value}
\begin{Author}\relax
B.D. Ripley\end{Author}
\begin{SeeAlso}\relax
\code{\Link{smooth.spline}}\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
data(cars)
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)
\end{ExampleCode}
\end{Examples}

