Change NAs to interpolated flat bars

强颜欢笑 提交于 2019-12-30 11:00:10

问题


If I have some OHLC data, with some NA rows, is there already a function in one of the R packages that will interpolate data?

na.locf has two cons:

  1. It is flat, then a sudden jump
  2. Used naively, it will repeat the whole of the last bar, with its high/lows

UPDATE: na.approx is superior to na.locf in the first respect, thanks Dirk, but I'm still searching for an existing function that is bar-aware. I.e. that will interpolate from the close of previous bar to the open of the next non-NA bar, and will create flat bars. Bonus points if it sets volume to zero! (So, I suspect the answer to my question is going to be "No", and I'll roll-my-own... but I'll wait a bit longer.)

ASIDE: na.approx vs. na.spline

Executive summary: na.spline is imaginative, use with care!

Here is some FX data (close prices only) with the original data in blue, and the na.approx joins shown in green:

Then here is the same data, but using na.spline:

Of particular concern is that na.spline has decided to create a new high!


回答1:


Besides na.locf(), there are already several other methods in package zoo:

  • na.aggregate()
  • na.approx()
  • na.fill()
  • na.spline()
  • na.trim()

and na.approx() or na.spline() offer you interpolation.




回答2:


The mice package (http://cran.r-project.org/web/packages/mice/index.html) may helps you.



来源:https://stackoverflow.com/questions/11897169/change-nas-to-interpolated-flat-bars

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