The R Project SVN R

Rev

Rev 61160 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 61160 Rev 61168
Line 34... Line 34...
34
\dontshow{
34
\dontshow{
35
print.default(cars.spl)
35
print.default(cars.spl)
36
}
36
}
37
 
37
 
38
## "Proof" that the derivatives are okay, by comparing with approximation
38
## "Proof" that the derivatives are okay, by comparing with approximation
39
diff.quot <- function(x,y) {
39
diff.quot <- function(x, y) {
40
  ## Difference quotient (central differences where available)
40
  ## Difference quotient (central differences where available)
41
  n <- length(x); i1 <- 1:2; i2 <- (n-1):n
41
  n <- length(x); i1 <- 1:2; i2 <- (n-1):n
42
  c(diff(y[i1]) / diff(x[i1]), (y[-i1] - y[-i2]) / (x[-i1] - x[-i2]),
42
  c(diff(y[i1]) / diff(x[i1]), (y[-i1] - y[-i2]) / (x[-i1] - x[-i2]),
43
    diff(y[i2]) / diff(x[i2]))
43
    diff(y[i2]) / diff(x[i2]))
44
}
44
}
45
 
45
 
46
xx <- unique(sort(c(seq(0,30, by = .2), kn <- unique(speed))))
46
xx <- unique(sort(c(seq(0, 30, by = .2), kn <- unique(speed))))
47
i.kn <- match(kn, xx)# indices of knots within xx
47
i.kn <- match(kn, xx)   # indices of knots within xx
48
op <- par(mfrow = c(2,2))
48
op <- par(mfrow = c(2,2))
49
plot(speed, dist, xlim = range(xx), main = "Smooth.spline & derivatives")
49
plot(speed, dist, xlim = range(xx), main = "Smooth.spline & derivatives")
50
lines(pp <- predict(cars.spl, xx), col = "red")
50
lines(pp <- predict(cars.spl, xx), col = "red")
51
points(kn, pp$y[i.kn], pch = 3, col = "dark red")
51
points(kn, pp$y[i.kn], pch = 3, col = "dark red")
52
mtext("s(x)", col = "red")
52
mtext("s(x)", col = "red")
53
for(d in 1:3){
53
for(d in 1:3){
54
  n <- length(pp$x)
54
  n <- length(pp$x)
55
  plot(pp$x, diff.quot(pp$x,pp$y), type = 'l', xlab = "x", ylab = "",
55
  plot(pp$x, diff.quot(pp$x,pp$y), type = "l", xlab = "x", ylab = "",
56
       col = "blue", col.main = "red",
56
       col = "blue", col.main = "red",
57
       main= paste0("s" ,paste(rep("'",d), collapse = ""),"(x)"))
57
       main = paste0("s" ,paste(rep("'", d), collapse = ""), "(x)"))
58
  mtext("Difference quotient approx.(last)", col = "blue")
58
  mtext("Difference quotient approx.(last)", col = "blue")
59
  lines(pp <- predict(cars.spl, xx, deriv = d), col = "red")
59
  lines(pp <- predict(cars.spl, xx, deriv = d), col = "red")
60
\dontshow{
60
\dontshow{
61
  print(pp)
61
  print(pp)
62
}
62
}