Learn R Programming

timeSeries (version 4041.111)

na: Handle missing values in 'timeSeries' objects

Description

Functions for handling missing values in "timeSeries" objects.

Usage

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

Arguments

object

an object of class "timeSeries".

method

the method of handling NAs, see section ‘Details’.

interp

Three alternative methods are provided to remove NAs from the data: type="zeros" replaces the missing values with zeros, type="mean" replaces the missing values with the column mean, type="median" replaces the missing values with the column median.

FUN

a function or a name of a function, such as "mean" or median. FUN is applied to the non-NA values in each column to determine the replacement value. The call looks like FUN(coli, na.rm = TRUE), so FUN should have argument na.rm. All arguments except object are ignored if FUN is specified.

...

arguments to be passed to the function as.matrix.

Details

Functions for handling missing values in "timeSeries" objects and in objects which can be transformed into a vector or a two dimensional matrix.

For na.omit argument method specifies how to handle NAs. Can be one of the following strings:

method = "s"

na.rm = FALSE, skip, i.e. do nothing,

method = "r"

remove NAs,

method = "z"

substitute NAs by zeros,

method = "ir"

interpolate NAs and remove NAs at the beginning and end of the series,

method = "iz"

interpolate NAs and substitute NAs at the beginning and end of the series,

method = "ie"

interpolate NAs and extrapolate NAs at the beginning and end of theseries.

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.

See Also

alignDailySeries

Examples

Run this code
X <- matrix(rnorm(100), ncol = 5)  # Create a Matrix X
X[3, 5] <- NA                      # Replace a Single NA Inside
X[17, 2:4] <- c(NA, NA, NA)        # Replace Three in a Row Inside
X[13:15, 4] <- c(NA, NA, NA)       # Replace Three in a Column Inside
X[11:12, 5] <- c(NA, NA)           # Replace Two at the Right Border
X[20, 1] <- NA                     # Replace One in the Lower Left Corner
X
Xts <- timeSeries(X)  # convert X to timeSeries Xts

## remove rows with NAs
na.omit(Xts)

## Subsitute NA's with zeros or column means (formerly substituteNA())
na.omit(Xts, method = "z")
na.omit(Xts, FUN = "mean")
na.omit(Xts, FUN = "median")

## Subsitute NA's with a trimmed mean
na.omit(Xts, FUN = function(x, na.rm) mean(x, trim = 0.10, na.rm = na.rm))

## interpolate NA's linearily (formerly interpNA())
na.omit(X, method = "ir", interp = "linear")
na.omit(X, method = "iz", interp = "linear")
na.omit(X, method = "ie", interp = "linear")
   
## take previous values in a column
na.omit(X, method = "ir", interp = "before")
na.omit(X, method = "iz", interp = "before")
na.omit(X, method = "ie", interp = "before")


## examples with X (which is a matrix, not "timeSeries")
## (these examples are not run automatically as these functions are
## deprecated.) 
if(FALSE){
## Remove Rows with NAs
removeNA(X)
   
## subsitute NA's by zeros or column means
substituteNA(X, type = "zeros")
substituteNA(X, type = "mean")
   
## interpolate NA's linearily
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