Skip to content

Moving Average

7 messages · Gabor Grothendieck, David Winsemius, mauede at alice.it +2 more

#
See rollapply in zoo or filter or embed in the core of R.
On Thu, Feb 26, 2009 at 7:07 AM, <mauede at alice.it> wrote:
#
I saw Gabor's reply but have a clarification to request. You say you  
want to remove low frequency components but then you request smoothing  
functions. The term "smoothing" implies removal of high-frequency  
components of a series.

If smoothing really is your goal then additional R resource would be  
smooth.spline, loess (or lowess), ksmooth, or using smoothing terms in  
regressions. Venables and Ripley have quite a few worked examples of  
such in MASS.
#
On 26-Feb-09 13:54:51, David Winsemius wrote:
If you produce a smoothed series, your result of course contains
the low-frequency comsponents, with the high-frequency components
removed.

But if you then subtract that from the original series, your result
contains the high-frequency components, with the low-frequency
compinents removed.

Moving-average is one way of smoothing (but can introduce periodic
components which were not there to start with).

Filtering a time-series is a very open-ended activity! In many
cases a useful start is exploration of the spectral properties
of the series, for which R has several functions. 'spectrum()'
in the stats package (loaded bvy default) is one basic function.
help.search("time series") will throw up a lot of functions.

You might want to look at package 'ltsa' (linear time series
analysis).

Alternatively, if yuou already have good information about the
frequency-structure of the series, or (for instance) know that
it has a will-defined seasonal component, then you could embark
on designing a transfer function specifically tuned to the job.
Have a look at RSiteSearch("{transfer function}")

Hoping this helps,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 26-Feb-09                                       Time: 14:54:43
------------------------------ XFMail ------------------------------
#
I wrote a little code using Fourier filtering if you would like to
take a look at this:

library(StreamMetabolism)
library(mFilter)
x <- read.production(file.choose())
#contiguous.zoo(data.frame(x[,"RM202DO.Conc"], coredata(x[,"RM202DO.Conc"])))
#contiguous.zoo(data.frame(x[,"RM61DO.Conc"], coredata(x[,"RM61DO.Conc"])))
short <- x[42685:48535,"RM202DO.Conc"]
#short <- x[53909:59957,"RM61DO.Conc"]
short.ts <- ts(coredata(short), frequency=96)
#fourier filtering
short.fft <- fft(short.ts)
plot(Re(short.fft), xlim=c(0,10), ylim=c(-1000, 1000))
short.fft[789:5563] = 0+0i
short.ifft = fft(short.fft, inverse = TRUE)/length(short.fft)
#zoo series
filt <- zoo(coredata(Re(short.ifft)) , index(short))



par(mfrow=c(2,1))
plot(short)
plot(filt)

window.plot <- function(x, y, a, b, s, d){
	par(mfrow=c(2,1))
	plot(window.chron(x, a, b, s, d))
	plot(window.chron(y, a, b, s, d))
	}
window.plot(short, filt, "04/17/2007", "00:01:00", "04/17/2007", "23:46:00")


plot.e <- function(b, w, x, y, z){
a <- window.chron(b, w, x, y, z)
plot(a, ylim=range(a)+0.06*c(-1, 1))
lines(a*0.98, col="blue")
lines(a*1.02, col="red")
}

it may not be exactly what you want, but you will have a handle on
what spectral properties that you have removed.

On Thu, Feb 26, 2009 at 9:54 AM, Ted Harding
<Ted.Harding at manchester.ac.uk> wrote:

  
    
#
On Feb 26, 2009, at 9:54 AM, (Ted Harding) wrote:

            
Yes. The time series term would be "detrending" or "de-trending".
As the OP's reply indicates, she is already using wavelet analysis.
My question at this point is whether she should just be advised to  
ignore
the low frequency components and concentrate on the middle and high
frequency components. If you already have some sort of spectral
decomposition, there should be no necessity of a subtraction or
de-trending step.