[R] block statistics with POSIX classes

Gabor Grothendieck ggrothendieck at myway.com
Thu Sep 23 12:56:05 CEST 2004


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)
z.m

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

# 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")
z.y.times

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




More information about the R-help mailing list