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("SbTs.txt",h=TRUE,dec = ",")
str(data)
## 'data.frame': 96 obs. of 2 variables:
## $ Period: Factor w/ 96 levels "2004-01-01","2004-02-01",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ SalesC: int 269607 542391 416533 124751 172263 264779 306335 428759 722836 1172508 ...
fix(data)
dates.df <- data.frame(data$Period)
str(dates.df)
## 'data.frame': 96 obs. of 1 variable:
## $ data.Period: Factor w/ 96 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.Period)
str(data.time)
## Date[1:96], format: "2004-01-01" "2004-02-01" "2004-03-01" "2004-04-01" "2004-05-01" ...
ts(data.time,frequency = 12,start = c(2004,1))
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 2004 12418 12449 12478 12509 12539 12570 12600 12631 12662 12692 12723 12753
## 2005 12784 12815 12843 12874 12904 12935 12965 12996 13027 13057 13088 13118
## 2006 13149 13180 13208 13239 13269 13300 13330 13361 13392 13422 13453 13483
## 2007 13514 13545 13573 13604 13634 13665 13695 13726 13757 13787 13818 13848
## 2008 13879 13910 13939 13970 14000 14031 14061 14092 14123 14153 14184 14214
## 2009 14245 14276 14304 14335 14365 14396 14426 14457 14488 14518 14549 14579
## 2010 14610 14641 14669 14700 14730 14761 14791 14822 14853 14883 14914 14944
## 2011 14975 15006 15034 15065 15095 15126 15156 15187 15218 15248 15279 15309
z <- ts(matrix(rnorm(300),100,2),
start = c(2004,1),
frequency = 12)
class(z)
## [1] "mts" "ts" "matrix"
plot(z,plot.type = "single",
lty=1:3)
forec.1 <- ts(data.time,start = c(2004,1), frequency = 12)
forec <- ts(cumsum(forec.1),frequency =12)
(acf(forec,main=""))#значимой переодичности нет!!!
##
## Autocorrelations of series 'forec', by lag
##
## 0.0000 0.0833 0.1667 0.2500 0.3333 0.4167 0.5000 0.5833 0.6667 0.7500 0.8333
## 1.000 0.969 0.937 0.906 0.875 0.844 0.813 0.782 0.751 0.720 0.690
## 0.9167 1.0000 1.0833 1.1667 1.2500 1.3333 1.4167 1.5000 1.5833
## 0.659 0.629 0.599 0.568 0.539 0.509 0.480 0.450 0.421
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))
## Warning in arima(forec, order = c(0, 0, 9)): possible convergence problem: optim
## gave code = 1
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))
#model012 <- arima(forec,order = c(0,0,12))
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))
AIC(model01,model02,model03,model04,model09,model010,model013)
## df AIC
## model01 3 2622.094
## model02 4 2502.007
## model03 5 2385.012
## model04 6 2272.307
## model09 11 1913.240
## model010 12 1897.241
## model013 15 1856.083
model013 <- arima(forec,order = c(0,0,13))
## Warning in arima(forec, order = c(0, 0, 13)): possible convergence problem:
## optim gave code = 1
#model113 <- arima(forec,order = c(1,0,13))
#model213 <- arima(forec,order = c(2,0,13))
#model313 <- arima(forec,order = c(3,0,13))
#model413 <- arima(forec,order = c(4,0,13))
AIC(model013)
## [1] 1856.083
model0130 <- arima(forec,order = c(0,0,13))
## Warning in arima(forec, order = c(0, 0, 13)): possible convergence problem:
## optim gave code = 1
model0131 <- arima(forec,order = c(0,1,13))
## Warning in arima(forec, order = c(0, 1, 13)): possible convergence problem:
## optim gave code = 1
model0132 <- arima(forec,order = c(0,2,13))
model0133 <- arima(forec,order = c(0,3,13))
model0134 <- arima(forec,order = c(0,4,13))
model0135 <- arima(forec,order = c(0,5,13))
## Warning in arima(forec, order = c(0, 5, 13)): possible convergence problem:
## optim gave code = 1
AIC(model0130,model0131,model0132,model0133,model0134,model0135)
## Warning in AIC.default(model0130, model0131, model0132, model0133, model0134, :
## models are not all fitted to the same number of observations
## df AIC
## model0130 15 1856.0826
## model0131 14 1243.8085
## model0132 14 361.2172
## model0133 14 149.5830
## model0134 14 588.7672
## model0135 14 646.3974
plot(forec,xlim=c(0,20),ylim=c(0,1700000),type="s",col="blue")
lines(predict(model0133,n.ahead = 50,se.fit = TRUE)$pred,col="green")
lines(predict(model0133,n.ahead = 50,se.fit = TRUE)$se + predict(model0133,n.ahead = 50,se.fit = TRUE)$pred,col="red")
lines(-predict(model0133,n.ahead = 50,se.fit = TRUE)$se + predict(model0133,n.ahead = 50,se.fit = TRUE)$pred,col="red")
round(predict(model0133,n.ahead = 50,se.fit = TRUE)$se + predict(model0133,n.ahead = 50,se.fit = TRUE)$pred)
## Jan Feb Mar Apr May Jun Jul Aug Sep
## 9 1346232 1361603 1377004 1392436 1407897 1423389 1438912 1454465 1470049
## 10 1532688 1548425 1564191 1579988 1595816 1611674 1627563 1643482 1659432
## 11 1723535 1739637 1755770 1771933 1788127 1804351 1820605 1836890 1853205
## 12 1918772 1935240 1951738 1968267 1984826 2001416 2018036 2034687 2051368
## 13 2118397 2135231
## Oct Nov Dec
## 9 1485663 1501308 1516983
## 10 1675412 1691423 1707464
## 11 1869551 1885928 1902335
## 12 2068080 2084822 2101594
## 13
round(-predict(model0133,n.ahead = 50,se.fit = TRUE)$se + predict(model0133,n.ahead = 50,se.fit = TRUE)$pred)
## Jan Feb Mar Apr May Jun Jul Aug Sep
## 9 1346231 1361602 1377002 1392432 1407893 1423384 1438905 1454457 1470040
## 10 1532675 1548410 1564175 1579970 1595796 1611652 1627539 1643455 1659403
## 11 1723496 1739595 1755725 1771885 1788075 1804296 1820547 1836828 1853140
## 12 1918692 1935156 1951651 1968175 1984731 2001316 2017932 2034578 2051255
## 13 2118266 2135095
## Oct Nov Dec
## 9 1485653 1501296 1516970
## 10 1675380 1691388 1707427
## 11 1869483 1885856 1902259
## 12 2067962 2084700 2101468
## 13
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.