missing value when calculating running medians?

你说的曾经没有我的故事 提交于 2019-12-04 03:54:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!