Just trying to create returns from prices, and do something like: returns.z = tail(prices.z,-1)/head(prices.z,-1) - 1 # should be equivalent to returns = exp(diff(log(prices.z))) - 1 Curiously, I get a zoo object back with zeros everywhere and also with the index having one fewer element than it should. Does anyone know how to pointwise divide zoo objects, and what exactly "/" is doing? [[alternative HTML version deleted]]
Hi,> returns.z = tail(prices.z,-1)/head(prices.z,-1) - 1 # should be equivalent > to returns = exp(diff(log(prices.z))) - 1 > > Curiously, I get a zoo object back with zeros everywhere and also with the > index having one fewer element than it should. > > Does anyone know how to pointwise divide zoo objects, and what exactly "/" > is doing? >I think any arithmetic operations on zoo objects work on values having the same indexes. So if x <- zoo(1:10, 1:10) y <- zoo(1:10, 11:20) x/y # no result as no elements have the same index y <- zoo(1:10, 6:15) x/y # last five elements from x, first five from z To do your first example, you can use coredata so that one of your obejcts would be an ordinary vector: returns.z = tail(prices.z,-1)/coredata(head(prices.z,-1)) - 1 Regards, Kenn [[alternative HTML version deleted]]
On Mon, Mar 14, 2011 at 10:16 PM, Adam Gustafson <amg81 at uw.edu> wrote:> Just trying to create returns from prices, and do something like: > > returns.z = tail(prices.z,-1)/head(prices.z,-1) - 1 ?# should be equivalent > to returns = exp(diff(log(prices.z))) - 1 > > Curiously, I get a zoo object back with zeros everywhere and also with the > index having one fewer element than it should. > > Does anyone know how to pointwise divide zoo objects, and what exactly "/" > is doing? >Try this: diff(x, arithmetic = FALSE) - 1 and see ?diff.zoo -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Seemingly Similar Threads
- Pointwise Confidence Bounds on Logistic Regression
- pointwise confidence bands or interval values for a non parametric sm.regression
- use of predict() with confidence/prediction bands
- bootstrap based confidence band
- [R-pkgs] bpcp package for pointwise confidence intervals for a survival distribution