[R] block statistics with POSIX classes

Kahra Hannu kahra at mpsgr.it
Thu Sep 23 15:04:48 CEST 2004

Thank you Petr and Gabor for the answers.

They did not, however, solve my original problem. When I have a monthly time series y with a POSIX date variable dp, the most obvious way to compute e.g. the annual means is to use aggregate(y, 1, mean) that works with time series. However, I got stuck with the idea of using the 'by' argument as by = dp$year. But in that case y has to be a data.frame. The easiest way must be the best way.


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Gabor Grothendieck
Sent: Thursday, September 23, 2004 12:56 PM
To: r-help at stat.math.ethz.ch
Subject: Re: [R] block statistics with POSIX classes

Kahra Hannu <kahra <at> mpsgr.it> writes:

: I have a monthly price index series x, the related return series y = diff(log
(x)) and a POSIXlt date-time
: variable dp. I would like to apply annual blocks to compute for example 
annual block maxima and mean of y.
: When studying the POSIX classes, in the first stage of the learning curve, I 
computed the maximum drawdown
: of x:
: > mdd <- maxdrawdown(x)
: > max.dd <- mdd$maxdrawdown
: > from <- as.character(dp[mdd$from]) 
: > to <- as.character(dp[mdd$to])                       
: > from; to
: [1] "2000-08-31"
: [1] "2003-03-31"
: that gives me the POSIX dates of the start and end of the period and 
suggests that I have done something correctly.
: Two questions:
: (1) how to implement annual blocks and compute e.g. annual max, min and mean 
of y (each year's max, min, mean)?
: (2) how to apply POSIX variables with the 'block' argument in gev in the 
evir package?
: The S+FinMetrics function aggregateSeries does the job in that module; but I 
do not know, how handle it in R.
: My guess is that (1) is done by using the function aggregate, but how to 
define the 'by' argument with POSIX variables?

1. To create a ts monthly time series you specify the first month
and a frequency of 12 like this.  

z.m <- ts(rep(1:6,4), start = c(2000,1), freq = 12)

# Annual aggregate is done using aggregate.ts with nfreq = 1
z.y <- aggregate(z.m, nfreq = 1, max)

# To create a POSIXct series of times using seq
# (This will use GMT.  Use tz="" arg to ISOdate if you want current tz.)
z.y.times <- seq(ISOdate(2000,1,1), length = length(z.y), by = "year")

2. Have not used evir but looking at ?gev it seems you can
use block = 12 if you have monthly data and want the blocks to be 
successive 12 month periods or you can add a POSIXct times attribute to 
your data as below (also see comment re tz above) and then use 
block = "year" in your gev call.

attr(z.m, "times") <- seq(ISOdate(2000,1,1), length=length(z.m), by="month")
str(z.m)  # display z.m along with attribute info

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list