1 ARIMA

require(WDI)
## Loading required package: WDI
## Loading required package: RJSONIO
# データを取得する
gdp <- WDI(country = c("US" , "CA" , "GB" , "DE" , "CN" , "JP" , "SG" , "IL") ,
indicator = c("NY.GDP.PCAP.CD" , "NY.GDP.MKTP.CD") ,
start = 1960 , end = 2011)
# 変数名を変更する
names(gdp) <- c("iso2c" , "country" , "Year" , "PerCapGDP" , "GDP")
head(gdp)
##   iso2c country Year PerCapGDP         GDP
## 1    CA  Canada 1960  2294.569 41093453545
## 2    CA  Canada 1961  2231.294 40767969454
## 3    CA  Canada 1962  2255.230 41978852041
## 4    CA  Canada 1963  2354.839 44657169109
## 5    CA  Canada 1964  2529.518 48882938810
## 6    CA  Canada 1965  2739.586 53909570342
require(ggplot2)
## Loading required package: ggplot2
require(scales)
## Loading required package: scales
# 1人あたりのGDP
ggplot(gdp , aes(Year , PerCapGDP , color = country , linetype = country)) +
geom_line() + scale_y_continuous(label = dollar)
## Warning: Removed 10 rows containing missing values (geom_path).

require(useful)
## Loading required package: useful
us <- gdp$PerCapGDP[gdp$country == "United States"]
# 時系列データへ変換
us <- ts(us , start = min(gdp$Year) , end = max(gdp$Year))
plot(us)

自己共分散関数(ACF)と偏自己共分散関数(PACF)を見る。

acf(us)

pacf(us)

原系列はトレンドを持っているので差分系列を考える。ndiffs()で適切な回数差分を取ってくれる。

require(forecast)
## Loading required package: forecast
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: timeDate
## This is forecast 7.2
ndiffs(us)
## [1] 2

ARIMAでフィットする。最適なパラメータは勝手に計算してくれる。

usBest <- auto.arima(x = us)
usBest
## Series:  
## ARIMA(1,2,1)                    
## 
## Coefficients:
##          ar1      ma1
##       0.4195  -0.8792
## s.e.  0.1577   0.0753
## 
## sigma^2 estimated as 306581:  log likelihood=-386.14
## AIC=778.27   AICc=778.79   BIC=784.01
theForecast <- forecast(object = usBest , h = 5)
plot(theForecast)

2 GARCH

require(quantmod)
## Loading required package: quantmod
## Loading required package: xts
## 
## Attaching package: 'xts'
## The following object is masked from 'package:useful':
## 
##     reclass
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
att <- getSymbols("T" , auto.assign = FALSE)
##     As of 0.4-0, 'getSymbols' uses env=parent.frame() and
##  auto.assign=TRUE by default.
## 
##  This  behavior  will be  phased out in 0.5-0  when the call  will
##  default to use auto.assign=FALSE. getOption("getSymbols.env") and 
##  getOptions("getSymbols.auto.assign") are now checked for alternate defaults
## 
##  This message is shown once per session and may be disabled by setting 
##  options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
chartSeries(att)

require(rugarch)
## Loading required package: rugarch
## Loading required package: parallel
## 
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
## 
##     sigma
attSpec <- ugarchspec(variance.model = list(model = "sGARCH" ,
garchOrder = c(1,1)) ,
mean.model = list(armaOrder = c(1,1)) ,
distribution.model = "std")
attClose <- att$T.Close
attGarch <- ugarchfit (spec = attSpec , data = attClose)