【发布时间】:2021-10-12 16:57:17
【问题描述】:
我有一条每日曲线 x,我试图逼近 x 的平均峰值和非峰值: https://ibb.co/Fq1Byzk
我已经定义了一个 delta 阈值,这样当 delta 低于阈值时,x 将处于非高峰期或高峰期。我想获得平均峰值,其中平均值仅是 delta
delta <- matrix(0,24,ncol=1)
for (i in 2:24){
# i-th element is the i-th hour per day
delta[i] = x[i,2]-x[i-1,2]
}
# Find hour at which max and min daily values occur
max_threshold = 0.15*max(delta)
min_threshold = 0.15*min(delta)
c <- abs(delta) < max_threshold
t1 <- which(delta>max_threshold)[1]-1 # t1: time index at end of off-peak
t2 <- which.max(delta) + 1 # t2 is time of initial peak
t3 <- which.min(delta)-2 # t3 is time of end peak
t4 <- which.min(delta) # t4 time index of evening off-peak
am <- mean(x[1:t1,2]) # average morning off-peak value
peak <- mean(x[t2:t3,2]) #average peak value
pm <- mean(x[t4:24,2]) # average evening off-peak value
> dput(x)
structure(list(time = structure(c(1451952000, 1451955600, 1451959200,
1451962800, 1451966400, 1451970000, 1451973600, 1451977200, 1451980800,
1451984400, 1451988000, 1451991600, 1451995200, 1451998800, 1452002400,
1452006000, 1452009600, 1452013200, 1452016800, 1452020400, 1452024000,
1452027600, 1452031200, 1452034800, 1452038400, 1452042000, 1452045600,
1452049200, 1452052800, 1452056400, 1452060000, 1452063600, 1452067200,
1452070800, 1452074400, 1452078000, 1452081600, 1452085200, 1452088800,
1452092400, 1452096000, 1452099600, 1452103200, 1452106800, 1452110400,
1452114000, 1452117600, 1452121200), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), Crow_education_Omer = c(0.019186330898848,
0.0192706664192825, 0.0182164724138513, 0.018174304653634, 0.019355001939717,
0.0197345117816722, 0.023951287803397, 0.0323848398468467, 0.0343245568168401,
0.0378244809148717, 0.0393003525224754, 0.0403545465279066, 0.0405232175687756,
0.0393425202826927, 0.0398907011655169, 0.0377401453944372, 0.0344932278577091,
0.0317101556833707, 0.0304872906370705, 0.0297282709531601, 0.0287584124681633,
0.0252584883701317, 0.0196080085010205, 0.0197345117816722, 0.0194815052203687,
0.0196080085010205, 0.0184273112149375, 0.0184694789751548, 0.0191441631386307,
0.019692344021455, 0.025469327171218, 0.0352522475416196, 0.0376136421137855,
0.0403967142881239, 0.0435592963044175, 0.0433484575033313, 0.0430532831818105,
0.042968947661376, 0.043306289743114, 0.044655658070066, 0.0424207667785518,
0.0416195793344241, 0.0382883262772615, 0.03769797763422, 0.0330173562501054,
0.0281680638251219, 0.0234452746807901, 0.0225597517162278)), row.names = 97:144, class = "data.frame")
另外,我如何能够在同一张图上同时绘制新的简化曲线和原始曲线 x?由于我的时间列是 POSIXCT,因此我似乎无法使用 x 减少数据点数量来融化或 rbind() 新曲线。
谢谢。
【问题讨论】:
-
您能否解释一下我想获得平均峰值,其中平均值仅是 x 内的值,其中 delta 稍微远一点?例如:您只想在
am中包含x[1:t1,2]的值,其delta < max_threshold?min_threshold呢? -
例如,我希望我的平均峰值仅包括从上午 10:00 到 14:00 的值,因为在 10:00 到 14:00 之间计算的增量小于我的 max_threshold 值 ~ 0.002。现在我的平均峰值包括从 t2 到 t3 的所有内容。我计算了 max_threshold 和 min_threshold,因为它们旨在确定 t2 和 t3 ......但我不确定如何做到这一点,所以我只是根据从最大和最小增量添加 1-2 小时来设置 t2 和 t3。理想情况下,t2 和 t3 将基于增量值超过最大和最小阈值的确切位置
-
您在
13:00的增量约为-0.00118。那是< max_threshold,也是< min_threshold。为什么要包括这个? (很抱歉提出愚蠢的问题,但我还没有得到算法......)。在14:00delta 是< max_thr和> min_thr。 -
你是绝对正确的。我已经修改了上面的 c 向量以显示 delta 的绝对值。基本上我想在 abs(delta) 的索引处平均 t2 和 t3 之间的 x