Learn R Programming

timeSeries (version 280.75)

na: Handling Missing Time Series Values

Description

Functions for handling missing values in 'timeSeries' objects or in objects which can be transformed into a vector or a two dimensional matrix. The functions are listed by topic. ll{ na.omit Handles NAs, removeNA Removes NAs from a matrix object, substituteNA substitute NAs by zero, the column mean or median, interpNA interpolates NAs using R's "approx" function. }

Usage

## S3 method for class 'timeSeries':
na.omit(object, method = c("r", "s", "z", "ir", "iz", "ie"), 
    interp = c("before", "linear", "after"), ...)

removeNA(x, ...) substituteNA(x, type = c("zeros", "mean", "median"), ...) interpNA(x, method = c("linear", "before", "after"), ...)

Arguments

interp, type
[nna.omit][substituteNA] - Three alternative methods are provided to remove NAs from the data: type="zeros" replaces the missing values by zeros, type="mean" replaces the missing values by the column mea
method
[na.omit] - Specifies the method how to handle NAs. One of the applied vector strings: method="s" na.rm = FALSE, skip, i.e. do nothing, method="r" remove NAs, method="z" substitute NAs by
object
an object of class("timeSeries").
x
a numeric matrix, or any other object which can be transformed into a matrix through x = as.matrix(x, ...). If x is a vector, it will be transformed into a one-dimensional matrix.
...
arguments to be passed to the function as.matrix.

Details

Missing Values in Price and Index Series: Applied to timeSeries objects the function removeNA just removes rows with NAs from the series. For an interpolation of time series points one can use the function interpNA. Three different methods of interpolation are offered: "linear" does a linear interpolation, "before" uses the previous value, and "after" uses the following value. Note, that the interpolation is done on the index scale and not on the time scale. Missing Values in Return Series: For return series the function substituteNA may be useful. The function allows to fill missing values either by method="zeros", the method="mean" or the method="median" value of the appropriate columns.

References

Troyanskaya O., Cantor M., Sherlock G., Brown P., Hastie T., Tibshirani R., Botstein D., Altman R.B., (2001); Missing Value Estimation Methods for DNA microarrays Bioinformatics 17, 520--525.

Examples

Run this code
## Create a Matrix with NAs:
   X = matrix(rnorm(100), ncol = 5)
   # a single NA inside:
   X[3, 5] = NA
   # three in a row inside:
   X[17, 2:4] = c(NA, NA, NA)
   # three in a column inside:
   X[13:15, 4] = c(NA, NA, NA)
   # two at the right border:
   X[11:12, 5] = c(NA, NA)
   # one in the lower left corner:
   X[20, 1] = NA
   print(X)
     
## removeNA -
   # Remove rows with NA's
   removeNA(X)
   # Now we have only 12 lines!
   
## substiuteNA -
   # Subsitute NA's by zeros or column mean
   substituteNA(X, type = "zeros")
   substituteNA(X, type = "mean")
   
## interpNA - 
   # Interpolate NA's liearily:
   interpNA(X, method = "linear")
   # Note the corner missing value cannot be interpolated!
   # Take previous values in a column:
   interpNA(X, method = "before")
   # Also here, the corner value is excluded

Run the code above in your browser using DataLab