Contents

1 譯殊
2 谿蟯(, )
3 plotForecastErrors
4 讌覯: Holt-Winters filtering
5 伎 讌
6 ARIMA
7 AR
8
9 蠍壱
10 VAR
11 auto.arima
12 谿瑚襭


1 譯殊 #

螻伎襭 朱朱 襴曙 覃, 螳 磯 覿 伎 . 螻伎 4螳 焔朱 覿企, リ鍵 覲 豢(Trend: T), 螻覲(Seasonal fluctuation: S), 觜螻 覲 覩誤 (Cyclic: C), 蠏碁Μ螻 覿蠏豺 れ姶(Random error: R)螳 蠏 焔れ . --[http]れ企 蟲′伎(http://terms.naver.com/entry.nhn?docId=1924236&cid=531&categoryId=531)

  • trend
  • seasonal fluctuation
  • cycles
  • white noise

2 谿蟯(, ) #

> tmp
   curr_val prev_val
5   3949515  4316313
6   3835690  4149917
7   3975137  4165120
8   3993659  4050347
9   3942373  3949515
10  3825807  3835690
11  3920338  3975137
12  3586568  3993659
13  3633877  3942373
14  3444306  3825807
15  3903889  3920338
16  3529384  3586568
17  3531147  3633877
18  3518490  3444306
19  3481886  3903889
20  3298512  3529384
21  3400325  3531147
22  3641914  3518490
23  3466527  3481886
24  6028886  3298512
25  6543880  3400325
26  5516709  3641914
27  5100715  3466527
28  5259752  6028886
29  5370662  6543880
30  5009019  5516709
31  4778911  5100715
32  4836214  5259752
33  4662136  5370662

tmp <- na.omit(tmp)
ccf(tmp$curr_val, tmp$prev_val)
豌 覯讌 襷り覲襯 蠍一朱 朱 / 讌 . 螳 覲朱蟆 覿覿 x豢 -4 企蟾 tmp$curr_val螳 4殊 觜襯企.
ts01.png

豌 覯讌 襷り覲襯 蠍一朱 朱 讌 .
lag.plot2(itall.R) 襯 伎伎 谿碁ゼ 蠏碁る慨.
source("http://databaser.net/moniwiki/pds/TimeSeries/itall.R")
lag.plot2(tmp$curr_val , tmp$prev_val, 11)
ts02.png

譴伎 殊 谿碁ゼ 蠏碁る慨.
plot(scale(tmp$curr_val), type="l", col="red")
lines(scale(tmp$prev_val), col="blue")
ts03.png

豕/豕 蟲谿蟯 覦 谿 ([http]豢豌(http://blog.naver.com/lofie21?Redirect=Log&logNo=110171809189))
max_ccf <- function(...)
{
    d <- ccf(...)
    cor <- d$acf[,,1]
    lag <- d$lag[,,1]
    dd <- data.frame(cor, lag)
    max_val <- dd[which.max(dd$cor),]
    min_val <- dd[which.min(dd$cor),]
    result <- c()
      if(abs(max_val[1]) >= abs(min_val[1])){
          result <- max_val
      } else {
          result <- min_val
      }
    return(result)
}

max_ccf <- function(...)
{
  d <- ccf(...)
  cor <- d$acf[,,1]
  lag <- d$lag[,,1]
  df <- data.frame(cor, lag)
  max_val <- df[which.max(df$cor),]
  return(max_val)
}

min_ccf <- function(...)
{
  d <- ccf(...)
  cor <- d$acf[,,1]
  lag <- d$lag[,,1]
  df <- data.frame(cor, lag)
  min_ccf <- df[which.min(df$cor),]
  return(min_ccf)
}

襯 れ, れ螻 螳 蟆郁骸螳 る..
> max_ccf(sales, val)
         cor lag
12 0.8932095  -1
  • 豕 蟯 螻 0.89
  • 螳豢 sales螳 val覲企 -1 れ 詩.

3 plotForecastErrors #

plotForecastErrors <- function(forecasterrors)
{
    # make a histogram of the forecast errors:
    mybinsize <- IQR(forecasterrors)/4
    mysd   <- sd(forecasterrors)
    mymin  <- min(forecasterrors) - mysd*5
    mymax  <- max(forecasterrors) + mysd*3
    # generate normally distributed data with mean 0 and standard deviation mysd
    mynorm <- rnorm(10000, mean=0, sd=mysd)
    mymin2 <- min(mynorm)
    mymax2 <- max(mynorm)
    if (mymin2 < mymin) { mymin <- mymin2 }
    if (mymax2 > mymax) { mymax <- mymax2 }
    # make a red histogram of the forecast errors, with the normally distributed data overlaid:
    mybins <- seq(mymin, mymax, mybinsize)
    hist(forecasterrors, col="red", freq=FALSE, breaks=mybins)
    # freq=FALSE ensures the area under the histogram = 1
    # generate normally distributed data with mean 0 and standard deviation mysd
    myhist <- hist(mynorm, plot=FALSE, breaks=mybins)
    # plot the normal curve as a blue line on top of the histogram of forecast errors:
    points(myhist$mids, myhist$density, type="l", col="blue", lwd=2)
}
p10 <- forecast(m, 10)
plotForecastErrors(p10 $residuals)
forecast_erros.png

4 讌覯: Holt-Winters filtering #

螻瑚骸 覿蠏豺語 蟇
m1<- HoltWinters(ts, gamma=FALSE)
plot(m1)
HoltWinters.png

plot(forecast.HoltWinters(m1, h=10))

5 伎 讌 #

library("TSA")
detectAO(m) #螳覯 伎(AO;additive outlier) 讌 - T覯讌 蟯豸♀ 譴 蟯 伎豺
detectIO(m) # 伎(IO;innovative outlier) 讌 - 覈螳 蟯 伎豺

6 ARIMA #

library("zoo")
ts <- zoo(long_term$dau, as.Date(long_term$dt))
plot(ts)
fit <- auto.arima(ts)
plot(forecast(fit, h=20))

arima(ts, order=c(2,1,2))
m <- arima(ts, order=c(2,1,2), fixed=c(0,NA,0,NA))
confint(m)
tsdiag(m)
predict(m, n.ahead=10)

library("lmtest") 
coeftest(fit) # ARIMA p-value test

?
t <- ts(training$amt, frequency=30)
fit <- auto.arima(t,D=1)
pred <- forecast(fit,h=30)
plot(pred)
grid()


7 AR #

library("MASS")
ar.result <- ar(ts)
predict(ar.result, n.ahead=9)

8 #

library("forecast")
data(gold)
g <- data.frame(g = na.omit(coredata(gold)))$g
y_width <- c(min(g) - min(g) * 0.05, max(g) - max(g) * 0.05) 

plot(g, type="l", lty="dotted", ylim=y_width)
par(new=T)
plot(smooth(g, twiceit=T), type="l", col="blue", ylim=y_width, ylab="")

企譴螳螳 : runmde()

loess.gif
--蠏碁 豢豌: http://simplystatistics.org/2014/02/13/loess-explained-in-a-gif/

9 蠍壱 #

7
ts_df <- ts(mydata$y, freq=365.25/7, start=decimal_date(ymd(min(mydata$date_key))))

10 VAR #

11 auto.arima #

library(lubridate)
library("forecast")
ts_df <- ts(mydata$y, freq=7)
#ts_df <- ts(mydata$y, freq=365.25/7, start=decimal_date(ymd(min(mydata$dt)))) 
d.arima <- auto.arima(ts_df)
d.forecast <- data.frame(forecast(d.arima, level = c(99), h = 1))

12 谿瑚襭 #