Debian Codegraph @

R/D1D2.R

raw
1 ## This is also sym.linked into
2 ## Martin's WpDensity package /u/maechler/R/Pkgs/WpDensity/
3
4 ###------- Numerical Derivatives ------------------------------------------
5
6 ### Test Programs and examples for those two are in
7 ### --> "/u/maechler/S/NUMERICS/D1-tst.S"
8 ###
9 ### For 'optimal' 2nd Deriv.: d2.est(..)
10 ### --> "/u/maechler/S/NUMERICS/diff2.S" "/u/maechler/S/NUMERICS/diff2-user.S"
11
12
13 D1tr <- function(y, x = 1)
14 {
15 ## Purpose: discrete trivial estimate of 1st derivative.
16 ## -------------------------------------------------------------------------
17 ## Arguments: x is optional
18 ## -------------------------------------------------------------------------
19 ##--> See also D1.naive in ~/S/D1-tst.S (and the (smoothing) one: 'D1') !
20 ## Author: Martin Maechler, ~ 1990
21 n <- length(y)
22 if(length(x) == 1)
23 c(y[2] - y[1], 0.5 * (y[-(1:2)] - y[-((n-1):n)]), y[n] - y[n-1])/x
24 else {
25 if(n != length(x)) stop("lengths of 'x' & 'y' must equal")
26 if(is.unsorted(x)) stop("'x' must be sorted !")
27 c(y[2] - y[1], 0.5 * (y[-(1:2)] - y[-((n-1):n)]), y[n] - y[n-1]) /
28 c(x[2] - x[1], 0.5 * (x[-(1:2)] - x[-((n-1):n)]), x[n] - x[n-1])
29 }
30 }
31
32
33 D1ss <- function(x, y, xout = x, spar.offset = 0.1384, spl.spar=NULL)
34 {
35 ## Purpose: Numerical first derivatives of f() for y_i = f(x_i) + e_i.
36 ## Find f'(xout) -- using smoothing splines with GCV'
37 ## Author: Martin Maechler, Date: 6 Sep 92, 00:04
38 ## -------------------------------------------------------------------------
39 ## Arguments: x = { x_i } MUST be sorted increasingly // y = { y_i }
40 ## -------------------------------------------------------------------------
41 sp <-
42 if(is.null(spl.spar)) {
43 sp <- smooth.spline(x,y)
44 smooth.spline(x,y, spar = sp$ spar + spar.offset)
45 } else smooth.spline(x,y, spar = spl.spar)
46 predict(sp, xout, deriv = 1) $ y
47 }
48
49 D2ss <- function(x, y, xout = x, spar.offset = 0.1384, spl.spar=NULL)
50 {
51 ## Purpose: Numerical 2nd derivative of f() for y_i = f(x_i) + e_i.
52 ## Find f''(xout) -- using smoothing splines (with GCV) -- DOUBLY:
53 ## f --ss-> f' --ss-> f''
54 ## -------------------------------------------------------------------------
55 ## Arguments: x = { x_i } MUST be sorted increasingly // y = { y_i }
56 ## -------------------------------------------------------------------------
57 ## Author: Martin Maechler, Date: 29 Jan 1997, 17:55 -- for S-plus
58 ## -------------------------------------------------------------------------
59
60 use.fudge <- is.null(spl.spar)
61 if(use.fudge) { ##-- use GCV * 'spar.offset' ---
62 if(is.null(spar.offset)) stop("must specify 'spl.spar' OR 'spar.offset'!")
63 lf <- length(spar.offset)
64 if(!is.numeric(spar.offset) || lf == 0 || lf > 2)
65 stop("'spar.offset' must be numeric(1 or 2) !")
66 if(lf == 1) spar.offset <- rep(spar.offset, 2)
67 sp <- smooth.spline(x,y)
68 sp <- smooth.spline(x,y, spar = spar.offset[1] + sp $ spar)
69 spl.spar <- numeric(2); spl.spar[1] <- sp $ spar
70 }
71 else {
72 lf <- length(spl.spar)
73 if(!is.numeric(spl.spar) || lf == 0 || lf > 2)
74 stop("'spl.spar' must be numeric(1 or 2) !")
75 if(lf == 1) spl.spar <- rep(spl.spar, 2)
76 sp <- smooth.spline(x,y, spar = spl.spar[1])
77 }
78
79 D1 <- predict(sp, x, deriv = 1) $ y #-- 1st derivative ...
80
81 if(use.fudge) { ##-- use GCV * 'spar.offset' ---
82 sp <- smooth.spline(x, D1)
83 sp <- smooth.spline(x, D1, spar = spar.offset[2] + sp $ spar)
84 spl.spar[2] <- sp $ spar
85 } else {
86 sp <- smooth.spline(x, D1, spar = spl.spar[2])
87 }
88 if(is.unsorted(xout))
89 xout <- sort(xout)
90 list(x=xout, y = predict(sp, xout, deriv = 1) $ y,
91 spl.spar = spl.spar, spar.offset = spar.offset)
92 }
93
94
95
96 D1D2 <- function(x, y, xout = x, spar.offset = 0.1384,
97 deriv = 1:2, spl.spar=NULL)
98 {
99 ## Purpose: Numerical first derivatives of f() for y_i = f(x_i) + e_i.
100 ## Find f'(xout) & f''(xout) -- using smoothing splines with GCV'
101 ## Author: Martin Maechler, Date: 23 Sep 1992, 9:40ith GCV'
102 ## Author: Martin Maechler, Date: 23 Sep 1992, 9:40
103 ## -------------------------------------------------------------------------
104 ## Arguments: x = { x_i } MUST be sorted increasingly // y = { y_i }
105 ## -------------------------------------------------------------------------
106
107 if(is.unsorted(xout))
108 xout <- sort(xout)
109 sp <-
110 if(is.null(spl.spar)) {
111 sp <- smooth.spline(x,y)
112 smooth.spline(x,y, spar = sp$ spar + spar.offset)
113 } else smooth.spline(x,y, spar = spl.spar)
114 c(list(x = xout,
115 D1 = if(any(deriv==1)) predict(sp, xout, deriv = 1) $ y,
116 D2 = if(any(deriv==2)) predict(sp, xout, deriv = 2) $ y),
117 sp[c("spar", "df")])
118 }