| 1 |
% This is also sym.linked into |
| 2 |
% Martin's WpDensity package /u/maechler/R/Pkgs/WpDensity/ |
| 3 |
\name{D1D2} |
| 4 |
\alias{D1D2} |
| 5 |
\title{Numerical Derivatives of (x,y) Data via Smoothing Splines} |
| 6 |
\description{ |
| 7 |
Compute numerical derivatives of \eqn{f()} given observations |
| 8 |
\code{(x,y)}, using cubic smoothing splines with GCV, see |
| 9 |
\code{\link[stats]{smooth.spline}}. In other words, estimate \eqn{f'()} |
| 10 |
and/or \eqn{f''()} for the model |
| 11 |
\deqn{Y_i = f(x_i) + E_i, \ \ i = 1,\dots n,} |
| 12 |
} |
| 13 |
\usage{ |
| 14 |
D1D2(x, y, xout = x, spar.offset = 0.1384, deriv = 1:2, spl.spar = NULL) |
| 15 |
} |
| 16 |
\arguments{ |
| 17 |
\item{x,y}{numeric vectors of same length, supposedly from a model |
| 18 |
\code{y ~ f(x)}.} |
| 19 |
\item{xout}{abscissa values at which to evaluate the derivatives.} |
| 20 |
\item{spar.offset}{numeric fudge added to the smoothing parameter, |
| 21 |
see \code{spl.par} below.} |
| 22 |
\item{deriv}{integer in \code{1:2} indicating which |
| 23 |
derivatives are to be computed.} |
| 24 |
\item{spl.spar}{direct smoothing parameter for \code{smooth.spline}. |
| 25 |
If it is \code{NULL} (as per default), the smoothing parameter used |
| 26 |
will be \code{spar.offset + sp$spar}, where \code{sp$spar} is the GCV |
| 27 |
estimated smoothing parameter, see \code{\link{smooth.spline}}.} |
| 28 |
} |
| 29 |
\details{ |
| 30 |
It is well known that for derivative estimation, the optimal smoothing |
| 31 |
parameter is larger (more smoothing) than for the function itself. |
| 32 |
\code{spar.offset} is really just a \emph{fudge} offset added to the |
| 33 |
smoothing parameter. Note that in \R's implementation of |
| 34 |
\code{\link{smooth.spline}}, \code{spar} is really on the |
| 35 |
\eqn{\log\lambda} scale. |
| 36 |
|
| 37 |
When \code{deriv = 1:2} (as per default), both derivatives are |
| 38 |
estimated with the \emph{same} smoothing parameter which is suboptimal |
| 39 |
for the single functions individually. Another possibility is to call |
| 40 |
\code{D1D2(*, deriv = k)} twice with \code{k = 1} and \code{k = 2} and |
| 41 |
use a \emph{larger} smoothing parameter for the second derivative. |
| 42 |
} |
| 43 |
\value{ |
| 44 |
a list with several components, |
| 45 |
\item{x}{the abscissae values at which the derivative(s) are evaluated.} |
| 46 |
\item{D1}{if \code{deriv} contains 1, estimated values of |
| 47 |
\eqn{f'(x_i)} where \eqn{x_i} are the values from \code{xout}.} |
| 48 |
\item{D2}{if \code{deriv} contains 2, estimated values of \eqn{f''(x_i)}.} |
| 49 |
\item{spar}{the \bold{s}moothing \bold{par}ameter used in the (final) |
| 50 |
\code{smooth.spline} call.} |
| 51 |
\item{df}{the equivalent \bold{d}egrees of \bold{f}reedom in that |
| 52 |
\code{smooth.spline} call.} |
| 53 |
} |
| 54 |
\author{Martin Maechler, in 1992 (for S).} |
| 55 |
\seealso{\code{\link{D2ss}} which calls \code{smooth.spline} twice, |
| 56 |
first on \code{y}, then on the \eqn{f'(x_i)} values; |
| 57 |
\code{\link[stats]{smooth.spline}} on which it relies completely. |
| 58 |
} |
| 59 |
\examples{ |
| 60 |
set.seed(8840) |
| 61 |
x <- runif(100, 0,10) |
| 62 |
y <- sin(x) + rnorm(100)/4 |
| 63 |
|
| 64 |
op <- par(mfrow = c(2,1)) |
| 65 |
plot(x,y) |
| 66 |
lines(ss <- smooth.spline(x,y), col = 4) |
| 67 |
str(ss[c("df", "spar")]) |
| 68 |
plot(cos, 0, 10, ylim = c(-1.5,1.5), lwd=2, |
| 69 |
main = expression("Estimating f'() : "~~ frac(d,dx) * sin(x) == cos(x))) |
| 70 |
offs <- c(-0.1, 0, 0.1, 0.2, 0.3) |
| 71 |
i <- 1 |
| 72 |
for(off in offs) { |
| 73 |
d12 <- D1D2(x,y, spar.offset = off) |
| 74 |
lines(d12$x, d12$D1, col = i <- i+1) |
| 75 |
} |
| 76 |
legend(2,1.6, c("true cos()",paste("sp.off. = ", format(offs))), lwd=1, |
| 77 |
col = 1:(1+length(offs)), cex = 0.8, bg = NA) |
| 78 |
par(op) |
| 79 |
} |
| 80 |
\keyword{smooth} |