问题
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:
- It is flat, then a sudden jump
- 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