Brijesh Gulati
2013-Aug-22 01:36 UTC
[R] cross-sectional analysis of a financial time series
Hi: I have a financial series data. For instance, one can take YHOO from the quantmod package.>library("quantmod")>getSymbols("YHOO")As you can see this series has date along with close prices. I want to do a cross-sectional analysis of the time series and want to see if there are any seasonal patterns in this. For instance, I want to see how the on average close price behaves a few days prior to last business day of the month. So, I want to see a cross sectional time series of average prices for the past 12-months. In other words, the resulting dataset would give me a series such as following Days_Prior_To_Month_End Avg_Price 10 25 9 24.3 8 22 7 21 6 23 5 24 4 22.2 3 24.8 2 24.9 1 25 0 26 In the above series, "0"would indicate the last day of the month. And the 26 would correspond to average price on the last day of the month for the past 12-month. Some issues with this kind of series: 1) Number of business day in a month is not fixed. Some months have 22 working day and others may have 23 or 21. So, the weekend data is not available. 2) Normally, one is interested in returns behavior, but for the sake of simplicity I used the example of just seeing average. Once I know the methodology, I can extend it to the returns. Any help would be greatly appreciated. [[alternative HTML version deleted]]
Joshua Ulrich
2013-Aug-22 13:09 UTC
[R] cross-sectional analysis of a financial time series
Below are two ways to accomplish this. The last() function will not pad with NA if you request more days than available, so you may need to handle that if it's an issue. I prefer the period.apply method because it keeps the data in an xts object, which I find easier to work with. getSymbols("YHOO") YHOO$YHOO.Return <- ROC(Ad(YHOO),type="discrete") f <- function(x,n) { coredata(last(x,n)) } # one way x <- sapply(split(YHOO$YHOO.Return, "months"), f, n=10) rowMeans(x) # another way ep <- endpoints(YHOO,"months") y <- period.apply(YHOO$YHOO.Return, ep, f, n=10) colMeans(y) Best, -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com On Wed, Aug 21, 2013 at 8:36 PM, Brijesh Gulati <brijgul at gmail.com> wrote:> Hi: > > I have a financial series data. For instance, one can take YHOO from the > quantmod package. > >>library("quantmod") > >>getSymbols("YHOO") > > > > As you can see this series has date along with close prices. I want to do a > cross-sectional analysis of the time series and want to see if there are any > seasonal patterns in this. For instance, I want to see how the on average > close price behaves a few days prior to last business day of the month. So, > I want to see a cross sectional time series of average prices for the past > 12-months. In other words, the resulting dataset would give me a series such > as following > > Days_Prior_To_Month_End Avg_Price > > 10 25 > > 9 24.3 > > 8 22 > > 7 21 > > 6 23 > > 5 24 > > 4 22.2 > > 3 24.8 > > 2 24.9 > > 1 25 > > 0 26 > > > > In the above series, "0"would indicate the last day of the month. And the 26 > would correspond to average price on the last day of the month for the past > 12-month. > > > > Some issues with this kind of series: > > 1) Number of business day in a month is not fixed. Some months have 22 > working day and others may have 23 or 21. So, the weekend data is not > available. > > 2) Normally, one is interested in returns behavior, but for the sake of > simplicity I used the example of just seeing average. Once I know the > methodology, I can extend it to the returns. > > Any help would be greatly appreciated. > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.