Skip to content

Smooth periodic splines

6 messages · cmr.Pent at gmail.com, Duncan Murdoch, Spencer Graves +2 more

#
Hello group!

Is there a package that allows to fit smooth *periodic* splines to
data? I'm interested in a function which combines the functionality of
smooth.spline and splines::periodicSpline.

Thanks,
Andrey
#
cmr.Pent at gmail.com wrote:
I don't know one, but you could use the same technique that 
periodicSpline uses:  repeat a copy of the data to the left and right of 
the main copy (similarly replicating the knots if you want regression 
splines rather than smoothing splines), then fit to the augmented 
dataset.  I don't think it is guaranteed to be exactly periodic, but it 
will be very close.

There is also the "periodic" option to splinefun and you might be able 
to use it to construct a true periodic basis, but you'll have to work 
out some tricky details to get that right.

Duncan Murdoch
#
1.  RSiteSearch('{periodic spline}') produced 12 hits.  I looked 
at the first five and found that four of them seemed relevant to your 
question. 

      2.  The third hit in this list notes that the DierckxSpline 
package has periodic splines, while 'fda' recommends finite Fourier 
series for periodic functions;  this hit documents an "as.fd" function 
that approximates a 'dierckx' periodic object with a periodic 'fd' object. 

      Hope this helps. 
      Spencer
Duncan Murdoch wrote:
#
2009/1/16 Spencer Graves <spencer.graves at pdf.com>:
Many thanks, DierckxSpline is what I was looking for.

Andrey
#
The "cc" and "cp" bases in package `mgcv' provide periodic splines, 
[e.g. gam(y~s(x,bs="cc"))], but this may not be exactly the functionality you 
want.

best,
Simon
On Friday 16 January 2009 08:42, cmr.Pent at gmail.com wrote:

  
    
#
Two other possibilities: 

      The 'DierckxSpline' package includes a function 'percur' for 
fitting periodic splines.  Unfortunately, it has a known bug that kills 
R with a segmentation fault, though it not affect your application. 

      The 'fda' package supports the use of finite Fourier series for 
fitting periodic functions.  Below please find code for this that I just 
posted to R-devel in response to a report of the 'percur' bug. 

      Hope this helps. 
      Spencer Graves

# problem
x <- seq(0.2, 0.8, 0.01)
y <- cos(2*pi*x2) + 0.1*rnorm(length(x))
plot(x, y, xlim=0:1)

# simple solution
library(fda)
Fourier1 <- create.fourier.basis()
FourierFit <- Data2fd(x, y, Fourier1)
plotfit.fd(y, x, FourierFit)

# Allow more flexibility
Fourier9 <- create.fourier.basis(nbasis=2*9+1)
# constant + 9 cosine & sine terms

# Naive initial solution
FourierSmooth0 <- smooth.basisPar(x, y, Fourier9)
plotfit.fd(y, x, FourierSmooth0$fd)
# Oops:  Need some smoothing

# Try again.
FourierSmooth1 <- smooth.basisPar(x, y, Fourier9, lambda=1)
plotfit.fd(y, x, FourierSmooth1$fd)
# Much better.

######################################
Simon Wood wrote: