> library(splines) > ?ns > x = runif(100,0,1) > y = 5*sin(2*pi*x) + rnorm(100) > plot(x,y) > ns(x,4) 1 2 3 4 [1,] 2.671861e-01 -0.138551081 0.49022434 -0.34804570 [2,] 9.011021e-02 0.555378122 0.24138900 0.11312267 [3,] 5.178343e-03 0.196740200 0.36472862 0.43335284 [4,] 4.043862e-02 -0.123004339 0.42411239 -0.30110805 [5,] 2.220834e-02 0.371721186 0.31468503 0.29138544 [6,] 0.000000e+00 -0.129652843 0.44703608 0.68261676 [7,] 5.537769e-01 0.333022253 0.12368923 -0.07981118 [8,] 4.606186e-01 0.445180658 0.12385809 -0.06135772 [9,] 3.271682e-03 0.152874223 0.37629311 0.46756098 [10,] 4.417745e-01 0.463269866 0.12584357 -0.05779864 [11,] 1.518089e-01 -0.148422992 0.51221532 -0.36365869 [12,] 3.189043e-02 -0.116277917 0.40092005 -0.28464213 [13,] 4.741783e-01 -0.073057396 0.36928163 -0.26217973 ### lines omitted [96,] 3.069546e-04 -0.027854273 0.09604005 -0.06818577 [97,] 6.550887e-02 -0.135784256 0.46817687 -0.33239261 [98,] 6.051078e-01 0.038421605 0.23805525 -0.16901263 [99,] 4.046039e-01 -0.102964369 0.41897989 -0.29746412 [100,] 3.158218e-03 -0.059382867 0.20474896 -0.14536609 attr(,"degree") [1] 3 attr(,"knots") 25% 50% 75% 0.1966724 0.4646389 0.7794378 attr(,"Boundary.knots") [1] 0.002084014 0.996848385 attr(,"intercept") [1] FALSE attr(,"class") [1] "ns" "basis" > bs(x,df=4) 1 2 3 4 [1,] 5.867781e-01 0.2989735352 4.367694e-02 0.000000e+00 [2,] 1.584162e-02 0.1718021744 5.752804e-01 2.370758e-01 [3,] 9.103670e-04 0.0311895583 3.473825e-01 6.205175e-01 [4,] 5.544673e-01 0.1047704134 6.181467e-03 0.000000e+00 [5,] 3.904288e-03 0.0765218635 4.787427e-01 4.408311e-01 [6,] 0.000000e+00 0.0000000000 0.000000e+00 1.000000e+00 [7,] 1.531179e-01 0.4652404237 3.749727e-01 6.668940e-03 [8,] 1.064774e-01 0.4182056357 4.531741e-01 2.214282e-02 [9,] 5.751708e-04 0.0233328785 3.088990e-01 6.671929e-01 [10,] 9.931178e-02 0.4082537307 4.661521e-01 2.628238e-02 [11,] 6.135264e-01 0.2192362849 2.331001e-02 0.000000e+00 [12,] 5.346601e-01 0.0910963792 4.874787e-03 0.000000e+00 [13,] 4.709852e-01 0.4179157117 9.908703e-02 0.000000e+00 [14,] 0.000000e+00 0.0000000000 0.000000e+00 0.000000e+00 ### lines omitted [97,] 5.884165e-01 0.1383396717 1.001372e-02 0.000000e+00 [98,] 3.373973e-01 0.4858477601 1.764745e-01 0.000000e+00 [99,] 5.184430e-01 0.3800074068 7.634485e-02 0.000000e+00 [100,] 3.207814e-01 0.0218266623 4.827668e-04 0.000000e+00 attr(,"degree") [1] 3 attr(,"knots") 50% 0.4646389 attr(,"Boundary.knots") [1] 0.002084014 0.996848385 attr(,"intercept") [1] FALSE attr(,"class") [1] "bs" "basis" ## Note that "bs" only has 1 knot, because we specified 4 df ## whereas with 4 df "ns" can have 3 knots because the function ## is linear at the endpoints > basis = ns(x,df=4) > newx = runif(5) > predict(basis,newx) 1 2 3 4 [1,] 0.02386236 0.3815322 0.3115888 0.28301659 [2,] 0.57319439 0.3022502 0.1269952 -0.08483746 [3,] 0.55184207 0.3358852 0.1234545 -0.07935517 [4,] 0.19476755 -0.1471081 0.5098843 -0.36200371 [5,] 0.28219103 0.5723714 0.1587831 -0.01786905 attr(,"degree") [1] 3 attr(,"knots") 25% 50% 75% 0.1966724 0.4646389 0.7794378 attr(,"Boundary.knots") [1] 0.002084014 0.996848385 attr(,"intercept") [1] FALSE attr(,"class") [1] "ns" "basis" ## regression splines > junk = lm(y~ns(x,df=4)) > plot(x,junk$fitted, pch="*") > points(x,y) > points(x,sin(2*pi*x),col="red") > points(x,5*sin(2*pi*x),col="red") > points(x,lm(y~ns(x,df=2))$fitted, col="blue") > points(x,lm(y~ns(x,df=3))$fitted, col="blue") > points(x,lm(y~ns(x,df=30))$fitted, col="blue") ## smoothing splines > junk2 = gam(y~s(x)) > junk2 Family: gaussian Link function: identity Formula: y ~ s(x) Estimated degrees of freedom: 5.577818 total = 6.577818 GCV score: 1.045801 > plot(x,junk2$fitted.value) > points(x,y,col="blue") # compare to regression splines points(x,junk$fitted.value,col="red")