"Breakpoints" 不显示日期而是连续测量

"Breakpoints" don't display dates but a continuous measure

我正在尝试检测我的系列中的一些结构性缺陷。问题是它显示连续的数字而不是日期,尽管我的系列是一个 ts 对象。

我找到了这个 但它对我不起作用。

这是我的数据集和代码:

df = structure(list(Date = structure(c(11690, 11725, 11753, 11781, 
11809, 11844, 11872, 11900, 11942, 11970, 11998, 12026, 12061, 
12089, 12117, 12145, 12180, 12208, 12243, 12265, 12299, 12327, 
12362, 12390, 12425, 12453, 12481, 12509, 12544, 12572, 12600, 
12631, 12663, 12698, 12726, 12754, 12796, 12817, 12845, 12880, 
12907, 12936, 12971, 12996, 13027, 13062, 13090, 13118, 13160, 
13181, 13209, 13244, 13272, 13307, 13335, 13363, 13392, 13426, 
13454, 13489, 13524, 13552, 13580, 13615, 13643, 13670, 13699, 
13726, 13762, 13790, 13825, 13853, 13888, 13916, 13944, 13979, 
14007, 14035, 14063, 14098, 14126, 14154, 14189, 14217, 14259, 
14280, 14308, 14336, 14371, 14399, 14427, 14462, 14490, 14525, 
14553, 14581, 14623, 14644, 14672, 14707, 14735, 14770, 14798, 
14826, 14854, 14889, 14917, 14945, 14987, 15008, 15036, 15071, 
15099, 15134, 15162, 15190, 15225, 15253, 15281, 15316, 15351, 
15379, 15407, 15434, 15463, 15497, 15526, 15554, 15589, 15617, 
15652, 15680, 15715, 15743, 15771, 15799, 15827, 15862, 15890, 
15918, 15953, 15980, 16016, 16044, 16079, 16107, 16135, 16163, 
16198, 16226, 16254, 16289, 16317, 16345, 16380, 16408, 16457, 
16467, 16499, 16540, 16556, 16589, 16632, 16648, 16681, 16730, 
16740, 16772, 16821, 16832, 16870, 16912, 16922, 16954, 17003, 
17014, 17052, 17094, 17106, 17143, 17185, 17198, 17234, 17283, 
17287, 17325, 17367, 17379, 17416, 17465, 17471, 17514, 17556, 
17563, 17598, 17647, 17652, 17696, 17738, 17744, 17787, 17829, 
17836, 17878, 17920, 17928, 17962, 17996, 18017, 18053, 18102, 
18109, 18151, 18193, 18201, 18242), class = "Date"), Fit = c(-2.01864866574525, 
-2.51081772611801, -3.07896216512166, -3.02724722640642, -0.764567739958455, 
-1.81459657078637, -2.13093106123547, -2.13093106123547, -1.91543051022373, 
-1.31418467170089, -1.86573850139921, -2.42539556395029, -1.26414303389104, 
-2.5433900359616, -1.99767537794132, -1.34728409808229, -1.64315561542246, 
-0.687106946387411, -2.48041219070826, -2.48041219070826, -1.78680159845671, 
-2.13687301896279, -2.6123923387608, -2.84563515334999, -3.41506073833104, 
-2.74565641471061, -2.3682788731863, -1.77410755661286, -2.46191758167165, 
-2.34829604543204, -2.37030627525843, -2.37030627525843, -1.75944822651175, 
-2.21875944722698, -2.60249841953241, -2.6758310533823, -1.99157259723667, 
-2.34860918772813, -3.24977356678388, -3.1998805120359, -3.64471855523435, 
-2.80762315792921, -1.46910836105049, -1.46910836105049, -2.24153954648439, 
-2.64718944648088, -2.61088260257325, -2.45889016663966, -2.59732356608009, 
-3.49037732690643, -2.75284369990193, -2.56284320115193, -3.01470163344929, 
-2.24267403694233, -3.36759206183078, -2.65899770326269, -2.65899770326269, 
-3.83487166356133, -2.30405890853423, -3.83487166356133, -2.91420930066836, 
-2.92649062542454, -2.45288174087111, -2.59203353843301, -2.37211828478634, 
-2.35485833573613, -3.28807932670479, -3.28807932670479, -2.69856893402308, 
-2.4482421908289, -3.42965769805337, -2.4002640291758, -1.72498017056001, 
-2.10246950134994, -2.75989530409431, -2.04609226712013, -1.50354129352453, 
-1.721866774994, -1.42652131446034, -1.99149928941641, -0.924508173463412, 
-0.34424720787331, -1.47956887747857, -0.699260660882747, -0.705970004477605, 
-2.89615299118885, -2.87168709242964, -3.49698896688496, -1.80133944039088, 
-2.3066390154612, -2.16578274820764, -1.62064416630292, -1.50034889686538, 
-1.64551702528081, -1.54888542275039, -2.36526073757675, -2.17980843362752, 
-2.61987658921009, -2.99580131757171, -3.27224528690084, -2.90968038360951, 
-2.43786428440244, -3.53447897261775, -2.94164730632451, -2.67914051197011, 
-3.08963971104142, -3.30489291781406, -3.16112222668117, -3.78875309229899, 
-3.27799815735179, -3.27546357519604, -3.28715323339141, -3.277230212033, 
-2.73537305926061, -2.63360778909794, -3.42285993586989, -3.02592822360864, 
-2.80491835054881, -3.1610709896381, -2.69912996631718, -2.48975331263934, 
-0.134524884114962, -2.3485759078928, -1.67019370390805, -1.30630530826772, 
-2.2627030307026, -1.19967822767006, -2.18902328617136, -2.32822018421121, 
-1.05335780233708, -2.32765305050142, -3.70136681094428, -2.47624061269887, 
-2.2395891355029, -0.873612387550348, -2.52750186765166, -1.58254587448088, 
-1.3519682697086, -2.67716755653968, -2.09120993997918, -2.83947106437091, 
-1.59227436938979, -2.70393468772428, -3.07475393381032, -1.72535933812472, 
-2.62864985613023, -2.1788856069182, -1.66072722296379, -2.02593106477748, 
-0.236862069023111, -2.20046381510765, 2.67747589830398, 2.03103654671807, 
-0.411843127888723, 0.15392859458, 3.15264600488878, -0.115883494946465, 
-0.115883494946465, -2.48408112888983, -2.13179786204659, -2.13179786204659, 
-0.421916196665926, -1.81454302259545, -1.81454302259545, -0.719344207794365, 
2.30623888786222, 2.30623888786222, 0.233349485130917, 0.807655736612547, 
0.807655736612547, 0.00810498434400109, 1.73561337499853, 1.73561337499853, 
2.05294933680988, 2.52332617911213, 2.52332617911213, 1.6590362509139, 
2.44897469036036, 2.44897469036036, 1.48162277916561, 0.109012820753664, 
0.109012820753664, -0.552382527186447, 0.342735574558364, 0.342735574558364, 
0.352860787128766, 0.352860787128766, 0.352860787128766, 0.352860787128766, 
0.726520452040748, 0.726520452040748, 0.176144461112964, 2.28171712015304, 
2.28171712015304, 0.256037205994603, 0.10686264754173, 0.10686264754173, 
0.10686264754173, 0.10686264754173, 0.10686264754173, -0.871047910469186, 
-1.61892724112359, -1.61892724112359, -2.04847571973674, -2.04847571973674, 
-2.04847571973674, 1.22730660297267, 1.94291846403141, 1.94291846403141, 
2.64766715573213, 1.66439852581802, 1.66439852581802, 3.92242045719081, 
2.92445832371034, 2.92445832371034, 4.09796304281725)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -216L))

word = ts(df$Fit, start = c(2002, 1), end = c(2019, 12), frequency = 12)

breaks = breakpoints(df$Fit ~ 1, h = 0.1)
plot(breaks)
coef(breaks, breaks = 2)

word[breaks$breakpoints] # July 2014


plot(df[,2], type = "b")
lines(fitted(breaks), col = 4, lwd = 2)
abline(v = df[breaks$breakpoints], lty = 2)

谁能帮我解决这个问题?

非常感谢!

如果我没记错的话,对于你使用动物园的不规则间隔,你可以试试下面的方法:

library(strucchange)
library(zoo)

breaks = breakpoints(df$Fit ~ 1, h = 0.1)
zoo_df = as.zoo(df[,2])
time(zoo_df) = df[,1]

plot(zoo_df, type = "b")
abline(v = time(zoo_df)[breaks$breakpoints], lty = 2)

这将为您提供 x 轴上带有日期的绘图。现在要获取 x 轴值,您需要执行 time(zoo_df) ,这是根据索引对其进行子集化的问题。对于拟合线,如果不想让线连在一起,需要拆分成断点+1组,每组分别绘制:

#group your breaks
grps = cut(1:length(zoo_df),
breaks=c(0,breaks$breakpoints,+Inf),
labels=1:(length(breaks$breakpoints)+1))

for(i in unique(grps)){
lines(time(zoo_df)[grps==i],fitted(breaks)[grps==i])
}

您创建了一个时间序列对象但没有使用它:

plot(word)
breaks <- breakpoints(word ~ 1, h = 0.1)
lines(fitted(breaks), col="blue", lwd=2)
abline(v = time(word)[breaks$breakpoints], lty = 2)