## Load Microsoft data -
# Microsoft Data:
setRmetricsOptions(myFinCenter = "GMT")
data(MSFT)
head(MSFT)
## Create a 'timeSeries' object, the direct Way ...
Close <- MSFT[, 5]
head(Close)
## Create a 'timeSeries' object from scratch -
data <- as.matrix(MSFT[, 4])
charvec <- rownames(MSFT)
Close <- timeSeries(data, charvec, units = "Close")
head(Close)
c(start(Close), end(Close))
## Cut out April data from 2001 -
tsApril01 <- window(Close, "2001-04-01", "2001-04-30")
tsApril01
## Compute Continuous Returns -
returns(tsApril01)
## Compute Discrete Returns -
returns(tsApril01, type = "discrete")
## Compute Discrete Returns, Don't trim -
returns(tsApril01, trim = FALSE)
## Compute Discrete Returns, Use Percentage Values -
tsRet <- returns(tsApril01, percentage = TRUE, trim = FALSE)
tsRet
## Aggregate Weekly -
GoodFriday(2001)
to <- timeSequence(from = "2001-04-11", length.out = 3, by = "week")
from <- to - 6*24*3600
from
to
applySeries(tsRet, from, to, FUN = sum)
## Create large 'timeSeries' objects with different 'charvec' object classes -
# charvec is a 'timeDate' object
head(timeSeries(1:1e6L, timeSequence(length.out = 1e6L, by = "sec")))
head(timeSeries(1:1e6L, seq(Sys.timeDate(), length.out = 1e6L, by = "sec")))
# 'charvec' is a 'POSIXt' object
head(timeSeries(1:1e6L, seq(Sys.time(), length.out = 1e6L, by = "sec")))
# 'charvec' is a 'numeric' object
head(timeSeries(1:1e6L, 1:1e6L))
Run the code above in your browser using DataLab