1 |
x |
|
k |
|
degree |
|
smooth |
|
intercept |
|
debug |
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 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (x, k, degree = rep(3, length(k) + 1), smooth = rep(2,
length(k)), intercept = FALSE, debug = FALSE)
{
if (!debug)
disp = function(x) invisible(0)
kernel = function(L) {
QR = qr(t(L))
qr.Q(QR, complete = T)[, -seq_len(QR$rank)]
}
Xfull = function(x, k, degree) {
if (length(degree) != length(k) + 1)
stop("length( degree ) must = length( k ) + 1")
Xmat = function(x, degree) {
do.call("cbind", lapply(0:degree, function(i) if (i ==
0)
rep(1, length(x))
else x^i))
}
k = sort(k)
g = cut(x, c(-Inf, k, Inf))
Xraw = Xmat(x, max(degree))
do.call("cbind", lapply(seq_along(degree), function(iint) (g ==
levels(g)[iint]) * Xraw[, 1:(degree[iint] + 1), drop = F]))
}
X0 = Xfull(0, k, degree)
if (FALSE) {
nk = length(k)
extend = (max(k) - min(k) + 1)/nk
ints = c(min(k) - extend, sort(k), max(k) + extend)
xs = approx(ints, n = (max(degree) + 1) * (nk + 1))$y
xs = c(-xs, xs)
X.full = Xfull(xs, k, degree)
disp(X.full)
}
fs = list(f = function(x) {
c(1, x, x^2, x^3, x^4)
}, f1 = function(x) {
c(0, 1, 2 * x, 3 * x^2, 4 * x^3)
}, f2 = function(x) {
c(0, 0, 2, 6 * x, 12 * x^2)
}, f3 = function(x) {
c(0, 0, 0, 6, 24 * x)
}, f4 = function(x) {
c(0, 0, 0, 0, 24)
})
nints = length(k) + 1
endcols = cumsum(degree + 1)
disp(endcols)
startcols = c(1, endcols[-length(endcols)] + 1)
disp(startcols)
ncols = degree + 1
disp(ncols)
Lsmooth = matrix(0, ncol = ncol(X0), nrow = sum(smooth +
1))
irow = 0
for (i in seq_along(k)) {
disp(i)
knot = k[i]
iplus = startcols[i]:endcols[i]
disp(iplus)
iminus = startcols[i + 1]:endcols[i + 1]
disp(iminus)
for (j in 0:smooth[i]) {
irow = irow + 1
Lsmooth[irow, iplus] = (fs[[j + 1]](knot)[1:ncols[i]])
Lsmooth[irow, iminus] = (-fs[[j + 1]](knot)[1:ncols[i +
1]])
}
}
disp(Lsmooth)
disp(Lsmooth %*% kernel(Lsmooth))
if (intercept == FALSE)
Lsmooth = rbind(Lsmooth, X0)
H = kernel(Lsmooth)
Xret = Xfull(x, k, degree) %*% H
Xret
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.