Learn R Programming

wq (version 0.4.8)

interpTs: Interpolate or substitute missing time series values

Description

Imterpolates or substitutes missing data in a time series for gaps up to a specified size.

Usage

interpTs(x, type = c("linear", "series.median", "series.mean", "cycle.median", "cycle.mean"), gap = NULL)

Arguments

x
object of class "ts" or "mts"
type
method of interpolation or substitution
gap
maximum gap to be replaced

Value

The time series with some or all missing values replaced.

Details

When type = "linear", the function performs linear interpolation of any NA runs of length smaller than or equal to gap. When gap = NULL, gaps of any size will be replaced. Does not change leading or trailing NA runs. This interpolation approach is best for periods of low biological activity when sampling is routinely suspended.

When type = "series.median" or "series.mean", missing values are replaced by the overall median or mean, respectively. This may be desirable when missing values are not allowed but one wants, for example, to avoid spurious enhancement of trends.

When type = "cycle.median" or type = "cycle.mean", missing values are replaced by the median or mean, respectively, for the same cycle position (i.e., same month, quarter, etc., depending on the frequency). This may give more realistic series than using the overall mean or median.

Intended for time series but first three types will work with any vector or matrix. Matrices will be interpolated by column.

See Also

decompTs

Examples

Run this code
### Interpolate a vector time series and highlight the imputed data
chl27 <- sfbayChla[, 's27']
x1 <- interpTs(chl27, gap = 3)
plot(x1, col = 'red')
lines(chl27, col = 'blue')
x2 <- interpTs(chl27, type = "series.median", gap = 3)
plot(x2, col = 'red')
lines(chl27, col = 'blue')

### Interpolate a matrix time series and plot results
x3 <- interpTs(sfbayChla, type = "cycle.mean", gap = 1)
plot(x3[, 1:10], main = "SF Bay Chl-a\n(gaps of 1 month replaced)")

Run the code above in your browser using DataLab