[R] cross-sectional analysis of a financial time series

Brijesh Gulati brijgul at gmail.com
Fri Aug 23 01:37:27 CEST 2013


Hello Joshua:
	First of all, I thank you for the solution. The solution is quite
elegant and concise. I have one follow-up question/comments. Everything
seems ok but the problem comes if the last month stops somewhere in the
middle of the month. For instance, if I run the data as of yesterday
(8/21/13), then the last day becomes 8/21/13 for that month. Is there any
way to ensure that does not happen. Also, a better solution for last() not
padding it with NA is an issue. Could you suggest a good way. I know I seem
to be asking some trivial questions and probably can come up with some sort
of solution, but looking at your elegant solution, I want to find the best
way.
	Again, I greatly appreciate your help.
Regards,
Brijesh 

-----Original Message-----
From: Joshua Ulrich [mailto:josh.m.ulrich at gmail.com] 
Sent: Thursday, August 22, 2013 9:10 AM
To: Brijesh Gulati
Cc: R-Help
Subject: Re: [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.



More information about the R-help mailing list