I’m attempting to model car speed data using B-spline basis functions and compute the associated roughness penalty matrix. I’m using the fda
package in R to create B-spline basis functions and the getbasispenalty
function to compute the roughness penalty matrix.
The problem arises when I’m computing the roughness penalty matrix K1
. Upon careful examination, I suspect that the issue may be due to some misconfiguration of the B-spline basis functions. While I’ve ensured that the range of the data and the B-spline basis functions are consistent, I’m still encountering this problem.
Code snippet
# 0. Data
library(fields)
library(fda)
library(car)
library(numDeriv)
x1 <- cars$speed # Speeds of cars
y1 <- cars$dist # Stopping distances of cars
range(x1); range(y1)
N1 <- length(y1)
###
# p1
rg <- c(3,26)
m1 <- 4 # Order of splines
bp <- c(sort(x1)) # Break points/knots
bf <- fda::create.bspline.basis(
rangeval = rg, norder = m1, breaks = bp)
# class(bf);names(bf);bf$names
nb <- bf$nbasis # Number of basis functions
plot(bf, lwd = 2, col = 1:nb,
main = "B-spline basis functions")
points(x1, rep(0, N1), pch = 20, cex = 1.5, col = "red")
# p2
nb <- bf$nbasis # nb = m1 + length(bp) - 2
plot(bf, lwd = 2, col = 1:nb,
main = "B-spline basis functions")
# p3
F1 <- fda::eval.basis(x1,bf) # dim(F1)
fields::image.plot((t(F1[, nb:1])),
main = "B-spline basis data matrix")
# p4
L1 <- int2Lfd(2) # The 2nd-order differential operator d^2 / dx^2
# p5
fields::image.plot((t(K1[,nb:1])),
main = "Roughness penalty matrix")
K1 <- getbasispenalty(bf, L1) # dim(K1) = nb x nb
getbasispenalty(bf, L1)
issues
Error in knotmult > rng[1] && knotmult < rng[2] :
‘length = 13’ in coercion to ‘logical(1)’
If anyone has any insights, ideas, or suggestions regarding this issue, I would greatly appreciate your help.
Saidin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.