SSブログ

R: dlmその3 [統計]

前回のつづき。

1899年にアスワンダムができたので、その前後を変数とした回帰モデル。

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

data(Nile)

## dlm
library(dlm)

x <- matrix(c(rep(0, 27), rep(1, length(Nile) - 27)), ncol = 1)

# regression
modNileReg <- dlmModReg(x, dW = c(1, 0))
# dW:  diagonal elements of the variance matrix of the system noise

buildFun <- function(theta) {
              V(modNileReg) <- exp(theta[1])
              diag(W(modNileReg))[1] <- exp(theta[2])
              return(modNileReg)
            }
fit <- dlmMLE(Nile, parm = rep(0, 2), build = buildFun)
modNileReg <- buildFun(fit$par)

# variance of the observation noise.
drop(V(modNileReg))

# variance of the system noise.
W(modNileReg)[1]

modSmooth <- dlmSmooth(Nile, mod = modNileReg)
plot(Nile, type = "o", las = 1)
lines(ts(modSmooth$s[-1, 1] + modSmooth$s[-1, 2] * x, start = 1871),
      lty = 2)

結果

> drop(V(modNileReg))
[1] 16928.26
> W(modNileReg)[1]
[1] 0.0001575562

Rplot01.png


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

nice! 0

コメント 0

コメントを書く

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

Facebook コメント

トラックバック 1