另一种方法sm.density
是在比默认值更精细的网格上评估密度,并使用approx
或approxfun
给出所需的密度插值值Returns
。这是一个带有虚拟数据的示例:
set.seed(1)
foo <- data.frame(Date = seq(as.Date("2010-01-01"), as.Date("2010-12-31"),
by = "days"),
Returns = rnorm(365))
head(foo)
## compute the density, on fin grid (512*8 points)
dens <- with(foo, density(Returns, n = 512 * 8))
在这一点上,我们可以使用和对返回的密度approx()
进行插值,但我更喜欢which 做同样的事情,但返回一个函数,然后我们可以使用它来进行插值。首先,生成插值函数:x
y
approxfun()
## x and y are components of dens, see str(dens)
BAR <- with(dens, approxfun(x = x, y = y))
现在您可以使用BAR()
在您希望的任何点返回插值密度,例如第一个Returns
:
> with(foo, BAR(Returns[1]))
[1] 0.3268715
要完成该示例,请在 中添加每个数据的密度Returns
:
> foo <- within(foo, Density <- BAR(Returns))
> head(foo)
Date Returns Density
1 2010-01-01 -0.6264538 0.3268715
2 2010-01-02 0.1836433 0.3707068
3 2010-01-03 -0.8356286 0.2437966
4 2010-01-04 1.5952808 0.1228251
5 2010-01-05 0.3295078 0.3585224
6 2010-01-06 -0.8204684 0.2490127
要查看插值效果如何,我们可以绘制密度和插值版本并进行比较。注意我们必须进行排序Returns
,因为要达到我们想要的效果,lines
需要以升序查看数据:
plot(dens)
with(foo, lines(sort(Returns), BAR(sort(Returns)), col = "red"))
这给出了这样的东西:

只要在一组点(上例中为 512*8)上对密度进行了足够精细的评估,您就不会有任何问题,并且很难区分插值版本和真实版本之间的差异。如果您的值中有“间隙”,Returns
那么您可能会发现,正如lines()
您要求它绘制的点一样,直线段可能不会跟随间隙位置处的黑色密度。这只是间隙和lines()
工作原理的产物,而不是插值问题。