SSブログ

R: dlmその2 [統計]

状態空間モデルをためすから2年以上してつづき。

予測してみる。

data(Nile)

## dlm
##
## Petris G., Petrone, S. (2011) State Space Models in R
## Journal of Statistical Software 41(4)
## http://www.jstatsoft.org/v41/i04
##

library(dlm)

buildNile <- function(theta) {
  dlmModPoly(order = 1, dV = theta[1], dW = theta[2])
}
fit <- dlmMLE(Nile, parm = c(100, 2), buildNile, lower = rep(1e-4, 2))
modNile <- buildNile(fit$par)
smoothNile <- dlmSmooth(Nile, modNile)

## 予測
filterNile <- dlmFilter(Nile, modNile)
foreNile <- dlmForecast(filterNile, nAhead = 10)

## 50%予測区間
hwidth <- qnorm(0.25, lower = FALSE) * sqrt(unlist(foreNile$Q))

plot(Nile, type = "o", xlim = c(1951, 1980), las = 1)
lines(smoothNile$s, col = "red")
lines(1971:1980, foreNile$f, type = "o", pch = 16, col = "blue")
lines(1971:1980, foreNile$f + hwidth, type = "o", pch = 16, lty = 2, col = "blue")
lines(1971:1980, foreNile$f - hwidth, type = "o", pch = 16, lty = 2, col = "blue")

Rplot.png


タグ:R dlm
nice!(0)  コメント(0)  トラックバック(1) 
共通テーマ:日記・雑感

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

Facebook コメント

トラックバック 1