Learn R Programming

wq (version 0.2-9)

interpTs: Interpolate missing time series values

Description

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

Usage

interpTs(x, gap = 1, type = c("linear", "median", "mean"))

Arguments

Value

The time series with some or all missing values interpolated.

Rdversion

1.1

Details

When type = "linear", performs linear interpolation of any NA runs of length smaller than or equal to gap. 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 = "median" or "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.

Intended for time series but 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, gap = 3, type = "median")
plot(x2, col = 'red')
lines(chl27, col = 'blue')

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

Run the code above in your browser using DataLab