Learn R Programming

FatTailsR (version 2.0.0)

fatreturns: Simple and Elaborated Prices to Returns

Description

fatreturns is an elaborated function to compute prices to returns. It includes a pre-treatment for negative prices. It computes either log-returns (default) or percentage-returns. It handles properly NA values in the input vector, replacing them by 0 in the output vector. Doing so, it warrants that the sum of the log-returns (when selected) is equal to the difference of the log-prices. It works with vector, matrix, data.frame, timeSeries, xts, zoo, list, list of lists and even list of vector, data.frame, timeSeries, xts, zoo mixed together. The returned object is of same dimension and same class than the input object with the first line filled with 0. The results may be as per one, per cent (default), per thousand and per ten thousand.

logreturns is an improved version of function 100*diff(log(x)) to handle vector, matrix, data.frame and list. It handles properly the first line and the NA values. It does not control time, rownames and colnames but may return them.

Usage

fatreturns(x, log = TRUE, per = "cent", e = NULL, dfrcol = 1,
  na.rm = TRUE)

logreturns(x)

replaceNA(x)

Arguments

x

The prices (vector, data.frame, matrix, timeSeries, xts, zoo, list).

log

boolean. log returns or percentage returns.

per

character. Either "one", "cent, "thousand", "tenthousand" or "o", "c", "th", "te". Multiply the result by 1, 100, 1000, 10000.

e

NULL or positive numeric. NULL is for no change f(x)=x. A positive numeric designates the focal point of the hyperbola to turn negative prices into positive prices, keeping the hierarchy: f(x)=(x+sqrt(x*x+e*e))/2. There is currently no rules of thumb for the optimal value of e. See elevate.

dfrcol

integer. For data.frame only, designates the column that handles the time and must be processed separately. Use dfrcol = 0 if all columns must be processed and there is no time (or turn the data.frame to a matrix).

na.rm

boolean. Replace x[t]=NA with the previous non-NA value available in the price serie such that (x[t-1], x[t]=x[t-1], x[t+1]) and calculate the returns accordingly. Force 0 in the first line of the returns if x[1]=NA.

Examples

Run this code

fatreturns(extractData())
logreturns(extractData())

Run the code above in your browser using DataLab