Description Usage Arguments Value Author(s) References See Also Examples
Update a fit using any of several different functions that produce an object of class 'dierckx'.
1 2 |
object |
an object of class 'dierckx' |
knots |
either a numeric vector or an object from which knots(knots, interior=FALSE) will produce the desired numeric vector. |
k |
a positive integer giving the degree of the spline = one more than
the order of the polynomial segments. Valid options for |
s |
a nonnegative number or NULL. |
... |
Currently ignored. Additional arguments used only in |
An object as produced by the function named in 'object[["routine"]]'.
Sundar Dorai-Raj and Spencer Graves
Dierckx, P. (1993) Curve and Surface Fitting with Splines, Oxford Science Publications.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | x <- 0:24
y <- c(1.0,1.0,1.4,1.1,1.0,1.0,4.0,9.0,13.0,
13.4,12.8,13.1,13.0,14.0,13.0,13.5,
10.0,2.0,3.0,2.5,2.5,2.5,3.0,4.0,3.5)
#fitLS0 <- curfit(x, y)
#fitSS0 <- curfit(x, y, s=0)
ks <- c(3, 5, 2)
kk <- length(ks)
z <- vector("list", kk)
names(z) <- ks
for(i in 1:kk) {
k <- ks[i]
z1 <- curfit(x, y, s = 1000, k = k)
z2 <- update(z1, s = 60)
z3 <- update(z2, s = 10)
z4 <- update(z3, s = 30)
z5 <- curfit(x, y, s = 30, k = k)
z6 <- update(z5, s = 0)
knots <- c(rep(0, k + 1), seq(3, 21, 3), rep(24, k + 1))
z7 <- curfit(x, y, s = 30, knots = knots, k = k)
z[[i]] <- list(z1, z2, z3, z4, z5, z6, z7)
}
p <- unlist(z, recursive = FALSE)
n <- sapply(lapply(p, knots), length)
s <- sapply(p, "[[", "s")
i <- sapply(p, "[[", "iopt")
m <- ifelse(i == -1, "ls", ifelse(i == 0, "ss", "ss1"))
k <- sprintf("k = %d", sapply(p, "[[", "k"))
g <- sprintf("%s(s=%d)", m, s, i)
sp <- data.frame(x = rep(x, times = length(p)),
y = rep(y, times = length(p)), z = unlist(lapply(p, fitted)),
k = factor(rep(k, each = length(x))), g = rep(g, each = length(x)))
library(lattice)
xyplot(z ~ x | k, data = sp, groups = g,
panel = function(x, y, subscripts, groups, obs, ...) {
panel.superpose(x, y, subscripts, groups, lwd = 3, type = "l", ...)
x <- unique(x)
y <- unique(obs)
panel.xyplot(x, obs, pch = 16, cex = 1.2, col = "darkblue")
},
auto.key = list(space = "right", points = FALSE, lines = TRUE),
obs = sp[["y"]])
## periodic spline
set.seed(42)
n <- 100
r <- 1:n
x <- 0.01 * (r - 1)
e <- rnorm(n, 0, 0.1)
w <- rep(1/sd(e), n + 1)
y <- cos(2 * pi * x) + 0.25 * sin(8 * pi * x) + e
x <- c(x, 1)
y <- c(y, y[1])
kn <- seq(0.01, 0.99, length = 12)
f1 <- percur(x, y, w = w, s = 90, k = 5)
library(lattice)
top <- xyplot(y ~ x,
panel = function(x, y, ...) {
panel.abline(v = knots(f1), lty = 2, lwd = 3, col = "gray")
panel.xyplot(x, y, pch = 16, col = "#800000", cex = 1.2)
panel.xyplot(x, fitted(f1), type = "l", lwd = 3, col = "#000080")
},
par.settings = list(layout.widths = list(left.padding = 0, right.padding = 0)),
scales = list(cex = 1.2),
xlab = "", ylab = "")
newx <- seq(-2, 2, 0.01)
newy <- predict(f1, newx)
bot <- xyplot(newy ~ newx, type = "l",
panel = function(...) {
panel.abline(v = -2:2, lty = 2, col = "salmon", lwd = 3)
panel.xyplot(...)
},
col = "#000080", lwd = 3,
par.settings = list(layout.widths = list(left.padding = 0, right.padding = 0)),
scales = list(cex = 1.2),
xlab = "", ylab = "")
print(top, c(0, 0.2, 1, 1))
print(bot, c(0.008, 0, 0.992, 0.25), newpage = FALSE)
## example borrowed from ?smooth.spline
plot(cars[["speed"]], cars[["dist"]],
main = "data(cars) & smoothing splines",
xlab = "SPEED", ylab = "DISTANCE",
cex.lab = 1.2, cex.axis = 1.2,
cex.main = 2, cex = 1.5, col = "blue")
## This example has duplicate points, so avoid cv=TRUE
cars.spl.0 <- smooth.spline(cars[["speed"]], cars[["dist"]])
cars.spl.1 <- smooth.spline(cars[["speed"]], cars[["dist"]], df = 10)
cars.spl.2 <- curfit(cars[["speed"]], cars[["dist"]], s = 5e3)
newx <- seq(min(cars[["speed"]]), max(cars[["speed"]]), len = 200)
lines(predict(cars.spl.0, newx), col = "blue", lwd = 3, lty = 2)
lines(predict(cars.spl.1, newx), lty="dashed", col = "red", lwd = 3)
lines(newx, predict(cars.spl.2, newx), lty="dotted", lwd = 3)
legend(5, 120, c(paste("smooth.spline( * , df = ",
round(cars.spl.0[["df"]], 1), ")", sep = ""),
"smooth.spline( * , df = 10)", "curfit( * , s = 5e3)"),
col = c("blue", "red", "black"),
lty = c("solid", "dashed", "dotted"), lwd = 3,
bg = 'bisque', cex = 1.5)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.