Saturday 15 January 2011

Create Growth Of $1,000 Column in XTS Object in R -



Create Growth Of $1,000 Column in XTS Object in R -

i struggling thought simple task. calculating investment returns , create column shows growth of $1,000 next returns column. ideally, this:

index homecoming 4/27/90 -0.011444191 989 5/30/90 0.000582282 989 6/28/90 0.007011319 996 7/30/90 0.005969896 1,002 10/30/90 0.007221412 1,009

the 3rd column equal value above times (1+return) , fills down. value of 1,009 obtained multiplying 1002*(1+0.005969896)

i have tried lag function head spinning @ moment staring @ long.

i did succeed in getting final number of 1,009 using this:

business relationship = 1000 (i in na.omit(sample$return)){ business relationship = (1+i) * business relationship }

however, have running total column in xts object. how can done?

this create xts object using in table above without 3rd column.

sample <- structure(c(-0.0114441907965878, 0.000582281990849955, 0.00701131947964186, 0.00596989588048895, 0.00722141248178086), .indexclass = "date", tclass = "date", tzone = "utc", src = "yahoo", updated = structure(1414972280.65536, class = c("posixct", "posixt")), index = structure(c(641174400, 644025600, 646531200, 649296000, 657244800), tclass = "date"), .dim = c(5l, 1l), .dimnames = list( null, "return"), class = c("xts", "zoo"))

thank you.

try cumprod

sample$account <- 1000 * cumprod(1+sample$return) sample homecoming business relationship 1990-04-27 -0.011444191 988.5558 1990-05-30 0.000582282 989.1314 1990-06-28 0.007011319 996.0665 1990-07-30 0.005969896 1002.0130 1990-10-30 0.007221412 1009.2489

r xts

No comments:

Post a Comment