Anyi Zhu
2010-Jun-07 03:11 UTC
[R] Computing day-over-day log return for a matrix containing multiple time series
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. Would apply function work in this case? Thanks a lot! [[alternative HTML version deleted]]
sayan dasgupta
2010-Jun-07 06:12 UTC
[R] Computing day-over-day log return for a matrix containing multiple 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))})) 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@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]]
Gabor Grothendieck
2010-Jun-07 09:19 UTC
[R] Computing day-over-day log return for a matrix containing multiple time series
You can use diff.zoo like this: library(zoo) z <- zoo(matrix(1:24, 6)) z diff(log(z)) # also try diff(z, arith = FALSE) - 1 See ?diff.zoo and read the three zoo vignettes (pdf documents): vignette(package = "zoo") # lists them vignette("zoo") etc. On Sun, Jun 6, 2010 at 11:11 PM, Anyi Zhu <anyi.zhu at 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. > > > > 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. >
Apparently Analagous Threads
- SMA and EMA in package TTR
- Converting daily data series to monthly series
- converting numbers in "YYYYMM" format to last calendar day and last exchange trading day of the month
- Efficient ways of merging data frames
- Calculating quarterly statistics for time series object