开发者

Plot density of periodic function

开发者 https://www.devze.com 2023-04-10 07:41 出处:网络
I\'ve开发者_开发百科 got a vector that represents the times (in seconds past midnight) that a bunch of events happened, and I want to plot the density of those events through the day.Here\'s one way t

I've开发者_开发百科 got a vector that represents the times (in seconds past midnight) that a bunch of events happened, and I want to plot the density of those events through the day. Here's one way to do that:

rs <- 60*60*24*c(rbeta(5000, 2, 5), runif(10000, 0, 1))
den <- density(rs, cut=0)
plot(den, ylim=range(0,den$y))

The problem with that is that it gets the endpoint density wrong, because this is a cyclical function. If you plot 3 periods in a row you see the true densities in the middle period:

den <- density(c(rs, rs+60*60*24, rs+2*60*60*24), cut=0)
plot(den, ylim=range(0,den$y))

My question is whether there's some [better] way to get the density of that middle chunk from the original data, without tripling the number of observations as I've done. I'd of course need to supply the length of the period, in case there aren't any observations near the endpoints.


I don't think your evidence that the curve should look line a portion of the repeated spline fit is compelling. You should examine the results of hist() on the same object where you specify breaks at hour boundaries. The logspline function allows calculation of density estimates with specified bounds on hte data:

hist( c(c(rs, rs+60*60*24, rs+2*60*60*24), breaks= 24*3 )
require(logspline)
?logspline
fit <- logspline(c(rs), lbound=0, ubound=60*60*24)
plot(fit)

A better fit because it correctly captures the fact that the end-of-the-day density is lower than the first of the day which isn't really correctly captured with that triple day densityplot.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号