-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdemoSpline.r
More file actions
72 lines (63 loc) · 2.27 KB
/
demoSpline.r
File metadata and controls
72 lines (63 loc) · 2.27 KB
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
# title: Interactive Demo of Spline Fitting
# major: regression modeling
# minor: curve fitting
require(rms)
pag <- function(x,titl,df) {
if(!show.constructed.variables) return(invisible())
titl <- paste('Constructed Var. for ', titl, ' (',df,' d.f.)',sep='')
page(x, title=titl, multi=TRUE)
invisible()
}
nam <- function(w, dof) paste(w, ' (', dof, ' d.f.)', sep='')
worked <- function(f) ! (inherits(f, 'try-error') || any(is.na(coef(f))))
show.constructed.variables <- FALSE
while (TRUE) {
curves <- list()
plot(0:1,0:1,xlab='x', ylab='y', type='n')
cat('\nClick any points you desire.\nTerminate points by mouse right-click (Esc if under RStudio).\n')
z <- locator(type='p')
x <- z$x
y <- z$y
xs <- seq(min(x), max(x), length=200)
dx <- data.frame(x=xs)
k <- 0
while(k < 3) {
cat('\nClick >= 3 knot locations (y-coordinates ignored).\nTerminate points by mouse right-click (Esc if under RStudio).\n')
knots <- locator()$x
abline(v=knots, lty=2)
k <- length(knots)
dfs <- c(1, 1 + k, 4, 3 + k, k - 1)
}
f <- ols(y ~ x)
curves[[nam('Linear', 1)]] <- list(xs, f$coef[1] + xs * f$coef[2])
f <- try(ols(y ~ lsp(x, knots), x=TRUE))
if(worked(f)) {
pag(f$x,'Linear Spline',dfs[2])
curves[[nam('Linear Spline', 1 + k)]] <- list(xs, predict(f, dx))
}
f <- try(ols(y ~ pol(x,4), x=TRUE))
if(worked(f)) {
pag(f$x,'Quartic Polynomial',dfs[3])
curves[[nam('Quartic Polynomial', 4)]] <- list(xs, predict(f,dx))
}
X <- cbind(x, x^2, x^3)
for(i in 1 : k) X <- cbind(X, pmax(x - knots[i], 0) ^ 3)
f <- try(ols(y ~ X, tol=1e-8), silent=TRUE)
if(worked(f)) {
dimnames(X)[[2]] <- c('x','x^2','x^3',rep('',k))
pag(X,'Unrestricted Cubic Spline', dfs[4])
X <- cbind(xs, xs^2, xs^3)
for(i in 1 : k) X <- cbind(X, pmax(xs - knots[i], 0) ^ 3)
curves[[nam('Unrestricted Cubic Spline', 3 + k)]] <-
list(xs, cbind(1,X) %*% coef(f))
}
f <- try(ols(y ~ rcs(x, knots), x=TRUE))
pag(f$x,'Restricted Cubic Spline',dfs[5])
if(worked(f))
curves[[nam('Restricted Cubic Spline', k - 1)]] <- list(xs, predict(f, dx))
labcurve(curves, col=1 : length(curves), pl=TRUE, add=TRUE, keys='lines')
title(sub='Left click for another example. Click outside plot to stop',
adj=0)
z <- locator(1)
if(any(z$x < 0 | z$x > 1 | z$y < 0 | z$y > 1)) break
}