# Get the data
data(bone_data)
x = bone_data$age
y = bone_data$spnbmd
m <- length(x)
# Set asymmetry levels
p = c(0.005, 0.01, 0.02, 0.05, 0.2, 0.5, 0.8, 0.9, 0.95, 0.98, 0.99, 0.995)
np <- length(p)
# Set P-spline parameters
x0 <- 5
x1 <- 30
ndx <- 20
bdeg <- 3
pord <- 2
# Compute bases
B <- bbase(x, x0, x1, ndx, bdeg)
xg <- seq(from = min(x), to = max(x), length = 100)
Bg <- clone_base(B, xg)
n <- ncol(B)
lambda = 1
alpha <- rep(1,n)
a = p
for (it in 1:20){
alpha <- fitampl(y, B, alpha, p, a, pord, lambda)
alpha <- alpha / sqrt(mean(alpha ^ 2))
anew <- fitasy(y, B, alpha, p, a)
da = max(abs(a - anew))
a = anew
cat(it, da, '\n')
if (da < 1e-6) break
}
# Compute bundle on grid
ampl <- Bg %*% alpha
Z <- ampl %*% a
# Plot data and bundle
plot(x, y, pch = 15, cex = 0.7, col = 'grey', xlab = 'Age', ylab = 'Density')
cols = colorspace::rainbow_hcl(np, start = 10, end = 350)
matlines(xg, Z, lty = 1, lwd = 2, col = cols)
Run the code above in your browser using DataLab