# RE: [R] ts to irts

From: Philippe Grosjean <phgrosjean_at_sciviews.org>
Date: Sat 24 Jul 2004 - 17:51:47 EST

... huummm, it seems you miss the functions in the PASTECS package. You will find there all you need to answer your question: - regul.screen(), which tests different time intervals and starting values and determines which one is the best (that is, which combinaison leads to the lower number of interpolated observations in the regular time series),

• regul.adj(), that represent how far are observations from interpolated values in the time axis, and allows you to determine if you would like to use a "tolerance window" (an example: your interval is 14 days, a tolerance window of +/- 1 day will allow you to keep an observation if it is within a window of +/- 1 day in the irregular time series compared to the regular time series. Otherwise, interpolation is used),
• regul(), that creates an object of the same name with a sophisticated plot() method to compare the irregular and regular time series, ident() to point sensitive observations with the mouse and give them more weight in the transformation from irregular to regular time series (let's say, isolated peaks that are possibly lost in the regular time series otherwise...). Also, regul() allows to treat many series as once, or to save parameters of the process and reapply them on other series at a later time very easily. It also proposes several methods for interpolation: constant, linear, spline, area. We found the area method particularly useful for hydroclimatic variables in oceanography... so, it would be worth trying them on your climatic data. As far as I know, this method is not proposed elsewhere in R.
• extract(), or as.tseries() that extract series or transform a 'regul' object into a ts() or mts() object.

If all this is powerful, it is also quite complex. I advise to follow examples in the PASTECS manual (currently only in French) that you can download from http://www.sciviews.org/pastecs/ to get familiarize with these functions and their philosophy.

Best,

Philippe Grosjean

.......................................................<?}))><....
) ) ) ) )
( ( ( ( ( Prof. Philippe Grosjean
\ ___ )
\/ECO\ ( Numerical Ecology of Aquatic Systems  /\___/ ) Mons-Hainaut University, Pentagone / ___ /( 8, Av. du Champ de Mars, 7000 Mons, Belgium  /NUM\/ )
\___/\ ( phone: + 32.65.37.34.97, fax: + 32.65.37.33.12

\ ) email: Philippe.Grosjean@umh.ac.be  ) ) ) ) ) SciViews project coordinator (http://www.sciviews.org) ( ( ( ( (

```...................................................................

```

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch]On Behalf Of Gabor Grothendieck Sent: Friday, 23 July, 2004 18:17
To: r-help@stat.math.ethz.ch
Subject: Re: [R] ts to irts

Gabor Grothendieck <ggrothendieck <at> myway.com> writes:

```:
: Jan Verbesselt <Jan.Verbesselt <at> agr.kuleuven.ac.be> writes:
:
: : I'm working with irregular time series (time series of climate data,
: : daily data 365/6 days a year) and would like to create regular time
: : series from them ( irts
: : 	e.g. Rain            <-
: : irts(as.POSIXct(Climate[,1]),Climate[,5])
: : 		to ts
: :     e.g. test <- ts(x, start=c(1997,1), frequency=365) )
: :
: : such that I can find where the gaps (lacking temperature data, ...) are
: : and try out methods to fill the gaps.
: :
: : The main objective is to detect gaps, how long they are and fill them if
: : possible with average, median values.
:
: First we create vectors of dates and values, d and val,
: to use as test data.
:
: Then we create, dd, a regular time vector of dates using
: class Date and a corresponding logical vector not.na
: indicating which dates in dd correspond to observations.
:
: Finally, we average the previous non-missing and next
: non-missing observation for each data point and convert the
: result to class ts.  The last line sets the time coordinates
: to the numeric representation of the Date class of the
: corresponding dates.  (If you leave it out the times will be
: 1,2,3,...)
:
: d <- structure(c(11,14,20,22), class = "Date")
: val <- c(10,20,30,40)
:
: dd <- seq(min(d), max(d), by = "day")
: not.na <- dd %in% d
:
: my.ts <- ts(val[cumsum(not.na)] + val[cumsum(not.na)+!not.na]/2)
: my.ts <- ts(my.ts, start = as.numeric(min(d)), end = as.numeric(max(d)))

```

I forgot one set of parentheses in the second last line. It should be:

d <- structure(c(11,14,20,22), class = "Date") val <- c(10,20,30,40)

dd <- seq(min(d), max(d), by = "day")
not.na <- dd %in% d

my.ts <- ts((val[cumsum(not.na)] + val[cumsum(not.na)+!not.na])/2) my.ts <- ts(my.ts, start = as.numeric(min(d)), end = as.numeric(max(d)))

R-help@stat.math.ethz.ch mailing list