Learn R Programming

timeSeries (version 280.75)

TimeSeriesClass: timeSeries Class

Description

A collection and description of functions and methods dealing with regular and irregular 'timeSeries' objects. Dates and times are implemented as 'timeDate' objects. Functions to generate and modify 'timeSeries' objects:

ll{ timeSeries Creates a 'timeSeries' object from scratch, readSeries Reads a 'timeSeries' from a spreadsheet file. } Data Slot and classification of 'timeSeries' objects: ll{ seriesData Extracts data slot from a 'timeSeries'. }

Usage

timeSeries(data, charvec, units = NULL, format = NULL, zone = "", 
    FinCenter = "", recordIDs = data.frame(), title = NULL, 
    documentation = NULL, ...)
       
readSeries(file, header = TRUE, sep = ";", zone = "", 
    FinCenter = "", title = NULL, documentation = NULL, ...)

seriesData(object)

Arguments

charvec
a character vector of dates and times.
data
a data.frame or a matrix object of numeric data.
documentation
optional documentation string, or a vector of character strings.
file
the filename of a spreadsheet data set from which to import the data records.
FinCenter
a character with the the location of the financial center named as "continent/city".
header
a logical value indicating whether the file contains the names of the variables as its first line. If missing, the value is determined from the file format: 'header' is set to 'TRUE' if and only if the first row contains one fewer
format
the format specification of the input character vector, [as.timeSeries] - a character string with the format in POSIX notation to be passed to the time series object.
object
[is][seriesData][seriesPositions][show][summary] - an object of class timeSeries.
recordIDs
a data frame which can be used for record identification information. [print] - a logical value. Should the recordIDs printed together with the data matrix and time series positions?
sep
[readSeries] - the field seperator used in the spreadsheet file to separate columns.
title
an optional title string, if not specified the inputs data name is deparsed.
units
an optional character string, which allows to overwrite the current column names of a timeSeries object. By default NULL which means that the column names are selected automatically.
zone
the time zone or financial center where the data were recorded.
...
arguments passed to other methods.

Value

  • timeSeries readSeries returnSeries return a S4 object of class timeSeries. orderStatistics returns ... series extracts the @.Data slot from a timeSeries object and is equivalent to as.amtrix.

Details

Generation of Time Series Objects: We have defined a timeSeries class which is in many aspects similar to the S-Plus class with the same name, but has also some important differences. The class has seven Slots, the 'Data' slot which holds the time series data in matrix form, the 'position' slot which holds the time/date as a character vector, the 'format' and 'FinCenter' slots which are the same as for the 'timeDate' object, the 'units' slot which holds the column names of the data matrix, and a 'title' and a 'documentation' slot which hold descriptive character strings. Date and time is managed in the same way as for timeDate objects.

Examples

Run this code
## data -
   # Microsoft Data: 
   setRmetricsOptions(myFinCenter = "GMT")
   MSFT = as.timeSeries(data(msft.dat))
   head(MSFT)

## timeSeries -  
   # Create a timeSeries Objec - The Direct Way ...
   Close = MSFT[, 5]
   head(Close)
   # From Scratch ...
   data = as.matrix(MSFT[, 4])
   charvec = rownames(MSFT)
   Close = timeSeries(data, charvec, units = "Close")
   head(Close)
   c(start(Close), end(Close))
    
## window  -    
   # Cut out April Data from 2001:
   tsApril01 = window(Close, "2001-04-01", "2001-04-30") 
   tsApril01

## returns -  
   # Continuous Returns:
   returns(tsApril01)
   # Discrete Returns:
   returns(tsApril01, type = "discrete")
   # Don't trim:
   returns(tsApril01, trim = FALSE)
   # Use Percentage Values:
   tsRet = returns(tsApril01, percentage = TRUE, trim = FALSE)
   tsRet
     
## applySeries -   
   # 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)

Run the code above in your browser using DataLab