[R-SIG-Finance] filter() on zoo objects

Jeff Ryan jeff.a.ryan at gmail.com
Thu Mar 27 18:25:15 CET 2008


One comment (caution?) to add to Gabor's excellent points:

If you use the *timeSeries* class - be careful as to the conversion of
irregular series to regular.

as.ts.timeSeries converts what _may_ be an irregular series into a
_regular_ one by dropping the time altogether - much like the
ts(coredata(z)) approach outlined by Gabor.

While this may be what you want - it is not necessarily correct IMO.
In fact just the choice of class can lead to unintended consequences:

Please ignore the simplicity of the example:

> getSymbols("MSFT",ret='zoo')
> arima(Cl(MSFT),order=c(2,0,0))

Call:
arima(x = Cl(MSFT), order = c(2, 0, 0))

Coefficients:
         ar1     ar2  intercept
      0.8375  0.1437    30.1932
s.e.  0.0631  0.0633     1.0612

sigma^2 estimated as 0.2238:  log likelihood = -239.51,  aic = 487.01

> arima(as.ts(coredata(Cl(MSFT))),order=c(2,0,0))

Call:
arima(x = as.ts(coredata(Cl(MSFT))), order = c(2, 0, 0))

Coefficients:
         ar1     ar2  intercept
      0.9582  0.0190    30.1612
s.e.  0.0569  0.0569     1.0718

sigma^2 estimated as 0.2359:  log likelihood = -216.81,  aic = 441.62


> getSymbols("MSFT",ret='timeSeries')
[1] "MSFT"
> arima(Cl(MSFT),order=c(2,0,0))

Call:
arima(x = Cl(MSFT), order = c(2, 0, 0))

Coefficients:
         ar1     ar2  intercept
      0.9582  0.0190    30.1612
s.e.  0.0569  0.0569     1.0718

sigma^2 estimated as 0.2359:  log likelihood = -216.81,  aic = 441.62
>

To me, the timeSeries class is making a decision that is not
necessarily the intended one (in general) - and the results are
therefore possibly different than what you might expect.

Jeff

On Thu, Mar 27, 2008 at 12:06 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Two points:
>
>  1. If you have a routine that accepts ts but not zoo then you have a problem
>  even beyond representations since the routine is assuming equally
>  spaced points.  Assuming your data is not equally spaced, e.g.
>  no data on weekends, then its up to you to figure out how you
>  want to map it to equally spaced points.  Two possibilities for
>  zoo object z are:
>
>  - as.ts(z) which will make it equally spaced by inserting NAs (e.g.
>  where weekends are)
>
>  - ts(coredata(z)) which uses the time base 1, 2, 3, ... or you can
>  use other args of ts to use a different time base.
>
>  If the routine you are calling returns a ts object, out,
>  then it may or may not be that as.zoo(out) or
>  zoo(coredata(out), time(z)) make sense.  It will all
>  depend on the situation.  You may have to write
>  a custom mapping to the time base or not use that
>  ts-only routine in the first place -- see first paragraph above.
>
>  2. Aside from rollapply and friends,  The last question in the zoo
>  faq gives a list of the packages that work with zoo objects --
>  there are now about 20 of them.  For example, dyn and dynlm
>  packages can perform regression on zoo series with lags
>  and diffs and keep track of the time base.  You want to stick
>  with zoo-capable routines if possible.
>
>
>  On Thu, Mar 27, 2008 at 6:17 AM, Vorlow Constantinos
>  <CVorlow at eurobank.gr> wrote:
>
>
> > Dear all,
>  >
>  > Am I right in understanding you cannot directly apply functions such as
>  > loess() (filter() as well ?) to zoo objects and you need to use the
>  > rollapply/rollmean functions instead?
>  >
>  > For example:
>  >
>  > library("tseries")
>  > DJ<- get.hist.quote("^DJI", start = "1990-01-01", quote = "Close")
>  > DJret<-diff(log(DJ))
>  >
>  > # the
>  >
>  > loess(DJret, time(DJret))
>  >
>  > does not work(?). What am I understanding  wrong? I found a reply by
>  > Achim on a similar issue.
>  >
>  > http://finzi.psych.upenn.edu/R/Rhelp02a/archive/88599.html
>  >
>  > Is Achim's answer all there is to it?
>  >
>  > I.e., my problem is a little bit  more general:
>  >
>  > Say I want to produce fits and then forecasts on a time series (zoo
>  > mostly) using a non-zoo routine which "understands" ts or timeSeries
>  > objects (or simple vectors).  Do I always have to "translate" zoo
>  > objects to vectors or to ts/simeSeries ones, run the routines and then
>  > put back the zoo attribute with the appropriate dates so as to produce
>  > correct time-series plots (with dates etc, especially correct dates
>  > alligned to the forecast period - which could be postsample)?
>  >
>  > Is there an easier way to shift between zoo-ts/timeSeries objects and
>  > produce plots statistical analysis that will have the dates correctly
>  > aligned on (to account for example for irregularly sampled sequences
>  > such as stock prices with non-trading days etc. etc.)?
>  >
>  > Thanks in advance,
>  > Costas
>  >
>  >
>  >
>  > P Think before you print.
>  >
>  > Disclaimer:
>  > This e-mail is confidential. If you are not the intended recipient, you should not copy it, re-transmit it, use it or disclose its contents, but should return it to the sender immediately and delete the copy from your system.
>  > EFG Eurobank Ergasias S.A. is not responsible for, nor endorses, any opinion, recommendation, conclusion, solicitation, offer or agreement or any information contained in this communication.
>  > EFG Eurobank Ergasias S.A. cannot accept any responsibility for the accuracy or completeness of this message as it has been transmitted over a public network. If you suspect that the message may have been intercepted or amended, please call the sender.
>  >
>  >
>  >        [[alternative HTML version deleted]]
>  >
>  > _______________________________________________
>  > R-SIG-Finance at stat.math.ethz.ch mailing list
>  > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>  > -- Subscriber-posting only.
>  > -- If you want to post, subscribe first.
>  >
>
>  _______________________________________________
>  R-SIG-Finance at stat.math.ethz.ch mailing list
>  https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>  -- Subscriber-posting only.
>  -- If you want to post, subscribe first.
>



-- 
There's a way to do it better - find it.
Thomas A. Edison



More information about the R-SIG-Finance mailing list