Re: [R] Computing day-over-day log return for a matrix containingmultiple time series

From: William Dunlap <wdunlap_at_tibco.com>
Date: Mon, 07 Jun 2010 09:56:03 -0700

> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of sayan dasgupta
> Sent: Sunday, June 06, 2010 11:13 PM
> To: anyi.zhu_at_gmail.com
> Cc: r-help_at_r-project.org
> Subject: Re: [R] Computing day-over-day log return for a
> matrix containingmultiple time series
>
> Hope this helps
>
> a <- matrix(runif(150),nrow=3,ncol=50)
> p2r <- function(x) 100 * diff(log(x))
> t(apply(a,1,function(x){p2r(c(x))}))

The following expressions compute the same thing as that t(apply(...)) does:
  100 * (log(a[,-1]) - log(a[,-ncol(a)])) or
  100 * log(a[,-1]/a[,-ncol(a)])

The [,-1] (all but the first column) and [,-ncol(a)] (all but the last column) is a way to work with lagged data.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

>
>
>
>
> On Mon, Jun 7, 2010 at 8:41 AM, Anyi Zhu <anyi.zhu@gmail.com> wrote:
>
> > Hi all,
> >
> >
> >
> > Thanks a lot for anyone's help in advance.
> >
> >
> >
> > I am trying to find a way to compute the day-to-day return
> (log return)
> > from
> > a n x r matrix containing, n different stocks and price
> quotes over r days.
> > The time series of prices are already split by using
> unstack function.
> >
>
> >
> >
> > For the result, I would like to see a n x (r-1) matrix,
> where by each entry
> > is the day-over-day return of each stock.
> >
> >
> >
> > I tried to look into the zoo package, however it seems to
> give only the
> >
> plots but not the actual data.
> >
> take a look at
> vignette("zoo-quickref",package="zoo")
> It gives an exact solution to your problem
>
>
>
> >
> >
> >
> > Would apply function work in this case?
> >
> >
> >
> > Thanks a lot!
> >
> >
> > [[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.
> >
>
> [[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.
>



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. Received on Mon 07 Jun 2010 - 16:59:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 07 Jun 2010 - 18:50:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive