Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

na.stinterp

Replace NAs by Stineman interpolation


Description

Generic functions for replacing each NA with Stineman interpolated values.

Usage

na.stinterp(object, ...) 
## Default S3 method:
na.stinterp(object, along = time(object), na.rm = TRUE, ...)

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.

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.

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).

Author(s)

Gabor Grothendieck

See Also

Examples

na.stinterp(c(2,NA,1,4,5,2))
na.stinterp(ts(c(2,NA,1,4,5,2)))
## Not run: 

#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")))


## End(Not run)

stinepack

Stineman, a Consistently Well Behaved Method of Interpolation

v1.4
GPL-2
Authors
Tomas Johannesson & Halldor Bjornsson, Icelandic Met. Office; Gabor Grothendieck
Initial release
2018-07-27

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.