R

檢測時間序列的變化(R 示例)

  • February 27, 2015

我想檢測通常具有相同形狀的時間序列數據的變化。到目前為止,我已經使用了changepointR 包和cpt.mean(), cpt.var()andcpt.meanvar()函數。cpt.mean()當數據通常停留在一個級別時,使用 PELT 方法效果很好。但是我也想檢測下降過程中的變化。我想檢測的一個變化示例是黑色曲線突然下降而實際上應該遵循示例性紅色虛線的部分。我已經嘗試過 cpt.var() 函數,但是我無法獲得好的結果。你有什麼建議嗎(那些不一定要使用 R)?

變化曲線

這是更改的數據(作為 R 對象):

dat.change <- c(12.013995263488, 11.8460207231808, 11.2845153487846, 11.7884417180764, 
11.6865425802022, 11.4703118125303, 11.4677576899063, 11.0227199625084, 
11.274775836817, 11.03073498338, 10.7771805591742, 10.7383206158923, 
10.5847230134625, 10.2479315651441, 10.4196381241735, 10.467607842288, 
10.3682422713283, 9.7834431752935, 9.76649842404295, 9.78257968297228, 
9.87817694914062, 9.3449034905713, 9.56400153361727, 9.78120084558148, 
9.3445162813738, 9.36767436354887, 9.12070987223648, 9.21909859069157, 
8.85136359917466, 8.8814423003979, 8.61830163359642, 8.44796977628488, 
8.06957847272046, 8.37999165387824, 7.98213210294954, 8.21977468333673, 
7.683960439316, 7.73213584532496, 7.98956476021092, 7.83036046746187, 
7.64496198988985, 4.49693528397253, 6.3459274845112, 5.86993447552116, 
4.58301192892403, 5.63419551523625, 6.67847511602895, 7.2005344054883, 
5.54970477623895, 6.00011922569104, 6.882667104467, 4.74057284230894, 
6.2140437333397, 6.18511450451019, 5.83973575417525, 6.57271194428385, 
5.36261938326723, 5.48948831338016, 4.93968645996861, 4.52598133247377, 
4.56372558828803, 5.74515428123725, 5.45931581984165, 5.58701112949141, 
6.00585679276365, 5.41639695946931, 4.55361875158434, 6.23720558202826, 
6.19433060301002, 5.82989415940829, 5.69321394985076, 5.53585871082265, 
5.42684812413063, 5.80887522466946, 5.56660158483312, 5.7284521523444, 
5.25425775891636, 5.4227645808924, 5.34778016248718, 5.07084809927736, 
5.324066161355, 5.03526881241705, 5.17387528516352, 5.29864121433813, 
5.36894461582415, 5.07436929444317, 4.80619983525015, 4.42858947882894, 
4.33623051506001, 4.33481791951228, 4.38041031792294, 3.90012900415342, 
4.04262777674943, 4.34383842876647, 4.36984816425014, 4.11641092254315, 
3.83985887104645, 3.81813419810962, 3.85174630901311, 3.66434598962311, 
3.4281724860426, 2.99726515704766, 2.96694634792395, 2.94003031547181, 
3.20892607367132, 3.03980832743458, 2.85952185077593, 2.70595278908964, 
2.50931109659839, 2.1912274016859)

您可以使用時間序列異常值檢測來檢測時間序列的變化。 TsayChen 和 Liu 的程序是流行的時間序列異常值檢測方法。在這個網站上查看我之前的問題。

R 的tsoutlier包使用 Chen 和 Liu 的方法檢測異常值。SAS/SPSS/Autobox 也可以做到這一點。請參閱下面的 R 代碼以檢測時間序列的變化。

library("tsoutliers")
dat.ts<- ts(dat.change,frequency=1)
data.ts.outliers <- tso(dat.ts)
data.ts.outliers
plot(data.ts.outliers)

tsoultlier 包中的 tso 函數可識別以下異常值。您可以閱讀文檔以了解異常值的類型。

Outliers:
 type ind time coefhat   tstat
1   TC  42   42 -2.9462 -10.068
2   AO  43   43  1.0733   4.322
3   AO  45   45 -1.2113  -4.849
4   TC  47   47  1.0143   3.387
5   AO  51   51  0.9002   3.433
6   AO  52   52 -1.3455  -5.165
7   AO  56   56  0.9074   3.710
8   LS  62   62  1.1284   3.717
9   AO  67   67 -1.3503  -5.502

該軟件包還提供了不錯的情節。見下文。該圖顯示了異常值的位置以及如果沒有異常值會發生什麼。

在此處輸入圖像描述

我還使用了名為strucchange的 R 包來檢測電平變化。以您的數據為例

library("strucchange")
breakpoints(dat.ts~1)

程序正確識別斷點或結構變化。

Optimal 4-segment partition: 

Call:
breakpoints.formula(formula = dat.ts ~ 1)

Breakpoints at observation number:
17 41 87 

Corresponding to breakdates:
17 41 87 

希望這可以幫助

引用自:https://stats.stackexchange.com/questions/139660

comments powered by Disqus