stats-na.omit: Handling Missing Time Series Values

Description Usage Arguments Details Note References Examples

Description

Functions for handling missing values in 'timeSeries' objects

Usage

1
2
3
4
5
6
7
## S4 method for signature '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 mean, type="median" replaces the missing values by the the column median.

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 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 the series, [interpNA] -
Specifies the method how to interpolate the matrix column by column. One of the applied vector strings: method="linear", method="before" or method="after".
For the interpolation the function approx is used.

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

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

The functions are listed by topic.

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.

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.

Note

The functions removeNA, substituteNA and interpNA are older implementations. Please use in all cases if possible the new function na.omit.

When dealing with daily data sets, there exists another function alignDaily Series which can handle missing data in un-aligned calendarical 'timeSeries' objects.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## Create a Matrix -
   X <- matrix(rnorm(100), ncol = 5)
   
## Replace a Single NA Inside - 
   X[3, 5] <- NA
   
## Replace Three in a Row Inside - 
   X[17, 2:4] <- c(NA, NA, NA)
   
## Replace Three in a Column Inside - 
   X[13:15, 4] <- c(NA, NA, NA)
   
## Replace Two at the Right Border - 
   X[11:12, 5] <- c(NA, NA)
   
## Replace One in the Lower Left Corner - 
   X[20, 1] <- NA
   print(X)
     
## 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

Example output

Loading required package: timeDate
             [,1]       [,2]        [,3]        [,4]       [,5]
 [1,] -0.08281463  3.0061860 -1.49243105 -1.66642903 -0.4311249
 [2,] -0.36975638 -1.7170384 -0.25150416  0.70895667 -0.6355284
 [3,]  1.03449724 -1.6271862  0.84518992  0.95156201         NA
 [4,] -0.78006647 -1.4764780  2.01733593 -0.28273689  0.7739710
 [5,] -1.09465649  0.6053823  0.13673551  0.33669582  1.1374852
 [6,]  1.96905967 -0.6612357  1.16040679 -1.55890909  1.0960261
 [7,]  0.13174462 -0.6723604  1.12406652 -0.15638389 -1.2737445
 [8,]  0.88843768 -1.5212735  0.84131839 -1.50879052  0.8279752
 [9,] -0.08214415  1.7183508 -0.13420328 -0.03360423 -0.8140881
[10,]  1.84940060  2.3304593  0.05434074  0.60488265  0.4291257
[11,]  0.44841479  0.9079702 -1.14767418  0.84885330         NA
[12,] -1.48528884  1.1698313  0.28013872  0.21053446         NA
[13,] -1.76959994  1.2567912 -0.99376700          NA  0.1211308
[14,]  0.86761846  1.3527765 -0.31855117          NA -0.7013266
[15,] -0.39933517  0.2203208 -0.06514871          NA -1.0700343
[16,] -0.94399145 -0.4952042  0.92399812  2.18504629 -2.3107524
[17,] -0.79846041         NA          NA          NA  1.5693047
[18,] -1.05278077  1.9160549 -0.11694461  0.78300008  0.8000871
[19,]  0.15548509  1.0764387  1.01104979  0.10346644 -0.4314200
[20,]          NA -0.1161461  0.98219513  0.95331641 -0.6852440
             [,1]       [,2]        [,3]        [,4]       [,5]
 [1,] -0.08281463  3.0061860 -1.49243105 -1.66642903 -0.4311249
 [2,] -0.36975638 -1.7170384 -0.25150416  0.70895667 -0.6355284
 [3,] -0.78006647 -1.4764780  2.01733593 -0.28273689  0.7739710
 [4,] -1.09465649  0.6053823  0.13673551  0.33669582  1.1374852
 [5,]  1.96905967 -0.6612357  1.16040679 -1.55890909  1.0960261
 [6,]  0.13174462 -0.6723604  1.12406652 -0.15638389 -1.2737445
 [7,]  0.88843768 -1.5212735  0.84131839 -1.50879052  0.8279752
 [8,] -0.08214415  1.7183508 -0.13420328 -0.03360423 -0.8140881
 [9,]  1.84940060  2.3304593  0.05434074  0.60488265  0.4291257
[10,] -0.94399145 -0.4952042  0.92399812  2.18504629 -2.3107524
[11,] -1.05278077  1.9160549 -0.11694461  0.78300008  0.8000871
[12,]  0.15548509  1.0764387  1.01104979  0.10346644 -0.4314200
attr(,"na.action")
[1] 20 17 13 14 15  3 11 12
attr(,"class")
[1] "omit"
             [,1]       [,2]        [,3]        [,4]       [,5]
 [1,] -0.08281463  3.0061860 -1.49243105 -1.66642903 -0.4311249
 [2,] -0.36975638 -1.7170384 -0.25150416  0.70895667 -0.6355284
 [3,]  1.03449724 -1.6271862  0.84518992  0.95156201  0.0000000
 [4,] -0.78006647 -1.4764780  2.01733593 -0.28273689  0.7739710
 [5,] -1.09465649  0.6053823  0.13673551  0.33669582  1.1374852
 [6,]  1.96905967 -0.6612357  1.16040679 -1.55890909  1.0960261
 [7,]  0.13174462 -0.6723604  1.12406652 -0.15638389 -1.2737445
 [8,]  0.88843768 -1.5212735  0.84131839 -1.50879052  0.8279752
 [9,] -0.08214415  1.7183508 -0.13420328 -0.03360423 -0.8140881
[10,]  1.84940060  2.3304593  0.05434074  0.60488265  0.4291257
[11,]  0.44841479  0.9079702 -1.14767418  0.84885330  0.0000000
[12,] -1.48528884  1.1698313  0.28013872  0.21053446  0.0000000
[13,] -1.76959994  1.2567912 -0.99376700  0.00000000  0.1211308
[14,]  0.86761846  1.3527765 -0.31855117  0.00000000 -0.7013266
[15,] -0.39933517  0.2203208 -0.06514871  0.00000000 -1.0700343
[16,] -0.94399145 -0.4952042  0.92399812  2.18504629 -2.3107524
[17,] -0.79846041  0.0000000  0.00000000  0.00000000  1.5693047
[18,] -1.05278077  1.9160549 -0.11694461  0.78300008  0.8000871
[19,]  0.15548509  1.0764387  1.01104979  0.10346644 -0.4314200
[20,]  0.00000000 -0.1161461  0.98219513  0.95331641 -0.6852440
             [,1]       [,2]        [,3]        [,4]        [,5]
 [1,] -0.08281463  3.0061860 -1.49243105 -1.66642903 -0.43112486
 [2,] -0.36975638 -1.7170384 -0.25150416  0.70895667 -0.63552841
 [3,]  1.03449724 -1.6271862  0.84518992  0.95156201 -0.09400925
 [4,] -0.78006647 -1.4764780  2.01733593 -0.28273689  0.77397096
 [5,] -1.09465649  0.6053823  0.13673551  0.33669582  1.13748520
 [6,]  1.96905967 -0.6612357  1.16040679 -1.55890909  1.09602613
 [7,]  0.13174462 -0.6723604  1.12406652 -0.15638389 -1.27374451
 [8,]  0.88843768 -1.5212735  0.84131839 -1.50879052  0.82797522
 [9,] -0.08214415  1.7183508 -0.13420328 -0.03360423 -0.81408805
[10,]  1.84940060  2.3304593  0.05434074  0.60488265  0.42912570
[11,]  0.44841479  0.9079702 -1.14767418  0.84885330 -0.09400925
[12,] -1.48528884  1.1698313  0.28013872  0.21053446 -0.09400925
[13,] -1.76959994  1.2567912 -0.99376700  0.15496628  0.12113083
[14,]  0.86761846  1.3527765 -0.31855117  0.15496628 -0.70132661
[15,] -0.39933517  0.2203208 -0.06514871  0.15496628 -1.07003434
[16,] -0.94399145 -0.4952042  0.92399812  2.18504629 -2.31075240
[17,] -0.79846041  0.3828231  0.25560797  0.15496628  1.56930473
[18,] -1.05278077  1.9160549 -0.11694461  0.78300008  0.80008712
[19,]  0.15548509  1.0764387  1.01104979  0.10346644 -0.43142001
[20,] -0.07969666 -0.1161461  0.98219513  0.95331641 -0.68524404
             [,1]       [,2]        [,3]        [,4]        [,5]
 [1,] -0.08281463  3.0061860 -1.49243105 -1.66642903 -0.43112486
 [2,] -0.36975638 -1.7170384 -0.25150416  0.70895667 -0.63552841
 [3,]  1.03449724 -1.6271862  0.84518992  0.95156201  0.06922127
 [4,] -0.78006647 -1.4764780  2.01733593 -0.28273689  0.77397096
 [5,] -1.09465649  0.6053823  0.13673551  0.33669582  1.13748520
 [6,]  1.96905967 -0.6612357  1.16040679 -1.55890909  1.09602613
 [7,]  0.13174462 -0.6723604  1.12406652 -0.15638389 -1.27374451
 [8,]  0.88843768 -1.5212735  0.84131839 -1.50879052  0.82797522
 [9,] -0.08214415  1.7183508 -0.13420328 -0.03360423 -0.81408805
[10,]  1.84940060  2.3304593  0.05434074  0.60488265  0.42912570
[11,]  0.44841479  0.9079702 -1.14767418  0.84885330  0.32646075
[12,] -1.48528884  1.1698313  0.28013872  0.21053446  0.22379579
[13,] -1.76959994  1.2567912 -0.99376700  0.70416242  0.12113083
[14,]  0.86761846  1.3527765 -0.31855117  1.19779038 -0.70132661
[15,] -0.39933517  0.2203208 -0.06514871  1.69141833 -1.07003434
[16,] -0.94399145 -0.4952042  0.92399812  2.18504629 -2.31075240
[17,] -0.79846041  0.7104253  0.40352676  1.48402319  1.56930473
[18,] -1.05278077  1.9160549 -0.11694461  0.78300008  0.80008712
[19,]  0.15548509  1.0764387  1.01104979  0.10346644 -0.43142001
[20,]          NA -0.1161461  0.98219513  0.95331641 -0.68524404
             [,1]       [,2]        [,3]        [,4]       [,5]
 [1,] -0.08281463  3.0061860 -1.49243105 -1.66642903 -0.4311249
 [2,] -0.36975638 -1.7170384 -0.25150416  0.70895667 -0.6355284
 [3,]  1.03449724 -1.6271862  0.84518992  0.95156201 -0.6355284
 [4,] -0.78006647 -1.4764780  2.01733593 -0.28273689  0.7739710
 [5,] -1.09465649  0.6053823  0.13673551  0.33669582  1.1374852
 [6,]  1.96905967 -0.6612357  1.16040679 -1.55890909  1.0960261
 [7,]  0.13174462 -0.6723604  1.12406652 -0.15638389 -1.2737445
 [8,]  0.88843768 -1.5212735  0.84131839 -1.50879052  0.8279752
 [9,] -0.08214415  1.7183508 -0.13420328 -0.03360423 -0.8140881
[10,]  1.84940060  2.3304593  0.05434074  0.60488265  0.4291257
[11,]  0.44841479  0.9079702 -1.14767418  0.84885330  0.4291257
[12,] -1.48528884  1.1698313  0.28013872  0.21053446  0.4291257
[13,] -1.76959994  1.2567912 -0.99376700  0.21053446  0.1211308
[14,]  0.86761846  1.3527765 -0.31855117  0.21053446 -0.7013266
[15,] -0.39933517  0.2203208 -0.06514871  0.21053446 -1.0700343
[16,] -0.94399145 -0.4952042  0.92399812  2.18504629 -2.3107524
[17,] -0.79846041 -0.4952042  0.92399812  2.18504629  1.5693047
[18,] -1.05278077  1.9160549 -0.11694461  0.78300008  0.8000871
[19,]  0.15548509  1.0764387  1.01104979  0.10346644 -0.4314200
[20,]          NA -0.1161461  0.98219513  0.95331641 -0.6852440

timeSeries documentation built on Nov. 17, 2017, 2:23 p.m.