This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(prophet)
library(lubridate)
library(ggplot2)
data <- read.table("TsSb.txt",h=TRUE,dec = ",")
str(data)
## 'data.frame': 89 obs. of 3 variables:
## $ Time: Factor w/ 89 levels "2004-01-01","2004-02-01",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ SalK: int 605 1984 1025 494 641 1716 1257 998 1180 1403 ...
## $ SalC: int 269607 542391 416533 124751 172263 264779 306335 428759 722836 1172508 ...
fix(data)
dates.df <- data.frame(data$Time)
str(dates.df)
## 'data.frame': 89 obs. of 1 variable:
## $ data.Time: Factor w/ 89 levels "2004-01-01","2004-02-01",..: 1 2 3 4 5 6 7 8 9 10 ...
#dates.time <-as.Date(dates.df$dteday,"%Y-%m-%d")#не знаю, как преобразовать date.df $ dteday в класс Date
data.time <- ymd(dates.df$data.Time)
str(data.time)
## Date[1:89], format: "2004-01-01" "2004-02-01" "2004-03-01" "2004-04-01" "2004-05-01" ...
dates.saleK <- data$SalK
dates.saleK
## [1] 605 1984 1025 494 641 1716 1257 998 1180 1403 1127 1687 1255 1092 1306
## [16] 2232 1131 1550 2005 1529 1824 2257 1731 2744 2260 2096 1841 2075 1472 1550
## [31] 1575 1553 2431 2323 1856 3493 2488 2488 3116 2246 1909 2918 2608 2079 3265
## [46] 2445 2622 3639 2355 2921 2735 2255 1393 2807 2692 2506 3229 3383 4222 7075
## [61] 1608 1872 2458 3040 3726 2898 4421 3200 2663 2960 2354 4125 4282 4333 3818
## [76] 3419 2900 6068 6178 4316 4851 4978 4181 6054 4356 4881 4431 5965 4176
dates.saleC <- data$SalC
dates.saleC
## [1] 269607 542391 416533 124751 172263 264779 306335 428759 722836
## [10] 1172508 1006760 1236768 554051 514427 534686 792693 386033 470864
## [19] 594361 698516 1087468 1587424 1574411 1791686 863407 924142 876372
## [28] 792964 491401 672489 633884 1097905 1994863 2226363 2197191 2376759
## [37] 1372341 1577593 1628783 1128700 869352 1622662 1489598 1752315 2861010
## [46] 2609937 3867529 3241078 1678588 2006365 2154445 1548438 1003634 1757179
## [55] 1849788 2004677 3248930 3482077 2885166 3161956 1168752 1316905 1250966
## [64] 1256732 1585652 1127210 1544773 1671149 1807886 2648822 2341570 3258579
## [73] 2345324 2244962 2311521 1903100 1585968 2520926 2454979 2407938 3453892
## [82] 4557630 3517299 5324057 2787904 3315663 2622287 3372816 2104893
time.serias <- data.frame(dates.df,dates.saleK)
time.serias
## data.Time dates.saleK
## 1 2004-01-01 605
## 2 2004-02-01 1984
## 3 2004-03-01 1025
## 4 2004-04-01 494
## 5 2004-05-01 641
## 6 2004-06-01 1716
## 7 2004-07-01 1257
## 8 2004-08-01 998
## 9 2004-09-01 1180
## 10 2004-10-01 1403
## 11 2004-11-01 1127
## 12 2004-12-01 1687
## 13 2005-01-01 1255
## 14 2005-02-01 1092
## 15 2005-03-01 1306
## 16 2005-04-01 2232
## 17 2005-05-01 1131
## 18 2005-06-01 1550
## 19 2005-07-01 2005
## 20 2005-08-01 1529
## 21 2005-09-01 1824
## 22 2005-10-01 2257
## 23 2005-11-01 1731
## 24 2005-12-01 2744
## 25 2006-01-01 2260
## 26 2006-02-01 2096
## 27 2006-03-01 1841
## 28 2006-04-01 2075
## 29 2006-05-01 1472
## 30 2006-06-01 1550
## 31 2006-07-01 1575
## 32 2006-08-01 1553
## 33 2006-09-01 2431
## 34 2006-10-01 2323
## 35 2006-11-01 1856
## 36 2006-12-01 3493
## 37 2007-01-01 2488
## 38 2007-02-01 2488
## 39 2007-03-01 3116
## 40 2007-04-01 2246
## 41 2007-05-01 1909
## 42 2007-06-01 2918
## 43 2007-07-01 2608
## 44 2007-08-01 2079
## 45 2007-09-01 3265
## 46 2007-10-01 2445
## 47 2007-11-01 2622
## 48 2007-12-01 3639
## 49 2008-01-01 2355
## 50 2008-02-01 2921
## 51 2008-03-01 2735
## 52 2008-04-01 2255
## 53 2008-05-01 1393
## 54 2008-06-01 2807
## 55 2008-07-01 2692
## 56 2008-08-01 2506
## 57 2008-09-01 3229
## 58 2008-10-01 3383
## 59 2008-11-01 4222
## 60 2008-12-01 7075
## 61 2009-01-01 1608
## 62 2009-02-01 1872
## 63 2009-03-01 2458
## 64 2009-04-01 3040
## 65 2009-05-01 3726
## 66 2009-06-01 2898
## 67 2009-07-01 4421
## 68 2009-08-01 3200
## 69 2009-09-01 2663
## 70 2009-10-01 2960
## 71 2009-11-01 2354
## 72 2009-12-01 4125
## 73 2010-01-01 4282
## 74 2010-02-01 4333
## 75 2010-03-01 3818
## 76 2010-04-01 3419
## 77 2010-05-01 2900
## 78 2010-06-01 6068
## 79 2010-07-01 6178
## 80 2010-08-01 4316
## 81 2010-09-01 4851
## 82 2010-10-01 4978
## 83 2010-11-01 4181
## 84 2010-12-01 6054
## 85 2011-01-01 4356
## 86 2011-02-01 4881
## 87 2011-03-01 4431
## 88 2011-04-01 5965
## 89 2011-05-01 4176
ts(time.serias,frequency = 4,start = c(2004,1))
## data.Time dates.saleK
## 2004 Q1 1 605
## 2004 Q2 2 1984
## 2004 Q3 3 1025
## 2004 Q4 4 494
## 2005 Q1 5 641
## 2005 Q2 6 1716
## 2005 Q3 7 1257
## 2005 Q4 8 998
## 2006 Q1 9 1180
## 2006 Q2 10 1403
## 2006 Q3 11 1127
## 2006 Q4 12 1687
## 2007 Q1 13 1255
## 2007 Q2 14 1092
## 2007 Q3 15 1306
## 2007 Q4 16 2232
## 2008 Q1 17 1131
## 2008 Q2 18 1550
## 2008 Q3 19 2005
## 2008 Q4 20 1529
## 2009 Q1 21 1824
## 2009 Q2 22 2257
## 2009 Q3 23 1731
## 2009 Q4 24 2744
## 2010 Q1 25 2260
## 2010 Q2 26 2096
## 2010 Q3 27 1841
## 2010 Q4 28 2075
## 2011 Q1 29 1472
## 2011 Q2 30 1550
## 2011 Q3 31 1575
## 2011 Q4 32 1553
## 2012 Q1 33 2431
## 2012 Q2 34 2323
## 2012 Q3 35 1856
## 2012 Q4 36 3493
## 2013 Q1 37 2488
## 2013 Q2 38 2488
## 2013 Q3 39 3116
## 2013 Q4 40 2246
## 2014 Q1 41 1909
## 2014 Q2 42 2918
## 2014 Q3 43 2608
## 2014 Q4 44 2079
## 2015 Q1 45 3265
## 2015 Q2 46 2445
## 2015 Q3 47 2622
## 2015 Q4 48 3639
## 2016 Q1 49 2355
## 2016 Q2 50 2921
## 2016 Q3 51 2735
## 2016 Q4 52 2255
## 2017 Q1 53 1393
## 2017 Q2 54 2807
## 2017 Q3 55 2692
## 2017 Q4 56 2506
## 2018 Q1 57 3229
## 2018 Q2 58 3383
## 2018 Q3 59 4222
## 2018 Q4 60 7075
## 2019 Q1 61 1608
## 2019 Q2 62 1872
## 2019 Q3 63 2458
## 2019 Q4 64 3040
## 2020 Q1 65 3726
## 2020 Q2 66 2898
## 2020 Q3 67 4421
## 2020 Q4 68 3200
## 2021 Q1 69 2663
## 2021 Q2 70 2960
## 2021 Q3 71 2354
## 2021 Q4 72 4125
## 2022 Q1 73 4282
## 2022 Q2 74 4333
## 2022 Q3 75 3818
## 2022 Q4 76 3419
## 2023 Q1 77 2900
## 2023 Q2 78 6068
## 2023 Q3 79 6178
## 2023 Q4 80 4316
## 2024 Q1 81 4851
## 2024 Q2 82 4978
## 2024 Q3 83 4181
## 2024 Q4 84 6054
## 2025 Q1 85 4356
## 2025 Q2 86 4881
## 2025 Q3 87 4431
## 2025 Q4 88 5965
## 2026 Q1 89 4176
z <- ts(matrix(rnorm(300),100,2),
start = c(2004,1),
frequency = 4)
class(z)
## [1] "mts" "ts" "matrix"
plot(z,plot.type = "single",
lty=1:3)
forec.1 <- ts(time.serias,start = c(2004,1), frequency = 4)
forec <- ts(cumsum(forec.1),frequency =4)
(acf(forec,main=""))
##
## Autocorrelations of series 'forec', by lag
##
## 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00
## 1.000 0.975 0.951 0.926 0.902 0.879 0.855 0.832 0.809 0.787 0.765 0.743 0.722
## 3.25 3.50 3.75 4.00 4.25 4.50 4.75 5.00 5.25 5.50
## 0.701 0.681 0.660 0.640 0.620 0.601 0.581 0.562 0.543 0.524
plot(stl(forec,s.window = "periodic"),main="Тренды и сезонные декомпозиции")
old.par <- par(mfrow=c(1,1))
plot(forec,type="b",log="y",xlab="",col="red")
plot(forec,type="s",ylim=c(1,1700000),log="y",col="blue")
#str(forec)
#fix(forec)
#summary(forec)
par(old.par)
model01 <- arima(forec,order = c(0,0,1))
model02 <- arima(forec,order = c(0,0,2))
model03 <- arima(forec,order = c(0,0,3))
model04 <- arima(forec,order = c(0,0,4))
model05 <- arima(forec,order = c(0,0,5))
model06 <- arima(forec,order = c(0,0,6))
model07 <- arima(forec,order = c(0,0,7))
model08 <- arima(forec,order = c(0,0,8))
model09 <- arima(forec,order = c(0,0,9))
model010 <- arima(forec,order = c(0,0,10))
## Warning in arima(forec, order = c(0, 0, 10)): possible convergence problem:
## optim gave code = 1
model011 <- arima(forec,order = c(0,0,11))
## Warning in arima(forec, order = c(0, 0, 11)): possible convergence problem:
## optim gave code = 1
model012 <- arima(forec,order = c(0,0,12))
## Warning in arima(forec, order = c(0, 0, 12)): possible convergence problem:
## optim gave code = 1
model013 <- arima(forec,order = c(0,0,13))
## Warning in arima(forec, order = c(0, 0, 13)): possible convergence problem:
## optim gave code = 1
model014 <- arima(forec,order = c(0,0,14))
## Warning in arima(forec, order = c(0, 0, 14)): possible convergence problem:
## optim gave code = 1
AIC(model01,model02,model03,model04,model05,model06,model07,model08,model09,model010,model011,model012,model013,model014)
## df AIC
## model01 3 4241.910
## model02 4 4013.325
## model03 5 3829.283
## model04 6 3656.600
## model05 7 3552.247
## model06 8 3435.066
## model07 9 3364.983
## model08 10 3288.464
## model09 11 3258.927
## model010 12 3191.134
## model011 13 3170.982
## model012 14 3151.350
## model013 15 3113.617
## model014 16 3086.790
model014 <- arima(forec,order = c(0,0,14))
## Warning in arima(forec, order = c(0, 0, 14)): possible convergence problem:
## optim gave code = 1
model114 <- arima(forec,order = c(1,0,14))
#model214 <- arima(forec,order = c(2,0,14))
#model314 <- arima(forec,order = c(3,0,14))
#model414 <- arima(forec,order = c(4,0,14))
AIC(model014,model114)
## df AIC
## model014 16 3086.790
## model114 17 2870.472
model0410 <- arima(forec,order = c(1,0,14))
model1141 <- arima(forec,order = c(1,1,14))
## Warning in arima(forec, order = c(1, 1, 14)): possible convergence problem:
## optim gave code = 1
model1142 <- arima(forec,order = c(1,2,14))
model1143 <- arima(forec,order = c(1,3,14))
model1144 <- arima(forec,order = c(1,4,14))
#model1145 <- arima(forec,order = c(1,5,14))
AIC(model0410,model1141,model1142,model1143,model1144)
## Warning in AIC.default(model0410, model1141, model1142, model1143, model1144):
## models are not all fitted to the same number of observations
## df AIC
## model0410 17 2870.472
## model1141 16 2797.308
## model1142 16 2774.336
## model1143 16 2762.362
## model1144 16 2775.465
plot(forec,xlim=c(0,60),ylim=c(0,500000),type="s",col="blue")
lines(predict(model1143,n.ahead = 30,se.fit = TRUE)$pred,col="green")
lines(predict(model1143,n.ahead = 30,se.fit = TRUE)$se + predict(model1143,n.ahead = 30,se.fit = TRUE)$pred,col="red")
lines(-predict(model1143,n.ahead = 30,se.fit = TRUE)$se + predict(model1143,n.ahead = 30,se.fit = TRUE)$pred,col="red")
round(predict(model1143,n.ahead = 30,se.fit = TRUE)$se + predict(model1143,n.ahead = 30,se.fit = TRUE)$pred)
## Qtr1 Qtr2 Qtr3 Qtr4
## 45 253196 259361
## 46 265408 271422 277906 283793
## 47 290399 296799 303470 309763
## 48 316494 323081 329798 336635
## 49 343583 350637 357793 365048
## 50 372399 379847 387390 395028
## 51 402761 410589 418512 426531
## 52 434645 442855 451161 459563
round(-predict(model1143,n.ahead = 30,se.fit = TRUE)$se + predict(model1143,n.ahead = 30,se.fit = TRUE)$pred)
## Qtr1 Qtr2 Qtr3 Qtr4
## 45 252047 257509
## 46 263014 268486 274459 279833
## 47 285849 291698 297762 303413
## 48 309431 315352 321273 327206
## 49 333162 339148 345170 351233
## 50 357338 363487 369682 375923
## 51 382211 388545 394926 401353
## 52 407827 414347 420913 427525
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.