[R] Aggregating frequency of irregular time series

Whit Armstrong Whit.Armstrong at tudor.com
Wed Mar 31 15:43:53 CEST 2004


You are quite right.

One can see from the original function I wrote that I am not very familiar
with the apply family.

Thanks for pointing out this improvement.

Below is the new version of the function; any other comments are welcome.

Regards,
Whit

Timebase <- function(x,monthly=T,first.day=T) {
	if(length(dim(x))!=2) stop("can only take 2 dimensional data.")

	seriesDates <- as.integer(Dates(x))
	
	# year and Month
	seriesYM <- as.integer(seriesDates/100)
	
	# for each seriesYM grab the max day of month
	ans.dates <- as.numeric(tapply( seriesDates, seriesYM, max ))

	ans <- matrix(NA,nrow=length(ans.dates),ncol=ncol(x))
	
	colnames(ans) <- colnames(x)
	ans[] <- x[as.character(ans.dates),]
	
	if(first.day==T) {
		rownames(ans) <-
paste(substring(as.character(ans.dates),1,6),"01",sep="")
	} else {
		rownames(ans) <- ans.dates
	}
	
	as.tseries(ans)
}

-----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: Tuesday, March 30, 2004 8:59 PM
To: r-help at stat.math.ethz.ch
Subject: Re: [R] Aggregating frequency of irregular time series




I am not 100% sure I understand what you are trying to accomplish but if its
to calculate the highest date in seriesDates for each value of 
seriesYM then you can do it without a loop like this (untested):

   tapply( seriesDates, seriesYM, max )

Whit Armstrong <Whit.Armstrong <at> tudor.com> writes:

: 
: This function will probably work for you if your dates are in the format
: yyyymmdd.  Use it to convert the series from daily to monthly, then use a
1
: period difference to calc the returns of the series.
: 
: Feel free to contact me off list with any questions.
: 
: Dates <- function(x) rownames(x)
: 
: Timebase <- function(x,monthly=T,first.day=T) {
: 	if(length(dim(x))!=2) stop("can only take 2 dimensional data.")
: 
: 	seriesDates <- as.integer(Dates(x))
: 	
: 	# year and Month
: 	seriesYM <- as.integer(seriesDates/100)
: 	seriesYM.unique <- sort(unique(seriesYM))
: 	ans.nrow <- length(seriesYM.unique)
: 
: 	# loop through all the unique months and grab the max day of month
: 	ans <- matrix(NA,nrow=ans.nrow,ncol=ncol(x))
: 	ans.dates <- vector("numeric",ans.nrow)
: 	
: 	for (i in 1:ans.nrow) {
: 	
: 		this.month <- seriesYM == seriesYM.unique[i]
: 		max.day <- max(seriesDates[this.month])		
: 		ans.dates[i] <- max.day
: 	}
: 
: 	
: 	colnames(ans) <- colnames(x)
: 	ans[] <- x[as.character(ans.dates),]
: 	
: 	if(first.day==T) {
: 		rownames(ans) <-
: paste(substring(as.character(ans.dates),1,6),"01",sep="")
: 	} else {
: 		rownames(ans) <- ans.dates
: 	}
: 	
: 	as.tseries(ans)
: }
: 
: -----Original Message-----
: From: r-help-bounces <at> stat.math.ethz.ch
: [mailto:r-help-bounces <at> stat.math.ethz.ch] On Behalf Of Patrick Burns
: Sent: Tuesday, March 30, 2004 2:37 PM
: To: Ajay Shah
: Cc: r-help <at> stat.math.ethz.ch; Ivan Alves
: Subject: Re: [R] Aggregating frequency of irregular time series
: 
: 
: Assuming that you are using log returns, one approach (probably not the
: best) is to convert the dates to julian days, find all of the Sundays
: spanning the dates in the series, then do something along the lines of:
: 
: tapply(the.log.returns, cut(julian.days, sundays), sum)
: 
: Patrick Burns
: 
: Burns Statistics
: patrick <at> burns-stat.com
: +44 (0)20 8525 0696
: http://www.burns-stat.com
: (home of S Poetry and "A Guide for the Unwilling S User")
: 
: Ajay Shah wrote:
: 
: >>S-Plus has the function AggregateSeries() whose name is self 
: >>explanatory.  For instance one can derive monthly series from daily 
: >>ones by specifying end-of-period, averages, sums, etc.  I looked for a 
: >>similar function in the packages "its" and "tseries", but found 
: >>nothing.  I also help.searched() for aggregate to no avail.  Would 
: >>anybody be so kind to point me in the right direction?
: >>    
: >>
: >
: >I once needed a function which would convert daily stock prices into 
: >weekly returns. I know, the code is pretty bad (it is all loops and 
: >it's very slow), but my knowledge of R is weak and I really needed it, 
: >so I just used brute force. See EOF for the function. Gabor and Dirk 
: >and Brian Ripley and others on the list were very helpful to me in 
: >getting to the point where I could write this, though obviously they 
: >should not be blamed for my bad code! 
: >
: >I would be very happy if listers could give me ideas on how to do this 
: >better.
: >
: >Daily to monthly is innately easier since months are 'more normal' than 
: >weeks. I have perl code which does this, which supports 2 cases: 
: >Reporting the last traded price (LTP) of the month versus reporting the 
: >average of the month. If this is useful to you, ask me.
: >
: >What are the specialised finance libraries available with S-Plus? Can 
: >one marry R with commercial S libraries? I don't like having a 
: >dependence on commercial code, but I might be willing to compromise and 
: >buy libraries that work with R.
: >
: >Thanks,
: >
: >  
: >
: 
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://www.stat.math.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide!
: http://www.R-project.org/posting-guide.html
: 
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://www.stat.math.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
: 
:

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




More information about the R-help mailing list