问题
I would like to smooth out a time series to avoid spurious jitter/error. In other words I want to do some very local robust smoothing.
I came across rollmean and rollmedian in the zoo package but ran into a problem because my vector had a NA in it. I then read somewhere that those zoo functions use runmed and therein lies the problem.
==examples==
median(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10),na.rm = TRUE)
runmed(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10),k=3)
The first line returns 2, but would have returned NA if na.rm = TRUE
was not included. The second line returns Error in runmed(c(1, 1, 1, 2, 2, 2, 7, NA, 1, 2, 3, 10, 10, 10), k = 3) :
NA/NaN/Inf in foreign function call (arg 1)
. There is no way to add a na.rm argument to the line.
How can I get runmed to handle the NA? By the way, rollmean returns a vector which is correct up to the NA and then returns NA for every value thereafter.
回答1:
Use na.omit
runmed(na.omit(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10)),k=3)
# [1] 1 1 1 2 2 2 2 2 2 3 10 10 10
#attr(,"k")
#[1] 3
Or use one of the na.*
functions from zoo (na.locf
, na.approx
, na.spline
, na.aggregate
, etc)
e.g.
runmed(na.locf(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10)),k=3)
#[1] 1 1 1 2 2 2 7 7 2 2 3 10 10 10
#attr(,"k")
#[1] 3
回答2:
See runquantile
from the caTools package.
来源:https://stackoverflow.com/questions/12150697/missing-value-when-calculating-running-medians