Mike Nielsen
2013-Feb-27 14:39 UTC
[R] Finding the knots in a smoothing spline using nknots
Hi r-helpers. Please forgive my ignorance, but I would like to plot a smoothing spline (smooth.spline) from package "stats", and show the knots in the plot, and I can't seem to figure out where smooth.spline has located the knots (when I use nknots). Unfortunately, I don't know a lot about splines, but I know that they provide me an easy way to estimate the location of local maxima and minima on varying time-scales (number of knots) in my original data. I see there is a fit$knot, but it's not clear to me what those values are: for some reason I had expected that they would be contained in my original y values, but they're not. I tried generating nknots equally spaced points in my x, but when I plotted the points that corresponded to my original y values at those equally-spaced x values, I found that the spline did not pass through them, which, perhaps naively, I thought it might. Also, the manual says that yin comprises "the y values used at the unique y values" -- should this read "at the unique x values"? Could someone kindly point to a resource where I can get a slightly fuller explanation? I looked at the code for smooth.spline, but can't readily follow it. Here's a toy example:> x<-seq(from=0,to=4*pi,length=1002) > y<-sin(x) > ss<-smooth.spline(x,y=y,all.knots=F,nknots=25) > ssCall: smooth.spline(x = x, y = y, all.knots = F, nknots = 25) Smoothing Parameter spar= -0.4573636 lambda= 1.006117e-09 (14 iterations) Equivalent Degrees of Freedom (Df): 26.99935 Penalized Criterion: 3.027077e-06 GCV: 3.190666e-09> str(ss)List of 15 $ x : num [1:1002] 0 0.0126 0.0251 0.0377 0.0502 ... $ y : num [1:1002] 2.88e-05 1.26e-02 2.51e-02 3.77e-02 5.02e-02 ... $ w : num [1:1002] 1 1 1 1 1 1 1 1 1 1 ... $ yin : num [1:1002] 0 0.0126 0.0251 0.0377 0.0502 ... $ data :List of 3 ..$ x: num [1:1002] 0 0.0126 0.0251 0.0377 0.0502 ... ..$ y: num [1:1002] 0 0.0126 0.0251 0.0377 0.0502 ... ..$ w: num [1:1002] 1 1 1 1 1 1 1 1 1 1 ... $ lev : num [1:1002] 0.2238 0.177 0.1399 0.1111 0.0891 ... $ cv.crit : num 3.19e-09 $ pen.crit: num 3.03e-06 $ crit : num 3.19e-09 $ df : num 27 $ spar : num -0.457 $ lambda : num 1.01e-09 $ iparms : Named int [1:3] 1 0 14 ..- attr(*, "names")= chr [1:3] "icrit" "ispar" "iter" $ fit :List of 5 ..$ knot : num [1:31] 0 0 0 0 0.041 ... ..$ nk : num 27 ..$ min : num 0 ..$ range: num 12.6 ..$ coef : num [1:27] 2.88e-05 1.72e-01 5.19e-01 9.04e-01 1.05 ... ..- attr(*, "class")= chr "smooth.spline.fit" $ call : language smooth.spline(x = x, y = y, all.knots = F, nknots 25) - attr(*, "class")= chr "smooth.spline">Many thanks! Regards, Mike Nielsen [[alternative HTML version deleted]]
David Winsemius
2013-Feb-27 18:19 UTC
[R] Finding the knots in a smoothing spline using nknots
On Feb 27, 2013, at 6:39 AM, Mike Nielsen wrote:> Hi r-helpers. > > Please forgive my ignorance, but I would like to plot a smoothing spline > (smooth.spline) from package "stats", and show the knots in the plot, and I > can't seem to figure out where smooth.spline has located the knots (when I > use nknots). Unfortunately, I don't know a lot about splines, but I know > that they provide me an easy way to estimate the location of local maxima > and minima on varying time-scales (number of knots) in my original data. > > I see there is a fit$knot, but it's not clear to me what those values are: > for some reason I had expected that they would be contained in my original > y values, but they're not.It appears they are in the range of [0-1] and the ss$fit$min and ss$fit$range provide the scaling data (.... for the x-values rather than the y-values):> unique(ss$fit$knot)[1] 0.00000000 0.04095904 0.08291708 0.12487512 0.16583417 0.20779221 0.24975025 0.29070929 [9] 0.33266733 0.37462537 0.41658342 0.45754246 0.49950050 0.54145854 0.58241758 0.62437562 [17] 0.66633367 0.70829171 0.74925075 0.79120879 0.83316683 0.87412587 0.91608392 0.95804196 [25] 1.00000000 I would think that in your case with x0 being 0 you could just use ss$fit$range*unique(ss$fit$knot) as your knot positions. In the more geneneral case you would need to add ss$fit$min. I tried confirming this hunch by looking "statiscal Models in S", inMASSe4, and at the R code but the R code calls a FORTRAN routine, so you would need to pull the source to confirm. -- David.> I tried generating nknots equally spaced points > in my x, but when I plotted the points that corresponded to my original y > values at those equally-spaced x values, I found that the spline did not > pass through them, which, perhaps naively, I thought it might. > > Also, the manual says that yin comprises "the y values used at the unique y > values" -- should this read "at the unique x values"? > > Could someone kindly point to a resource where I can get a slightly fuller > explanation? I looked at the code for smooth.spline, but can't readily > follow it. > > Here's a toy example: > >> x<-seq(from=0,to=4*pi,length=1002) >> y<-sin(x) >> ss<-smooth.spline(x,y=y,all.knots=F,nknots=25) >> ss > Call: > smooth.spline(x = x, y = y, all.knots = F, nknots = 25) > > Smoothing Parameter spar= -0.4573636 lambda= 1.006117e-09 (14 iterations) > Equivalent Degrees of Freedom (Df): 26.99935 > Penalized Criterion: 3.027077e-06 > GCV: 3.190666e-09 >> str(ss) > List of 15 > $ x : num [1:1002] 0 0.0126 0.0251 0.0377 0.0502 ... > $ y : num [1:1002] 2.88e-05 1.26e-02 2.51e-02 3.77e-02 5.02e-02 ... > $ w : num [1:1002] 1 1 1 1 1 1 1 1 1 1 ... > $ yin : num [1:1002] 0 0.0126 0.0251 0.0377 0.0502 ... > $ data :List of 3 > ..$ x: num [1:1002] 0 0.0126 0.0251 0.0377 0.0502 ... > ..$ y: num [1:1002] 0 0.0126 0.0251 0.0377 0.0502 ... > ..$ w: num [1:1002] 1 1 1 1 1 1 1 1 1 1 ... > $ lev : num [1:1002] 0.2238 0.177 0.1399 0.1111 0.0891 ... > $ cv.crit : num 3.19e-09 > $ pen.crit: num 3.03e-06 > $ crit : num 3.19e-09 > $ df : num 27 > $ spar : num -0.457 > $ lambda : num 1.01e-09 > $ iparms : Named int [1:3] 1 0 14 > ..- attr(*, "names")= chr [1:3] "icrit" "ispar" "iter" > $ fit :List of 5 > ..$ knot : num [1:31] 0 0 0 0 0.041 ... > ..$ nk : num 27 > ..$ min : num 0 > ..$ range: num 12.6 > ..$ coef : num [1:27] 2.88e-05 1.72e-01 5.19e-01 9.04e-01 1.05 ... > ..- attr(*, "class")= chr "smooth.spline.fit" > $ call : language smooth.spline(x = x, y = y, all.knots = F, nknots > 25) > - attr(*, "class")= chr "smooth.spline" >> > > Many thanks! > > > Regards, > > Mike Nielsen > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA