’19년 5월~7월 기간의 Btv pre-roll 광고 inven에 대한 Time Series(ETS 및 ARIMA) 예측 테스트 R code

if(!require(ggplot2)){install.packages("ggplot2", dependencies = TRUE); library(ggplot2)}
## Loading required package: ggplot2
if(!require(dplyr)){install.packages("dplyr", dependencies = TRUE); library(dplyr)}
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
if(!require(fpp2)){install.packages("fpp2", dependencies = TRUE); library(fpp2)}
## Loading required package: fpp2
## Loading required package: forecast
## Registered S3 method overwritten by 'xts':
##   method     from
##   as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
##   method             from    
##   fitted.fracdiff    fracdiff
##   residuals.fracdiff fracdiff
## Loading required package: fma
## Loading required package: expsmooth
###  ★★★★★★ 모든 변수명 지우기
rm(list =  ls())
gc()
##           used (Mb) gc trigger (Mb) max used (Mb)
## Ncells  795795 42.6    1508124 80.6  1210210 64.7
## Vcells 1356112 10.4    8388608 64.0  2180376 16.7
#getwd()
setwd("/home/mjs0428/inven_forcast")
btv_pre_roll <- read.csv("./Btv_Preroll-Req_Inv_Res_Imp-data_20190731.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE, fileEncoding = "euc-kr")
glimpse(btv_pre_roll)
## Observations: 577
## Variables: 7
## $ Date            <chr> "2018-01-01", "2018-01-02", "2018-01-03", "2018-…
## $ VOD_시청시간_초 <chr> "8,421,536,616", "7,878,348,464", "7,498,034,394", "7…
## $ VOD_시청건수    <chr> "4,496,473", "4,658,646", "4,535,338", "4,500,479", …
## $ REQ             <chr> "4,567,997", "4,630,415", "4,533,702", "4,490,70…
## $ INV             <chr> "7,377,096", "7,110,607", "6,932,244", "6,839,95…
## $ Res             <chr> "4,787,165", "4,391,051", "4,252,549", "4,101,29…
## $ Imp             <chr> "4,498,714", "4,141,349", "4,011,362", "3,872,03…
# 숫자형 데이터 변환
btv_pre_roll$REQ  <- as.numeric(gsub(",", "",  btv_pre_roll$REQ))
btv_pre_roll$INV <- as.numeric(gsub(",", "",  btv_pre_roll$INV))
btv_pre_roll$Res <- as.numeric(gsub(",", "",  btv_pre_roll$Res))
btv_pre_roll$Imp <- as.numeric(gsub(",", "",  btv_pre_roll$Imp))

# 단위 변경 : second -> hour
btv_pre_roll$VOD_시청시간  <- round(as.numeric(gsub(",", "", btv_pre_roll$VOD_시청시간))/3600,0)
# 변수명 바꾸기
btv_pre_roll$VOD_시청건수  <- as.numeric(gsub(",", "",  btv_pre_roll$VOD_시청건수))
# 날짜형 타입으로 변환
btv_pre_roll$Date  <- as.Date(btv_pre_roll$Date)

# 보정전 raw data 그래프
ggplot(btv_pre_roll, aes(x=Date, y=REQ)) +
  geom_point() + geom_smooth(method = 'lm', color = 'red', linetype =2) + # 직선 추세선 추가
  geom_smooth() + # 곡선 추세선 추가
  ggtitle("Raw Data-before correction")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

# 이상치 데이터 보정작업 
### >>> VOD 시청건수와 큰 차이 데이터 대체처리, 260만 미만 8개
btv_pre_roll$REQ_chg_flag = FALSE
arrange(btv_pre_roll %>% filter(REQ < 2650000), REQ)
##          Date VOD_시청시간_초 VOD_시청건수     REQ     INV     Res     Imp
## 1  2018-09-30   7,687,891,855      3896865  221664  297700  209140  554894
## 2  2018-07-02   6,390,874,049      3532602  736092 1189102  705927  709272
## 3  2018-10-01   6,320,324,590      3356570 2324932 2846820 2099119 1355917
## 4  2018-12-07   6,508,015,397      3567662 2466112 2872066 2032149 2852099
## 5  2018-02-16   5,214,340,882      2751726 2492996 4050293 3108664 2858587
## 6  2019-06-11   4,782,592,013      2600687 2577232 4380563 2897582 2809274
## 7  2018-12-06   6,210,719,990      3412828 2589182 2954937 2111337 2770486
## 8  2018-07-10   6,153,696,046      3476201 2590101 4002227 2311666 2309964
## 9  2019-05-31   4,928,624,460      2656316 2618025 4496329 2779988 2697892
## 10 2019-06-12   4,824,223,186      2657927 2628286 4491931 2957502 2867923
## 11 2019-05-30   5,029,899,714      2680261 2642520 4534099 2751991 2674374
##    VOD_시청시간 REQ_chg_flag
## 1       2135526        FALSE
## 2       1775243        FALSE
## 3       1755646        FALSE
## 4       1807782        FALSE
## 5       1448428        FALSE
## 6       1328498        FALSE
## 7       1725200        FALSE
## 8       1709360        FALSE
## 9       1369062        FALSE
## 10      1340062        FALSE
## 11      1397194        FALSE
btv_pre_roll$REQ_chg_flag [btv_pre_roll$REQ < 2600000] <- TRUE
#btv_pre_roll$REQ [REQ < 2600000] <- round(btv_pre_roll$VOD_시청건수 [REQ < 2600000] * 0.98, 0)
btv_pre_roll$REQ [btv_pre_roll$REQ < 2600000] <- btv_pre_roll$VOD_시청건수 [btv_pre_roll$REQ < 2600000]
filter(btv_pre_roll, REQ_chg_flag == TRUE)
##         Date VOD_시청시간_초 VOD_시청건수     REQ     INV     Res     Imp
## 1 2018-02-16   5,214,340,882      2751726 2751726 4050293 3108664 2858587
## 2 2018-07-02   6,390,874,049      3532602 3532602 1189102  705927  709272
## 3 2018-07-10   6,153,696,046      3476201 3476201 4002227 2311666 2309964
## 4 2018-09-30   7,687,891,855      3896865 3896865  297700  209140  554894
## 5 2018-10-01   6,320,324,590      3356570 3356570 2846820 2099119 1355917
## 6 2018-12-06   6,210,719,990      3412828 3412828 2954937 2111337 2770486
## 7 2018-12-07   6,508,015,397      3567662 3567662 2872066 2032149 2852099
## 8 2019-06-11   4,782,592,013      2600687 2600687 4380563 2897582 2809274
##   VOD_시청시간 REQ_chg_flag
## 1      1448428         TRUE
## 2      1775243         TRUE
## 3      1709360         TRUE
## 4      2135526         TRUE
## 5      1755646         TRUE
## 6      1725200         TRUE
## 7      1807782         TRUE
## 8      1328498         TRUE
###2018-05-15 ~ 2018-06-12까지 이상 데이터 대체 처리 >> 29개
btv_pre_roll$REQ_chg_flag [btv_pre_roll$Date >= '2018-05-15' & btv_pre_roll$Date <= '2018-06-12'] <- TRUE
btv_pre_roll$REQ [btv_pre_roll$Date >= '2018-05-15' & btv_pre_roll$Date <= '2018-06-12'] <- 
    btv_pre_roll$VOD_시청건수 [btv_pre_roll$Date >= '2018-05-15' & btv_pre_roll$Date <= '2018-06-12']

# 보정후 raw data 그래프
ggplot(btv_pre_roll, aes(x=Date, y=REQ)) +
  geom_point() + geom_smooth(method = 'lm', color = 'red', linetype =2) + # 직선 추세선 추가
  geom_smooth() + # 곡선 추세선 추가
  ggtitle("Raw Data afer correction")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

tail(btv_pre_roll)
##           Date VOD_시청시간_초 VOD_시청건수     REQ     INV     Res
## 572 2019-07-26                           NA 3295263 5626978 3311638
## 573 2019-07-27                           NA 3441295 5889893 3478406
## 574 2019-07-28                           NA 3590597 6246041 3630744
## 575 2019-07-29                           NA 3438216 5805305 3420024
## 576 2019-07-30                           NA 3449464 5834693 3412681
## 577 2019-07-31                           NA 3558777 6079928 3434790
##         Imp VOD_시청시간 REQ_chg_flag
## 572 3202774           NA        FALSE
## 573 3366122           NA        FALSE
## 574 3514548           NA        FALSE
## 575 3306799           NA        FALSE
## 576 3297728           NA        FALSE
## 577 3320717           NA        FALSE
# 요일  구하기
Sys.setlocale("LC_TIME", "English")
## Warning in Sys.setlocale("LC_TIME", "English"): OS reports request to set
## locale to "English" cannot be honored
## [1] ""
btv_pre_roll$weekday = factor(weekdays(btv_pre_roll$Date), 
                              levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
summary(btv_pre_roll$weekday)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##        83        83        83        82        82        82        82
Sys.setlocale("LC_TIME", "Korean")
## [1] "Korean"
# 월(month)  구하기
btv_pre_roll$month <-factor(substr(btv_pre_roll$Date,6,7),
                            levels = c('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'))
summary(btv_pre_roll$month)
## 01 02 03 04 05 06 07 08 09 10 11 12 
## 62 56 62 60 62 60 62 31 30 31 30 31
length(btv_pre_roll$month)
## [1] 577
# 특일 구하기
# 참고 페이지 : https://m.blog.naver.com/hancury/221057426711
# 사이트 : https://www.data.go.kr/
# 메뉴명 : 마이페이지 > OPEN API > 개발계정 상세보기
#인증키 "SD5uTucKQxefBuN79R8TKiBa15fAbw5j1id%2FdBH0SsWFDK4boXqDpgMPtc8QJ8UNyaSO8HpGJf%2FAbh65oBzg7g%3D%3D"
if(!require(glue)){install.packages("glue"); library(glue)}
## Loading required package: glue
## 
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
## 
##     collapse
if(!require(XML)){install.packages("XML"); library(XML)}
## Loading required package: XML
if(!require(stringr)){install.packages("stringr"); library(stringr)}
## Loading required package: stringr
api.key <- "SD5uTucKQxefBuN79R8TKiBa15fAbw5j1id%2FdBH0SsWFDK4boXqDpgMPtc8QJ8UNyaSO8HpGJf%2FAbh65oBzg7g%3D%3D"

url.format <- 
  'http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo?ServiceKey={key}&solYear={year}&solMonth={month}'

holiday.request <- function(key, year, month) glue(url.format)

df_holiday <- data.frame(dateName=NULL, Date=NULL)

for(m in 1:12){
  holiday_2018 <- xmlToList(holiday.request(api.key, 2018, str_pad(m, 2, pad=0)))
  holiday_2019 <- xmlToList(holiday.request(api.key, 2019, str_pad(m, 2, pad=0)))
  
  items_2018 <- holiday_2018$body$items
  items_2019 <- holiday_2019$body$items
  items_test <- holiday_2018$body$items
  items_test <- holiday_2019$body$items
  
  for(item_2018 in items_2018){
    if(item_2018$isHoliday == 'Y') {
      #print(paste(item_2018$dateName, item_2018$locdate, sep=' : '))
      df_holiday <- rbind(df_holiday, 
                          data.frame(dateName = item_2018$dateName, 
                                     Date = (paste(substr(item_2018$locdate,1,4),
                                                   substr(item_2018$locdate,5,6), 
                                                   substr(item_2018$locdate,7,8), sep = '-')),
                                     stringsAsFactors = FALSE))
    }
  }
  
  for(item_2019 in items_2019){
    if(item_2019$isHoliday == 'Y') {
      #print(paste(item_2019$dateName, item_2019$locdate, sep=' : '))
      df_holiday <- rbind(df_holiday, 
                          data.frame(dateName = item_2019$dateName, 
                                     Date = (paste(substr(item_2019$locdate,1,4),
                                                   substr(item_2019$locdate,5,6), 
                                                   substr(item_2019$locdate,7,8), sep = '-')),
                                     stringsAsFactors = FALSE))
    }
  }
  
}

# 날짜 데이터의 데이터 타입 변환
df_holiday$Date <- as.Date(df_holiday$Date)

### Left 조인하기 : 광고 데이터 + 특일 데이터..
btv_pre_roll <- left_join(btv_pre_roll, df_holiday)
## Joining, by = "Date"
# 특일 데이터 범주화
btv_pre_roll <- mutate(btv_pre_roll, isHolyday = as.numeric(!is.na(dateName)))

# 필요한 데이터 잘라내기
head(btv_pre_roll); tail(btv_pre_roll)
##         Date VOD_시청시간_초 VOD_시청건수     REQ     INV     Res     Imp
## 1 2018-01-01   8,421,536,616      4496473 4567997 7377096 4787165 4498714
## 2 2018-01-02   7,878,348,464      4658646 4630415 7110607 4391051 4141349
## 3 2018-01-03   7,498,034,394      4535338 4533702 6932244 4252549 4011362
## 4 2018-01-04   7,500,242,195      4500479 4490705 6839954 4101297 3872031
## 5 2018-01-05   7,458,641,277      4488078 4480584 6786827 4167156 3928040
## 6 2018-01-06   7,981,271,276      4558005 4583425 7065201 4469992 4208227
##   VOD_시청시간 REQ_chg_flag   weekday month dateName isHolyday
## 1      2339316        FALSE    Monday    01   1월1일         1
## 2      2188430        FALSE   Tuesday    01     <NA>         0
## 3      2082787        FALSE Wednesday    01     <NA>         0
## 4      2083401        FALSE  Thursday    01     <NA>         0
## 5      2071845        FALSE    Friday    01     <NA>         0
## 6      2217020        FALSE  Saturday    01     <NA>         0
##           Date VOD_시청시간_초 VOD_시청건수     REQ     INV     Res
## 572 2019-07-26                           NA 3295263 5626978 3311638
## 573 2019-07-27                           NA 3441295 5889893 3478406
## 574 2019-07-28                           NA 3590597 6246041 3630744
## 575 2019-07-29                           NA 3438216 5805305 3420024
## 576 2019-07-30                           NA 3449464 5834693 3412681
## 577 2019-07-31                           NA 3558777 6079928 3434790
##         Imp VOD_시청시간 REQ_chg_flag   weekday month dateName isHolyday
## 572 3202774           NA        FALSE    Friday    07     <NA>         0
## 573 3366122           NA        FALSE  Saturday    07     <NA>         0
## 574 3514548           NA        FALSE    Sunday    07     <NA>         0
## 575 3306799           NA        FALSE    Monday    07     <NA>         0
## 576 3297728           NA        FALSE   Tuesday    07     <NA>         0
## 577 3320717           NA        FALSE Wednesday    07     <NA>         0
btv_pre_roll2 <- filter(btv_pre_roll, Date <= '2019-04-30')
btv_pre_roll3 <- filter(btv_pre_roll, Date <= '2019-05-31')
btv_pre_roll4 <- filter(btv_pre_roll, Date <= '2019-06-30')

tail(btv_pre_roll2); tail(btv_pre_roll3); tail(btv_pre_roll4)
##           Date VOD_시청시간_초 VOD_시청건수     REQ     INV     Res
## 480 2019-04-25   5,742,016,958      3169507 3144534 5187095 3049096
## 481 2019-04-26   5,902,626,036      3223486 3217561 5361283 3311676
## 482 2019-04-27   6,873,743,765      3681518 3636698 6140815 3592538
## 483 2019-04-28   7,368,785,353      3760089 3714765 6366117 3668620
## 484 2019-04-29   6,127,155,357      3215605 3187055 5342248 3141680
## 485 2019-04-30   5,579,185,843      3036480 3024436 5049381 2996593
##         Imp VOD_시청시간 REQ_chg_flag  weekday month dateName isHolyday
## 480 2928027      1595005        FALSE Thursday    04     <NA>         0
## 481 3186781      1639618        FALSE   Friday    04     <NA>         0
## 482 3457596      1909373        FALSE Saturday    04     <NA>         0
## 483 3530846      2046885        FALSE   Sunday    04     <NA>         0
## 484 3032163      1701988        FALSE   Monday    04     <NA>         0
## 485 2896734      1549774        FALSE  Tuesday    04     <NA>         0
##           Date VOD_시청시간_초 VOD_시청건수     REQ     INV     Res
## 511 2019-05-26   6,984,546,895      3430338 3359779 6038953 3458243
## 512 2019-05-27   6,054,678,789      3051248 3006505 5251215 2884850
## 513 2019-05-28   5,196,644,876      2712230 2680322 4644723 2626915
## 514 2019-05-29   5,131,811,785      2723640 2682910 4669094 2730911
## 515 2019-05-30   5,029,899,714      2680261 2642520 4534099 2751991
## 516 2019-05-31   4,928,624,460      2656316 2618025 4496329 2779988
##         Imp VOD_시청시간 REQ_chg_flag   weekday month dateName isHolyday
## 511 3365408      1940152        FALSE    Sunday    05     <NA>         0
## 512 2811020      1681855        FALSE    Monday    05     <NA>         0
## 513 2556817      1443512        FALSE   Tuesday    05     <NA>         0
## 514 2655686      1425503        FALSE Wednesday    05     <NA>         0
## 515 2674374      1397194        FALSE  Thursday    05     <NA>         0
## 516 2697892      1369062        FALSE    Friday    05     <NA>         0
##           Date VOD_시청시간_초 VOD_시청건수     REQ     INV     Res
## 541 2019-06-25   5,154,136,394      2768698 2727756 4646931 3110832
## 542 2019-06-26   5,501,653,033      2958304 2879423 4942865 3221068
## 543 2019-06-27   5,347,072,193      2879408 2776976 4742231 3202517
## 544 2019-06-28   5,284,184,366      2886162 2780258 4714376 3151617
## 545 2019-06-29   6,958,043,262      3650343 3509160 6067504 3727858
## 546 2019-06-30   7,014,237,936      3536303 3393263 5933480 3645685
##         Imp VOD_시청시간 REQ_chg_flag   weekday month dateName isHolyday
## 541 3013639      1431705        FALSE   Tuesday    06     <NA>         0
## 542 3123573      1528237        FALSE Wednesday    06     <NA>         0
## 543 3105521      1485298        FALSE  Thursday    06     <NA>         0
## 544 3053953      1467829        FALSE    Friday    06     <NA>         0
## 545 3617139      1932790        FALSE  Saturday    06     <NA>         0
## 546 3539916      1948399        FALSE    Sunday    06     <NA>         0
# 예측 Dataset 생성
# 예측할 날짜로 된 수열 생성하기
s_date2 <- as.Date("2019-05-01")
e_date2 <- as.Date("2019-05-31")
s_date3 <- as.Date("2019-06-01")
e_date3 <- as.Date("2019-06-30")
s_date4 <- as.Date("2019-07-01")
e_date4 <- as.Date("2019-07-31")

add_df2 <- data.frame(Date = seq(from = s_date2, to=e_date2, by=1))
add_df2$Date <- as.Date(add_df2$Date); add_df2$REQ <- NA; add_df2$weekday <- NA; add_df2$month <- NA; add_df2$isHolyday <- NA; 
add_df3 <- data.frame(Date = seq(from = s_date3, to=e_date3, by=1))
add_df3$Date <- as.Date(add_df3$Date); add_df3$REQ <- NA; add_df3$weekday <- NA; add_df3$month <- NA; add_df3$isHolyday <- NA;

add_df4 <- data.frame(Date = seq(from = s_date4, to=e_date4, by=1))
add_df4$Date <- as.Date(add_df4$Date); add_df4$REQ <- NA; add_df4$weekday <- NA; add_df4$month <- NA; add_df4$isHolyday <- NA;


# 요일  구하기
Sys.setlocale("LC_TIME", "English")
## Warning in Sys.setlocale("LC_TIME", "English"): OS reports request to set
## locale to "English" cannot be honored
## [1] ""
add_df2$weekday = factor(weekdays(add_df2$Date), 
                         levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
summary(add_df2$weekday)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##         0         0         0         0         0         0         0 
##      NA's 
##        31
add_df3$weekday = factor(weekdays(add_df3$Date), 
                         levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
summary(add_df3$weekday)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##         0         0         0         0         0         0         0 
##      NA's 
##        30
add_df4$weekday = factor(weekdays(add_df4$Date), 
                         levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
summary(add_df4$weekday)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##         0         0         0         0         0         0         0 
##      NA's 
##        31
Sys.setlocale("LC_TIME", "Korean")
## [1] "Korean"
# 월(month)  구하기
add_df2$month <-factor(substr(add_df2$Date,6,7),
                       levels = c('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'))
summary(add_df2$month)
## 01 02 03 04 05 06 07 08 09 10 11 12 
##  0  0  0  0 31  0  0  0  0  0  0  0
add_df3$month <-factor(substr(add_df3$Date,6,7),
                       levels = c('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'))
summary(add_df3$month)
## 01 02 03 04 05 06 07 08 09 10 11 12 
##  0  0  0  0  0 30  0  0  0  0  0  0
add_df4$month <-factor(substr(add_df4$Date,6,7),
                       levels = c('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'))
summary(add_df4$month)
## 01 02 03 04 05 06 07 08 09 10 11 12 
##  0  0  0  0  0  0 31  0  0  0  0  0
# 특일 데이터 범주화 추가
add_df2$isHolyday <- as.numeric(!is.na(left_join(add_df2, df_holiday)$dateName))
## Joining, by = "Date"
add_df3$isHolyday <- as.numeric(!is.na(left_join(add_df3, df_holiday)$dateName))
## Joining, by = "Date"
add_df4$isHolyday <- as.numeric(!is.na(left_join(add_df4, df_holiday)$dateName))
## Joining, by = "Date"
### 시계열 적용 시작
ts2_train <- ts(btv_pre_roll2$REQ, start = 1, frequency = 7)
ts3_train <- ts(btv_pre_roll3$REQ, start = 1, frequency = 7)
time(ts2_train)
## Time Series:
## Start = c(1, 1) 
## End = c(70, 2) 
## Frequency = 7 
##   [1]  1.000000  1.142857  1.285714  1.428571  1.571429  1.714286  1.857143
##   [8]  2.000000  2.142857  2.285714  2.428571  2.571429  2.714286  2.857143
##  [15]  3.000000  3.142857  3.285714  3.428571  3.571429  3.714286  3.857143
##  [22]  4.000000  4.142857  4.285714  4.428571  4.571429  4.714286  4.857143
##  [29]  5.000000  5.142857  5.285714  5.428571  5.571429  5.714286  5.857143
##  [36]  6.000000  6.142857  6.285714  6.428571  6.571429  6.714286  6.857143
##  [43]  7.000000  7.142857  7.285714  7.428571  7.571429  7.714286  7.857143
##  [50]  8.000000  8.142857  8.285714  8.428571  8.571429  8.714286  8.857143
##  [57]  9.000000  9.142857  9.285714  9.428571  9.571429  9.714286  9.857143
##  [64] 10.000000 10.142857 10.285714 10.428571 10.571429 10.714286 10.857143
##  [71] 11.000000 11.142857 11.285714 11.428571 11.571429 11.714286 11.857143
##  [78] 12.000000 12.142857 12.285714 12.428571 12.571429 12.714286 12.857143
##  [85] 13.000000 13.142857 13.285714 13.428571 13.571429 13.714286 13.857143
##  [92] 14.000000 14.142857 14.285714 14.428571 14.571429 14.714286 14.857143
##  [99] 15.000000 15.142857 15.285714 15.428571 15.571429 15.714286 15.857143
## [106] 16.000000 16.142857 16.285714 16.428571 16.571429 16.714286 16.857143
## [113] 17.000000 17.142857 17.285714 17.428571 17.571429 17.714286 17.857143
## [120] 18.000000 18.142857 18.285714 18.428571 18.571429 18.714286 18.857143
## [127] 19.000000 19.142857 19.285714 19.428571 19.571429 19.714286 19.857143
## [134] 20.000000 20.142857 20.285714 20.428571 20.571429 20.714286 20.857143
## [141] 21.000000 21.142857 21.285714 21.428571 21.571429 21.714286 21.857143
## [148] 22.000000 22.142857 22.285714 22.428571 22.571429 22.714286 22.857143
## [155] 23.000000 23.142857 23.285714 23.428571 23.571429 23.714286 23.857143
## [162] 24.000000 24.142857 24.285714 24.428571 24.571429 24.714286 24.857143
## [169] 25.000000 25.142857 25.285714 25.428571 25.571429 25.714286 25.857143
## [176] 26.000000 26.142857 26.285714 26.428571 26.571429 26.714286 26.857143
## [183] 27.000000 27.142857 27.285714 27.428571 27.571429 27.714286 27.857143
## [190] 28.000000 28.142857 28.285714 28.428571 28.571429 28.714286 28.857143
## [197] 29.000000 29.142857 29.285714 29.428571 29.571429 29.714286 29.857143
## [204] 30.000000 30.142857 30.285714 30.428571 30.571429 30.714286 30.857143
## [211] 31.000000 31.142857 31.285714 31.428571 31.571429 31.714286 31.857143
## [218] 32.000000 32.142857 32.285714 32.428571 32.571429 32.714286 32.857143
## [225] 33.000000 33.142857 33.285714 33.428571 33.571429 33.714286 33.857143
## [232] 34.000000 34.142857 34.285714 34.428571 34.571429 34.714286 34.857143
## [239] 35.000000 35.142857 35.285714 35.428571 35.571429 35.714286 35.857143
## [246] 36.000000 36.142857 36.285714 36.428571 36.571429 36.714286 36.857143
## [253] 37.000000 37.142857 37.285714 37.428571 37.571429 37.714286 37.857143
## [260] 38.000000 38.142857 38.285714 38.428571 38.571429 38.714286 38.857143
## [267] 39.000000 39.142857 39.285714 39.428571 39.571429 39.714286 39.857143
## [274] 40.000000 40.142857 40.285714 40.428571 40.571429 40.714286 40.857143
## [281] 41.000000 41.142857 41.285714 41.428571 41.571429 41.714286 41.857143
## [288] 42.000000 42.142857 42.285714 42.428571 42.571429 42.714286 42.857143
## [295] 43.000000 43.142857 43.285714 43.428571 43.571429 43.714286 43.857143
## [302] 44.000000 44.142857 44.285714 44.428571 44.571429 44.714286 44.857143
## [309] 45.000000 45.142857 45.285714 45.428571 45.571429 45.714286 45.857143
## [316] 46.000000 46.142857 46.285714 46.428571 46.571429 46.714286 46.857143
## [323] 47.000000 47.142857 47.285714 47.428571 47.571429 47.714286 47.857143
## [330] 48.000000 48.142857 48.285714 48.428571 48.571429 48.714286 48.857143
## [337] 49.000000 49.142857 49.285714 49.428571 49.571429 49.714286 49.857143
## [344] 50.000000 50.142857 50.285714 50.428571 50.571429 50.714286 50.857143
## [351] 51.000000 51.142857 51.285714 51.428571 51.571429 51.714286 51.857143
## [358] 52.000000 52.142857 52.285714 52.428571 52.571429 52.714286 52.857143
## [365] 53.000000 53.142857 53.285714 53.428571 53.571429 53.714286 53.857143
## [372] 54.000000 54.142857 54.285714 54.428571 54.571429 54.714286 54.857143
## [379] 55.000000 55.142857 55.285714 55.428571 55.571429 55.714286 55.857143
## [386] 56.000000 56.142857 56.285714 56.428571 56.571429 56.714286 56.857143
## [393] 57.000000 57.142857 57.285714 57.428571 57.571429 57.714286 57.857143
## [400] 58.000000 58.142857 58.285714 58.428571 58.571429 58.714286 58.857143
## [407] 59.000000 59.142857 59.285714 59.428571 59.571429 59.714286 59.857143
## [414] 60.000000 60.142857 60.285714 60.428571 60.571429 60.714286 60.857143
## [421] 61.000000 61.142857 61.285714 61.428571 61.571429 61.714286 61.857143
## [428] 62.000000 62.142857 62.285714 62.428571 62.571429 62.714286 62.857143
## [435] 63.000000 63.142857 63.285714 63.428571 63.571429 63.714286 63.857143
## [442] 64.000000 64.142857 64.285714 64.428571 64.571429 64.714286 64.857143
## [449] 65.000000 65.142857 65.285714 65.428571 65.571429 65.714286 65.857143
## [456] 66.000000 66.142857 66.285714 66.428571 66.571429 66.714286 66.857143
## [463] 67.000000 67.142857 67.285714 67.428571 67.571429 67.714286 67.857143
## [470] 68.000000 68.142857 68.285714 68.428571 68.571429 68.714286 68.857143
## [477] 69.000000 69.142857 69.285714 69.428571 69.571429 69.714286 69.857143
## [484] 70.000000 70.142857
time(ts3_train)
## Time Series:
## Start = c(1, 1) 
## End = c(74, 5) 
## Frequency = 7 
##   [1]  1.000000  1.142857  1.285714  1.428571  1.571429  1.714286  1.857143
##   [8]  2.000000  2.142857  2.285714  2.428571  2.571429  2.714286  2.857143
##  [15]  3.000000  3.142857  3.285714  3.428571  3.571429  3.714286  3.857143
##  [22]  4.000000  4.142857  4.285714  4.428571  4.571429  4.714286  4.857143
##  [29]  5.000000  5.142857  5.285714  5.428571  5.571429  5.714286  5.857143
##  [36]  6.000000  6.142857  6.285714  6.428571  6.571429  6.714286  6.857143
##  [43]  7.000000  7.142857  7.285714  7.428571  7.571429  7.714286  7.857143
##  [50]  8.000000  8.142857  8.285714  8.428571  8.571429  8.714286  8.857143
##  [57]  9.000000  9.142857  9.285714  9.428571  9.571429  9.714286  9.857143
##  [64] 10.000000 10.142857 10.285714 10.428571 10.571429 10.714286 10.857143
##  [71] 11.000000 11.142857 11.285714 11.428571 11.571429 11.714286 11.857143
##  [78] 12.000000 12.142857 12.285714 12.428571 12.571429 12.714286 12.857143
##  [85] 13.000000 13.142857 13.285714 13.428571 13.571429 13.714286 13.857143
##  [92] 14.000000 14.142857 14.285714 14.428571 14.571429 14.714286 14.857143
##  [99] 15.000000 15.142857 15.285714 15.428571 15.571429 15.714286 15.857143
## [106] 16.000000 16.142857 16.285714 16.428571 16.571429 16.714286 16.857143
## [113] 17.000000 17.142857 17.285714 17.428571 17.571429 17.714286 17.857143
## [120] 18.000000 18.142857 18.285714 18.428571 18.571429 18.714286 18.857143
## [127] 19.000000 19.142857 19.285714 19.428571 19.571429 19.714286 19.857143
## [134] 20.000000 20.142857 20.285714 20.428571 20.571429 20.714286 20.857143
## [141] 21.000000 21.142857 21.285714 21.428571 21.571429 21.714286 21.857143
## [148] 22.000000 22.142857 22.285714 22.428571 22.571429 22.714286 22.857143
## [155] 23.000000 23.142857 23.285714 23.428571 23.571429 23.714286 23.857143
## [162] 24.000000 24.142857 24.285714 24.428571 24.571429 24.714286 24.857143
## [169] 25.000000 25.142857 25.285714 25.428571 25.571429 25.714286 25.857143
## [176] 26.000000 26.142857 26.285714 26.428571 26.571429 26.714286 26.857143
## [183] 27.000000 27.142857 27.285714 27.428571 27.571429 27.714286 27.857143
## [190] 28.000000 28.142857 28.285714 28.428571 28.571429 28.714286 28.857143
## [197] 29.000000 29.142857 29.285714 29.428571 29.571429 29.714286 29.857143
## [204] 30.000000 30.142857 30.285714 30.428571 30.571429 30.714286 30.857143
## [211] 31.000000 31.142857 31.285714 31.428571 31.571429 31.714286 31.857143
## [218] 32.000000 32.142857 32.285714 32.428571 32.571429 32.714286 32.857143
## [225] 33.000000 33.142857 33.285714 33.428571 33.571429 33.714286 33.857143
## [232] 34.000000 34.142857 34.285714 34.428571 34.571429 34.714286 34.857143
## [239] 35.000000 35.142857 35.285714 35.428571 35.571429 35.714286 35.857143
## [246] 36.000000 36.142857 36.285714 36.428571 36.571429 36.714286 36.857143
## [253] 37.000000 37.142857 37.285714 37.428571 37.571429 37.714286 37.857143
## [260] 38.000000 38.142857 38.285714 38.428571 38.571429 38.714286 38.857143
## [267] 39.000000 39.142857 39.285714 39.428571 39.571429 39.714286 39.857143
## [274] 40.000000 40.142857 40.285714 40.428571 40.571429 40.714286 40.857143
## [281] 41.000000 41.142857 41.285714 41.428571 41.571429 41.714286 41.857143
## [288] 42.000000 42.142857 42.285714 42.428571 42.571429 42.714286 42.857143
## [295] 43.000000 43.142857 43.285714 43.428571 43.571429 43.714286 43.857143
## [302] 44.000000 44.142857 44.285714 44.428571 44.571429 44.714286 44.857143
## [309] 45.000000 45.142857 45.285714 45.428571 45.571429 45.714286 45.857143
## [316] 46.000000 46.142857 46.285714 46.428571 46.571429 46.714286 46.857143
## [323] 47.000000 47.142857 47.285714 47.428571 47.571429 47.714286 47.857143
## [330] 48.000000 48.142857 48.285714 48.428571 48.571429 48.714286 48.857143
## [337] 49.000000 49.142857 49.285714 49.428571 49.571429 49.714286 49.857143
## [344] 50.000000 50.142857 50.285714 50.428571 50.571429 50.714286 50.857143
## [351] 51.000000 51.142857 51.285714 51.428571 51.571429 51.714286 51.857143
## [358] 52.000000 52.142857 52.285714 52.428571 52.571429 52.714286 52.857143
## [365] 53.000000 53.142857 53.285714 53.428571 53.571429 53.714286 53.857143
## [372] 54.000000 54.142857 54.285714 54.428571 54.571429 54.714286 54.857143
## [379] 55.000000 55.142857 55.285714 55.428571 55.571429 55.714286 55.857143
## [386] 56.000000 56.142857 56.285714 56.428571 56.571429 56.714286 56.857143
## [393] 57.000000 57.142857 57.285714 57.428571 57.571429 57.714286 57.857143
## [400] 58.000000 58.142857 58.285714 58.428571 58.571429 58.714286 58.857143
## [407] 59.000000 59.142857 59.285714 59.428571 59.571429 59.714286 59.857143
## [414] 60.000000 60.142857 60.285714 60.428571 60.571429 60.714286 60.857143
## [421] 61.000000 61.142857 61.285714 61.428571 61.571429 61.714286 61.857143
## [428] 62.000000 62.142857 62.285714 62.428571 62.571429 62.714286 62.857143
## [435] 63.000000 63.142857 63.285714 63.428571 63.571429 63.714286 63.857143
## [442] 64.000000 64.142857 64.285714 64.428571 64.571429 64.714286 64.857143
## [449] 65.000000 65.142857 65.285714 65.428571 65.571429 65.714286 65.857143
## [456] 66.000000 66.142857 66.285714 66.428571 66.571429 66.714286 66.857143
## [463] 67.000000 67.142857 67.285714 67.428571 67.571429 67.714286 67.857143
## [470] 68.000000 68.142857 68.285714 68.428571 68.571429 68.714286 68.857143
## [477] 69.000000 69.142857 69.285714 69.428571 69.571429 69.714286 69.857143
## [484] 70.000000 70.142857 70.285714 70.428571 70.571429 70.714286 70.857143
## [491] 71.000000 71.142857 71.285714 71.428571 71.571429 71.714286 71.857143
## [498] 72.000000 72.142857 72.285714 72.428571 72.571429 72.714286 72.857143
## [505] 73.000000 73.142857 73.285714 73.428571 73.571429 73.714286 73.857143
## [512] 74.000000 74.142857 74.285714 74.428571 74.571429
ts4_train <- ts(btv_pre_roll4$REQ, start = 1, frequency = 7)
time(ts4_train)
## Time Series:
## Start = c(1, 1) 
## End = c(78, 7) 
## Frequency = 7 
##   [1]  1.000000  1.142857  1.285714  1.428571  1.571429  1.714286  1.857143
##   [8]  2.000000  2.142857  2.285714  2.428571  2.571429  2.714286  2.857143
##  [15]  3.000000  3.142857  3.285714  3.428571  3.571429  3.714286  3.857143
##  [22]  4.000000  4.142857  4.285714  4.428571  4.571429  4.714286  4.857143
##  [29]  5.000000  5.142857  5.285714  5.428571  5.571429  5.714286  5.857143
##  [36]  6.000000  6.142857  6.285714  6.428571  6.571429  6.714286  6.857143
##  [43]  7.000000  7.142857  7.285714  7.428571  7.571429  7.714286  7.857143
##  [50]  8.000000  8.142857  8.285714  8.428571  8.571429  8.714286  8.857143
##  [57]  9.000000  9.142857  9.285714  9.428571  9.571429  9.714286  9.857143
##  [64] 10.000000 10.142857 10.285714 10.428571 10.571429 10.714286 10.857143
##  [71] 11.000000 11.142857 11.285714 11.428571 11.571429 11.714286 11.857143
##  [78] 12.000000 12.142857 12.285714 12.428571 12.571429 12.714286 12.857143
##  [85] 13.000000 13.142857 13.285714 13.428571 13.571429 13.714286 13.857143
##  [92] 14.000000 14.142857 14.285714 14.428571 14.571429 14.714286 14.857143
##  [99] 15.000000 15.142857 15.285714 15.428571 15.571429 15.714286 15.857143
## [106] 16.000000 16.142857 16.285714 16.428571 16.571429 16.714286 16.857143
## [113] 17.000000 17.142857 17.285714 17.428571 17.571429 17.714286 17.857143
## [120] 18.000000 18.142857 18.285714 18.428571 18.571429 18.714286 18.857143
## [127] 19.000000 19.142857 19.285714 19.428571 19.571429 19.714286 19.857143
## [134] 20.000000 20.142857 20.285714 20.428571 20.571429 20.714286 20.857143
## [141] 21.000000 21.142857 21.285714 21.428571 21.571429 21.714286 21.857143
## [148] 22.000000 22.142857 22.285714 22.428571 22.571429 22.714286 22.857143
## [155] 23.000000 23.142857 23.285714 23.428571 23.571429 23.714286 23.857143
## [162] 24.000000 24.142857 24.285714 24.428571 24.571429 24.714286 24.857143
## [169] 25.000000 25.142857 25.285714 25.428571 25.571429 25.714286 25.857143
## [176] 26.000000 26.142857 26.285714 26.428571 26.571429 26.714286 26.857143
## [183] 27.000000 27.142857 27.285714 27.428571 27.571429 27.714286 27.857143
## [190] 28.000000 28.142857 28.285714 28.428571 28.571429 28.714286 28.857143
## [197] 29.000000 29.142857 29.285714 29.428571 29.571429 29.714286 29.857143
## [204] 30.000000 30.142857 30.285714 30.428571 30.571429 30.714286 30.857143
## [211] 31.000000 31.142857 31.285714 31.428571 31.571429 31.714286 31.857143
## [218] 32.000000 32.142857 32.285714 32.428571 32.571429 32.714286 32.857143
## [225] 33.000000 33.142857 33.285714 33.428571 33.571429 33.714286 33.857143
## [232] 34.000000 34.142857 34.285714 34.428571 34.571429 34.714286 34.857143
## [239] 35.000000 35.142857 35.285714 35.428571 35.571429 35.714286 35.857143
## [246] 36.000000 36.142857 36.285714 36.428571 36.571429 36.714286 36.857143
## [253] 37.000000 37.142857 37.285714 37.428571 37.571429 37.714286 37.857143
## [260] 38.000000 38.142857 38.285714 38.428571 38.571429 38.714286 38.857143
## [267] 39.000000 39.142857 39.285714 39.428571 39.571429 39.714286 39.857143
## [274] 40.000000 40.142857 40.285714 40.428571 40.571429 40.714286 40.857143
## [281] 41.000000 41.142857 41.285714 41.428571 41.571429 41.714286 41.857143
## [288] 42.000000 42.142857 42.285714 42.428571 42.571429 42.714286 42.857143
## [295] 43.000000 43.142857 43.285714 43.428571 43.571429 43.714286 43.857143
## [302] 44.000000 44.142857 44.285714 44.428571 44.571429 44.714286 44.857143
## [309] 45.000000 45.142857 45.285714 45.428571 45.571429 45.714286 45.857143
## [316] 46.000000 46.142857 46.285714 46.428571 46.571429 46.714286 46.857143
## [323] 47.000000 47.142857 47.285714 47.428571 47.571429 47.714286 47.857143
## [330] 48.000000 48.142857 48.285714 48.428571 48.571429 48.714286 48.857143
## [337] 49.000000 49.142857 49.285714 49.428571 49.571429 49.714286 49.857143
## [344] 50.000000 50.142857 50.285714 50.428571 50.571429 50.714286 50.857143
## [351] 51.000000 51.142857 51.285714 51.428571 51.571429 51.714286 51.857143
## [358] 52.000000 52.142857 52.285714 52.428571 52.571429 52.714286 52.857143
## [365] 53.000000 53.142857 53.285714 53.428571 53.571429 53.714286 53.857143
## [372] 54.000000 54.142857 54.285714 54.428571 54.571429 54.714286 54.857143
## [379] 55.000000 55.142857 55.285714 55.428571 55.571429 55.714286 55.857143
## [386] 56.000000 56.142857 56.285714 56.428571 56.571429 56.714286 56.857143
## [393] 57.000000 57.142857 57.285714 57.428571 57.571429 57.714286 57.857143
## [400] 58.000000 58.142857 58.285714 58.428571 58.571429 58.714286 58.857143
## [407] 59.000000 59.142857 59.285714 59.428571 59.571429 59.714286 59.857143
## [414] 60.000000 60.142857 60.285714 60.428571 60.571429 60.714286 60.857143
## [421] 61.000000 61.142857 61.285714 61.428571 61.571429 61.714286 61.857143
## [428] 62.000000 62.142857 62.285714 62.428571 62.571429 62.714286 62.857143
## [435] 63.000000 63.142857 63.285714 63.428571 63.571429 63.714286 63.857143
## [442] 64.000000 64.142857 64.285714 64.428571 64.571429 64.714286 64.857143
## [449] 65.000000 65.142857 65.285714 65.428571 65.571429 65.714286 65.857143
## [456] 66.000000 66.142857 66.285714 66.428571 66.571429 66.714286 66.857143
## [463] 67.000000 67.142857 67.285714 67.428571 67.571429 67.714286 67.857143
## [470] 68.000000 68.142857 68.285714 68.428571 68.571429 68.714286 68.857143
## [477] 69.000000 69.142857 69.285714 69.428571 69.571429 69.714286 69.857143
## [484] 70.000000 70.142857 70.285714 70.428571 70.571429 70.714286 70.857143
## [491] 71.000000 71.142857 71.285714 71.428571 71.571429 71.714286 71.857143
## [498] 72.000000 72.142857 72.285714 72.428571 72.571429 72.714286 72.857143
## [505] 73.000000 73.142857 73.285714 73.428571 73.571429 73.714286 73.857143
## [512] 74.000000 74.142857 74.285714 74.428571 74.571429 74.714286 74.857143
## [519] 75.000000 75.142857 75.285714 75.428571 75.571429 75.714286 75.857143
## [526] 76.000000 76.142857 76.285714 76.428571 76.571429 76.714286 76.857143
## [533] 77.000000 77.142857 77.285714 77.428571 77.571429 77.714286 77.857143
## [540] 78.000000 78.142857 78.285714 78.428571 78.571429 78.714286 78.857143
# ETS (지수평활(exponential smoothing))
fit1 <- ets(ts2_train) 
fit1_2 <- ets(ts3_train) 
fit1_3 <- ets(ts4_train) 

fcast1 <- forecast(fit1, h=31); autoplot(fcast1) # 5월 예측

fcast1_2 <- forecast(fit1_2, h=30); autoplot(fcast1_2) # 6월 예측

fcast1_3 <- forecast(fit1_3, h=31); autoplot(fcast1_3) # 7월 예측

checkresiduals(fit1); summary(fcast1) # 5월

## 
##  Ljung-Box test
## 
## data:  Residuals from ETS(A,N,A)
## Q* = 101.45, df = 5, p-value < 2.2e-16
## 
## Model df: 9.   Total lags used: 14
## 
## Forecast method: ETS(A,N,A)
## 
## Model Information:
## ETS(A,N,A) 
## 
## Call:
##  ets(y = ts2_train) 
## 
##   Smoothing parameters:
##     alpha = 0.4616 
##     gamma = 1e-04 
## 
##   Initial states:
##     l = 4550115.4612 
##     s = 275903.1 324576.5 -135739.8 -143547.9 -83962.3 -119893.9
##            -117335.6
## 
##   sigma:  246653.8
## 
##      AIC     AICc      BIC 
## 15053.50 15053.96 15095.34 
## 
## Error measures:
##                     ME     RMSE      MAE        MPE     MAPE      MASE
## Training set -5814.633 244354.5 177077.9 -0.4963145 4.776987 0.8043576
##                   ACF1
## Training set 0.1405098
## 
## Forecasts:
##          Point Forecast   Lo 80   Hi 80   Lo 95   Hi 95
## 70.28571        3164450 2848350 3480549 2681017 3647882
## 70.42857        3104865 2756719 3453012 2572421 3637309
## 70.57143        3112673 2735190 3490155 2535363 3689983
## 70.71429        3572985 3168288 3977683 2954054 4191917
## 70.85714        3524327 3094133 3954522 2866402 4182253
## 71.00000        3131081 2676819 3585343 2436347 3825815
## 71.14286        3128513 2651386 3605640 2398810 3858216
## 71.28571        3164450 2665513 3663386 2401392 3927507
## 71.42857        3104865 2585034 3624697 2309852 3899879
## 71.57143        3112673 2572755 3652591 2286939 3938407
## 71.71429        3572985 3013701 4132270 2717634 4428337
## 71.85714        3524327 2946326 4102329 2640350 4408304
## 72.00000        3131081 2534949 3727213 2219376 4042786
## 72.14286        3128513 2514779 3742247 2189888 4067138
## 72.28571        3164450 2533612 3795288 2199667 4129233
## 72.42857        3104865 2457375 3752355 2114615 4095116
## 72.57143        3112673 2448948 3776398 2097594 4127752
## 72.71429        3572985 2893414 4252557 2533670 4612300
## 72.85714        3524327 2829270 4219384 2461329 4587326
## 73.00000        3131081 2420876 3841286 2044916 4217246
## 73.14286        3128513 2403470 3853556 2019655 4237371
## 73.28571        3164450 2424872 3904027 2033364 4295536
## 73.42857        3104865 2351034 3858697 1951979 4257751
## 73.57143        3112673 2344852 3880494 1938392 4286954
## 73.71429        3572985 2791425 4354545 2377692 4768278
## 73.85714        3524327 2729265 4319389 2308385 4740269
## 74.00000        3131081 2322743 3939419 1894835 4367327
## 74.14286        3128513 2307108 3949918 1872282 4384744
## 74.28571        3164450 2330188 3998712 1888556 4440344
## 74.42857        3104865 2257941 3951790 1809606 4400124
## 74.57143        3112673 2253273 3972073 1798335 4427011
checkresiduals(fit1_2); summary(fcast1_2) # 6월

## 
##  Ljung-Box test
## 
## data:  Residuals from ETS(A,N,A)
## Q* = 96.764, df = 5, p-value < 2.2e-16
## 
## Model df: 9.   Total lags used: 14
## 
## Forecast method: ETS(A,N,A)
## 
## Model Information:
## ETS(A,N,A) 
## 
## Call:
##  ets(y = ts3_train) 
## 
##   Smoothing parameters:
##     alpha = 0.4338 
##     gamma = 1e-04 
## 
##   Initial states:
##     l = 4550115.2107 
##     s = 283294.2 326223.8 -141043.9 -146431.7 -83865.87 -126526.5
##            -111650.1
## 
##   sigma:  247859.8
## 
##      AIC     AICc      BIC 
## 16051.99 16052.43 16094.45 
## 
## Error measures:
##                     ME     RMSE      MAE        MPE     MAPE      MASE
## Training set -7811.836 245688.7 179026.1 -0.5836397 4.893955 0.8097644
##                  ACF1
## Training set 0.146734
## 
## Forecasts:
##          Point Forecast   Lo 80   Hi 80   Lo 95   Hi 95
## 74.71429        3127726 2810081 3445371 2641930 3613523
## 74.85714        3084792 2738548 3431036 2555258 3614326
## 75.00000        2689903 2317249 3062558 2119978 3259829
## 75.14286        2674954 2277641 3072267 2067317 3282591
## 75.28571        2717592 2297065 3138120 2074450 3360734
## 75.42857        2655035 2212508 3097562 1978249 3331822
## 75.57143        2660461 2196969 3123954 1951611 3369312
## 75.71429        3127726 2644186 3611267 2388215 3867238
## 75.85714        3084792 2582002 3587582 2315841 3853743
## 76.00000        2689903 2168574 3211233 1892599 3487208
## 76.14286        2674954 2135723 3214185 1850271 3499637
## 76.28571        2717592 2161034 3274150 1866410 3568774
## 76.42857        2655035 2081674 3228396 1778155 3531916
## 76.57143        2660461 2070768 3250155 1758603 3562319
## 76.71429        3127726 2522149 3733304 2201575 4053878
## 76.85714        3084792 2463736 3705848 2134968 4034616
## 77.00000        2689903 2053745 3326062 1716983 3662823
## 77.14286        2674954 2024044 3325864 1679473 3670435
## 77.28571        2717592 2052258 3382927 1700051 3735134
## 77.42857        2655035 1975582 3334488 1615902 3694169
## 77.57143        2660461 1967171 3353751 1600165 3720757
## 77.71429        3127726 2420876 3834577 2046692 4208761
## 77.85714        3084792 2364637 3804948 1983409 4186175
## 78.00000        2689903 1956684 3423123 1568542 3811265
## 78.14286        2674954 1928900 3421008 1533963 3815945
## 78.28571        2717592 1958921 3476264 1557304 3877881
## 78.42857        2655035 1883952 3426118 1475765 3834305
## 78.57143        2660461 1877158 3443765 1462502 3858421
## 78.71429        3127726 2332396 3923057 1911373 4344080
## 78.85714        3084792 2277614 3891970 1850319 4319265
checkresiduals(fit1_3); summary(fcast1_3) # 7월

## 
##  Ljung-Box test
## 
## data:  Residuals from ETS(A,N,A)
## Q* = 96.682, df = 5, p-value < 2.2e-16
## 
## Model df: 9.   Total lags used: 14
## 
## Forecast method: ETS(A,N,A)
## 
## Model Information:
## ETS(A,N,A) 
## 
## Call:
##  ets(y = ts4_train) 
## 
##   Smoothing parameters:
##     alpha = 0.4272 
##     gamma = 1e-04 
## 
##   Initial states:
##     l = 4550114.8701 
##     s = 288403 328734.3 -139540.5 -142463.3 -88463.7 -131435.8
##            -115234
## 
##   sigma:  246639.3
## 
##      AIC     AICc      BIC 
## 17010.08 17010.49 17053.11 
## 
## Error measures:
##                     ME     RMSE      MAE        MPE     MAPE      MASE
## Training set -6364.978 244598.1 175860.8 -0.5406877 4.835202 0.8057636
##                   ACF1
## Training set 0.1499347
## 
## Forecasts:
##          Point Forecast   Lo 80   Hi 80   Lo 95   Hi 95
## 79.00000        2950335 2634254 3266416 2466931 3433739
## 79.14286        2934043 2590331 3277756 2408381 3459706
## 79.28571        2976996 2607714 3346278 2412228 3541764
## 79.42857        2923056 2529863 3316248 2321720 3524391
## 79.57143        2925965 2510235 3341694 2290161 3561768
## 79.71429        3394262 2957156 3831369 2725765 4062759
## 79.85714        3353913 2896418 3811408 2654235 4053592
## 80.00000        2950335 2473331 3427339 2220820 3679849
## 80.14286        2934043 2438298 3429789 2175866 3692221
## 80.28571        2976996 2463192 3490800 2191200 3762791
## 80.42857        2923056 2391807 3454305 2110580 3735531
## 80.57143        2925965 2377826 3474104 2087658 3764271
## 80.71429        3394262 2829738 3958786 2530897 4257628
## 80.85714        3353913 2773459 3934368 2466185 4241642
## 81.00000        2950335 2354383 3546286 2038906 3861764
## 81.14286        2934043 2322988 3545099 1999514 3868573
## 81.28571        2976996 2351200 3602791 2019924 3934068
## 81.42857        2923056 2282860 3563252 1943960 3902151
## 81.57143        2925965 2271685 3580245 1925330 3926600
## 81.71429        3394262 2726195 4062329 2372542 4415982
## 81.85714        3353913 2672332 4035494 2311525 4396302
## 82.00000        2950335 2255508 3645161 1887689 4012980
## 82.14286        2934043 2226219 3641867 1851520 4016567
## 82.28571        2976996 2256409 3697583 1874953 4079038
## 82.42857        2923056 2189928 3656184 1801833 4044278
## 82.57143        2925965 2180507 3671423 1785885 4066044
## 82.71429        3394262 2636675 4151850 2235632 4552892
## 82.85714        3353913 2584382 4123445 2177017 4530810
## 83.00000        2950335 2169048 3731622 1755459 4145210
## 83.14286        2934043 2141175 3726912 1721456 4146631
## 83.28571        2976996 2172713 3781279 1746951 4207040
autoplot(fit1) + ylab("fcast1, ETS 광고인벤 예측(ts2_train)") # 5월

autoplot(fit1_2) + ylab("fcast1_2, ETS 광고인벤 예측(ts3_train)") # 6월

autoplot(fit1_3) + ylab("fcast1_3, ETS 광고인벤 예측(ts4_train)") # 7월

# ARIMA auto (자기회귀누적이동평균(ARIMA, AutoRegressive Integrated Moving Average)
fit2 <- auto.arima(ts2_train) # 5월
fit2_2 <- auto.arima(ts3_train) # 6월
fit2_3 <- auto.arima(ts4_train) # 7월

fcast2 <- forecast(fit2, h=31); autoplot(fcast2) # 5월 

fcast2_2 <- forecast(fit2_2, h=30); autoplot(fcast2_2) # 6월

fcast2_3 <- forecast(fit2_3, h=31); autoplot(fcast2_3) # 7월

checkresiduals(fit2); summary(fcast2)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(3,0,0)(0,1,2)[7]
## Q* = 16.97, df = 9, p-value = 0.0492
## 
## Model df: 5.   Total lags used: 14
## 
## Forecast method: ARIMA(3,0,0)(0,1,2)[7]
## 
## Model Information:
## Series: ts2_train 
## ARIMA(3,0,0)(0,1,2)[7] 
## 
## Coefficients:
##          ar1     ar2     ar3     sma1     sma2
##       0.6156  0.0354  0.1526  -0.6602  -0.2489
## s.e.  0.0464  0.0534  0.0463   0.0511   0.0538
## 
## sigma^2 estimated as 5.588e+10:  log likelihood=-6595.92
## AIC=13203.84   AICc=13204.02   BIC=13228.86
## 
## Error measures:
##                     ME   RMSE    MAE        MPE     MAPE      MASE
## Training set -19761.99 233453 165322 -0.9066252 4.554031 0.7509575
##                     ACF1
## Training set -0.02231689
## 
## Forecasts:
##          Point Forecast   Lo 80   Hi 80   Lo 95   Hi 95
## 70.28571        3109849 2806895 3412803 2646521 3573178
## 70.42857        3195592 2839835 3551349 2651508 3739676
## 70.57143        3255102 2877848 3632356 2678141 3832062
## 70.71429        3762269 3363213 4161324 3151966 4372571
## 70.85714        3811081 3396340 4225822 3176789 4445372
## 71.00000        3305679 2880571 3730786 2655533 3955824
## 71.14286        3256894 2824085 3689703 2594970 3918818
## 71.28571        3314377 2848082 3780673 2601240 4027515
## 71.42857        3342904 2860421 3825386 2605010 4080797
## 71.57143        3389785 2898071 3881499 2637773 4141797
## 71.71429        3919439 3419693 4419185 3155144 4683734
## 71.85714        3936694 3431021 4442366 3163334 4710053
## 72.00000        3417211 2907359 3927063 2637460 4196963
## 72.14286        3401099 2888131 3914067 2616582 4185615
## 72.28571        3426261 2907641 3944880 2633101 4219421
## 72.42857        3433898 2911939 3955856 2635632 4232164
## 72.57143        3471761 2947637 3995885 2670182 4273339
## 72.71429        3990193 3464305 4516080 3185916 4794469
## 72.85714        3997033 3469833 4524232 3190750 4803315
## 73.00000        3469366 2941216 3997516 2661631 4277102
## 73.14286        3446135 2917276 3974994 2637315 4254955
## 73.28571        3465036 2933708 3996364 2652440 4277632
## 73.42857        3467318 2934683 3999954 2652722 4281914
## 73.57143        3500578 2967156 4033999 2684780 4316376
## 73.71429        4015031 3480929 4549132 3198192 4831869
## 73.85714        4018441 3483832 4553051 3200826 4836057
## 74.00000        3487821 2952848 4022794 2669650 4305991
## 74.14286        3462043 2926797 3997288 2643456 4280630
## 74.28571        3478748 2941835 4015661 2657610 4299886
## 74.42857        3479138 2941407 4016869 2656750 4301526
## 74.57143        3510766 2972571 4048961 2687668 4333864
checkresiduals(fit2_2); summary(fcast2_2)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(2,0,1)(0,1,1)[7]
## Q* = 22.36, df = 10, p-value = 0.01337
## 
## Model df: 4.   Total lags used: 14
## 
## Forecast method: ARIMA(2,0,1)(0,1,1)[7]
## 
## Model Information:
## Series: ts3_train 
## ARIMA(2,0,1)(0,1,1)[7] 
## 
## Coefficients:
##          ar1      ar2      ma1     sma1
##       1.3286  -0.3491  -0.7928  -0.8296
## s.e.  0.0869   0.0768   0.0657   0.0571
## 
## sigma^2 estimated as 5.867e+10:  log likelihood=-7034.39
## AIC=14078.77   AICc=14078.89   BIC=14099.93
## 
## Error measures:
##                     ME     RMSE      MAE        MPE     MAPE      MASE
## Training set -12486.56 239632.8 167796.5 -0.7247433 4.650259 0.7589708
##                     ACF1
## Training set 0.009220046
## 
## Forecasts:
##          Point Forecast   Lo 80   Hi 80   Lo 95   Hi 95
## 74.71429        3281368 2970939 3591796 2806608 3756127
## 74.85714        3343036 2990864 3695209 2804435 3881637
## 75.00000        2869283 2499550 3239016 2303825 3434741
## 75.14286        2688792 2307898 3069685 2106265 3271318
## 75.28571        2723819 2334135 3113502 2127849 3319788
## 75.42857        2680937 2283644 3078230 2073329 3288545
## 75.57143        2709950 2305808 3114092 2091868 3328031
## 75.71429        3322140 2899316 3744964 2675486 3968793
## 75.85714        3365116 2931230 3799001 2701545 4028686
## 76.00000        2884385 2442070 3326700 2207922 3560847
## 76.14286        2701147 2251621 3150674 2013656 3388638
## 76.28571        2734962 2278965 3190959 2037575 3432349
## 76.42857        2691429 2229515 3153343 1984992 3397866
## 76.57143        2719999 2252630 3187367 2005220 3434778
## 76.71429        3331828 2848863 3814793 2593197 4070459
## 76.85714        3374479 2882267 3866691 2621705 4127253
## 77.00000        2893443 2394170 3392715 2129871 3657014
## 77.14286        2709913 2204583 3215242 1937078 3482747
## 77.28571        2743446 2232664 3254227 1962273 3524618
## 77.42857        2699640 2183857 3215423 1910818 3488461
## 77.57143        2727946 2207540 3248352 1932054 3523838
## 77.71429        3339520 2805497 3873543 2522802 4156238
## 77.85714        3381924 2839845 3924004 2552886 4210963
## 78.00000        2900649 2352422 3448876 2062208 3739090
## 78.14286        2716888 2163381 3270394 1870372 3563403
## 78.28571        2750197 2191930 3308463 1896401 3603992
## 78.42857        2706174 2143533 3268816 1845689 3566660
## 78.57143        2734271 2167578 3300963 1867590 3600952
## 78.71429        3345642 2766733 3924551 2460277 4231006
## 78.85714        3387850 2801734 3973965 2491463 4284236
checkresiduals(fit2_3); summary(fcast2_3)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(2,0,1)(0,1,1)[7]
## Q* = 24.455, df = 10, p-value = 0.006479
## 
## Model df: 4.   Total lags used: 14
## 
## Forecast method: ARIMA(2,0,1)(0,1,1)[7]
## 
## Model Information:
## Series: ts4_train 
## ARIMA(2,0,1)(0,1,1)[7] 
## 
## Coefficients:
##          ar1      ar2      ma1     sma1
##       1.3452  -0.3611  -0.8093  -0.8678
## s.e.  0.0759   0.0688   0.0548   0.0612
## 
## sigma^2 estimated as 5.784e+10:  log likelihood=-7445.71
## AIC=14901.42   AICc=14901.53   BIC=14922.86
## 
## Error measures:
##                     ME   RMSE      MAE        MPE     MAPE      MASE
## Training set -12238.67 238056 165042.3 -0.7208341 4.597109 0.7561951
##                    ACF1
## Training set 0.01208896
## 
## Forecasts:
##          Point Forecast   Lo 80   Hi 80   Lo 95   Hi 95
## 79.00000        2885293 2577091 3193494 2413939 3356646
## 79.14286        2764850 2415190 3114510 2230091 3299608
## 79.28571        2805819 2439001 3172638 2244819 3366820
## 79.42857        2855631 2478045 3233216 2278163 3433098
## 79.57143        2830791 2444741 3216841 2240379 3421203
## 79.71429        3398927 3005502 3792353 2797235 4000620
## 79.85714        3402865 3002730 3803001 2790911 4014820
## 80.00000        2892363 2476977 3307749 2257085 3527641
## 80.14286        2770894 2345721 3196067 2120648 3421141
## 80.28571        2811398 2378438 3244357 2149243 3473552
## 80.42857        2860952 2421161 3300743 2188349 3533554
## 80.57143        2835935 2389903 3281967 2153788 3518082
## 80.71429        3403926 2952098 3855754 2712914 4094938
## 80.85714        3407732 2950483 3864981 2708430 4107034
## 81.00000        2897105 2427016 3367194 2178165 3616045
## 81.14286        2775516 2297174 3253858 2043955 3507077
## 81.28571        2815902 2330980 3300825 2074278 3557527
## 81.42857        2865343 2374632 3356054 2114865 3615821
## 81.57143        2840216 2344200 3336232 2081625 3598807
## 81.71429        3408099 2907141 3909056 2641950 4174247
## 81.85714        3411800 2906208 3917391 2638564 4185035
## 82.00000        2901070 2384224 3417916 2110623 3691517
## 82.14286        2779381 2255320 3303442 1977898 3580864
## 82.28571        2819670 2289858 3349483 2009392 3629949
## 82.42857        2869016 2334138 3403894 2050991 3687041
## 82.57143        2843796 2304269 3383323 2018661 3668931
## 82.71429        3411589 2867723 3955454 2579818 4243359
## 82.85714        3415202 2867261 3963143 2577198 4253205
## 83.00000        2904387 2346340 3462433 2050928 3757845
## 83.14286        2782614 2218108 3347120 1919276 3645952
## 83.28571        2822822 2253172 3392472 1951617 3694026
autoplot(fit2) + ylab("fcast2, ARIMA(auto) 광고인벤 예측(ts2_train)") # 5월

autoplot(fit2_2) + ylab("fcast2_2, ARIMA(auto) 광고인벤 예측(ts3_train)") # 6월

autoplot(fit2_3) + ylab("fcast2_3, ARIMA(auto) 광고인벤 예측(ts4_train)") # 7월

#예측하기 > # ETS, ARIMA auto
#2019년 5월 
t1 <- length(fcast1$fitted)-30; t2 <- length(fcast1$fitted)
paste(sum(fcast1$lower[,"95%"]), "|", sum(fcast1$fitted[t1:t2]), "|", sum(fcast1$upper[,"95%"]))
## [1] "70554716.4002906 | 104794363.886769 | 130120417.384799"
paste(sum(fcast1$lower[,"80%"]), "|", sum(fcast1$fitted[t1:t2]), "|", sum(fcast1$upper[,"80%"]))
## [1] "80863607.6893168 | 104794363.886769 | 119811526.095773"
paste(sum(fcast2$lower[,"95%"]), "|", sum(fcast2$fitted[t1:t2]), "|", sum(fcast2$upper[,"95%"]))
## [1] "86290593.9187959 | 106360089.333256 | 132784483.906288"
paste(sum(fcast2$lower[,"80%"]), "|", sum(fcast2$fitted[t1:t2]), "|", sum(fcast2$upper[,"80%"]))
## [1] "94337178.6070153 | 106360089.333256 | 124737899.218068"
#2019년 6월
t1 <- length(fcast1_2$fitted)-29; t2 <- length(fcast1_2$fitted)
paste(sum(fcast1_2$lower[,"95%"]), "/", sum(fcast1_2$fitted[t1:t2]), "/", sum(fcast1_2$upper[,"95%"]))
## [1] "57277874.9363902 / 90815624.4961479 / 112030880.70457"
paste(sum(fcast1_2$lower[,"80%"]), "/", sum(fcast1_2$fitted[t1:t2]), "/", sum(fcast1_2$upper[,"80%"]))
## [1] "66753844.7459017 / 90815624.4961479 / 102554910.895059"
paste(sum(fcast2_2$lower[,"95%"]), "/", sum(fcast2_2$fitted[t1:t2]), "/", sum(fcast2_2$upper[,"95%"]))
## [1] "66597259.2110926 / 91388610.6958451 / 110323076.116423"
paste(sum(fcast2_2$lower[,"80%"]), "/", sum(fcast2_2$fitted[t1:t2]), "/", sum(fcast2_2$upper[,"80%"]))
## [1] "74164780.209765 / 91388610.6958451 / 102755555.11775"
#2019년 7월
t1 <- length(fcast1_3$fitted)-29; t2 <- length(fcast1_3$fitted)
paste(sum(fcast1_3$lower[,"95%"]), "/", sum(fcast1_3$fitted[t1:t2]), "/", sum(fcast1_3$upper[,"95%"]))
## [1] "66512723.9690094 / 89362238.4840273 / 122878581.343909"
paste(sum(fcast1_3$lower[,"80%"]), "/", sum(fcast1_3$fitted[t1:t2]), "/", sum(fcast1_3$upper[,"80%"]))
## [1] "76267826.0913149 / 89362238.4840273 / 113123479.221603"
paste(sum(fcast2_3$lower[,"95%"]), "/", sum(fcast2_3$fitted[t1:t2]), "/", sum(fcast2_3$upper[,"95%"]))
## [1] "70305796.6991194 / 89814021.2570356 / 114676007.604492"
paste(sum(fcast2_3$lower[,"80%"]), "/", sum(fcast2_3$fitted[t1:t2]), "/", sum(fcast2_3$upper[,"80%"]))
## [1] "77984841.4032791 / 89814021.2570356 / 106996962.900332"