Restricted (Natural) cubic spline - wrong equation?

  • Level: Graduate 
  • Thread starter shockedMonkey
  • Start date
  • Tags Tags
    Cubic Natural
1 reply · 2K views · last reply
shockedMonkey
Messages
2
Reaction score
0
My Restricted (Natural) Cubic Spline Equation is Wrong ?

I am trying to fit a restricted cubic spline (natural cubic spline) to toy data, attempting to follow
Hastie, Tibshirani, Friedman 2nd ed. 5.2.1 p.144-146, Eqs 5.4 and 5.5.
Data: Is basically a transposed ‘S’ shape. R-code is:
n <© 100
x <- (1:n)/n
true <- ((exp(1.2*x)+1.5*sin(7*x))-1)/3
noise <- rnorm(n, 0, 0.15)
y <- true + noise
plot(x,y)

I set knots at: {.2, .4, .6, .8} and am fitting using the non-linear NLS() function in R, but I can’t get the S-shape of the data no matter what I try.

My equations is wrong ? Or I am completely off-base in my approach? Any suggestions?
(Book-excerpt, my equation, and data-plot posted below)
 
Attachments
  • Textbook excerpt on natural cubic splines with K knots, showing basis functions N1(X) = 1, N2(X) = X, and Nk+2(X) = dk(X) - dK-1(X) as Equation 5.4.
    book1.JPG
    16 KB · Views: 527
  • Page 146 of a textbook showing equation 5.5: d_k(X) equals the difference of truncated cubes (X minus xi_k) cubed and (X minus xi_K) cubed, divided by xi_K minus xi_k, with the note that each basis function has zero second and third derivative for X at or above xi_K.
    book2.JPG
    8.2 KB · Views: 549
  • Equation for a restricted cubic spline: y = β0 + β1x + β2 times a difference of truncated cubic terms over (k4 − k1), minus a similar term over (k4 − k3), plus β3 times a similar pair over (k4 − k2) and (k4 − k3).
    splineEq.JPG
    8.7 KB · Views: 557
  • Scatter plot of y against x for 100 noisy points on x from 0 to 1, forming a rising, wavy curve: y dips to about -0.3 near x=0.75 and rises to about 1.2 at x=1.
    data.JPG
    17.3 KB · Views: 509
Q: Is my problem unclear? Or - is anyone aware of a better forum for my question? Thank you.