stinepack (version 1.5)

na.stinterp: Replace NAs by Stineman interpolation

Description

Generic functions for replacing each NA with Stineman interpolated values.

Usage

na.stinterp(object, ...) 
# S3 method for default
na.stinterp(object, along = time(object), na.rm = TRUE, ...)

Value

An object in which each NA in the input object is replaced by interpolating the non-NA values before and after it. Leading and trailing NAs are omitted (if na.rm = TRUE) or not replaced (if na.rm = FALSE).

Arguments

object

object in which NAs are to be replaced.

along

variable to use for interpolation. Has to be numeric, is otherwise coerced to numeric.

na.rm

logical. Should leading and trailing NAs be removed?

...

further arguments passed to methods.

Author

Gabor Grothendieck

Details

Missing values (NAs) are replaced by piecewice rational interpolation via stinterp.

By default the time index associated with object is used for interpolation. Note, that if this calls time.default this gives an equidistant spacing 1:NROW(object). If object is a matrix or data.frame, the interpolation is done separately for each column.

See Also

stinterp.

Examples

Run this code
na.stinterp(c(2,NA,1,4,5,2))
na.stinterp(ts(c(2,NA,1,4,5,2)))
if (FALSE) {

#comparison of gap filling with na.stinterp/stinterp and splines,
#the solid lines show the continuous interpolation functions 
#implicitly assumed by the gap-filling, they show how the Stineman
#interpolation restricts the range of the interpolant to the 
#nearby range of the points and suppresses the well known oscillations 
#characteristic of splines and other methods based on polynomials
x <- 1:6
y <- c(2,NA,1,4,5,2)
plot(x,y,ylim=c(-1,5))
points(na.stinterp(c(2,NA,1,4,5,2)),pch=2,col=2)
points(spline(x,y,n=6),pch=3,col=3)
lines(stinterp(x[!is.na(y)],y[!is.na(y)],xout=seq(1,6,by=0.1)),col=2)
lines(spline(x,y,n=50),col=3)

library(zoo)
na.stinterp(zoo(c(2,NA,1,4,5,2)))

library(its)
na.stinterp(its(c(2,NA,1,4,5,2), seq(Sys.time(), length = 6, by = "day")))

}

Run the code above in your browser using DataCamp Workspace