我想用一些特定的假设来估计一个状态空间模型。我研究了如何使用“KFAS”R 包和 AR(1) 转换方程估计卡尔曼滤波器?克服这个问题Error ... : System matrices (excluding Z) contain NA or infinite values
,但是我仍然无法获得预期的结果。
下面是一些例子:
# simulation ----------------------
library(KFAS)
set.seed(2018)
xt<-arima.sim(n = 100, list(ar = c(0.7)))+5
at<-arima.sim(n = 100, list(ar = c(0.2,0.1)),sd=0.1)
bt<-arima.sim(n = 100, list(ar = c(0.1)),sd=0.1)
e1<-rnorm(100,0,0.5)
e2<-rnorm(100,0,0.5)
plot(xt+at,type='l')
lines(xt+bt,type='l',col='red')
tempM<-matrix(c(xt+at+e1,
xt+bt+e2),100,2)
# model -----------------------------------------------
Zt <- t(matrix(c(1,1,0,1,0,1,0,1,0,1),5,2))
Ht <- matrix(c(0,0,0,0),2,2)
Tt <- diag(NA,5)
Rt <- matrix(c(c(NA,0,0,0,0),c(NA,NA,0,0,0),c(NA,NA,NA,0,0),c(0,0,0,NA,0),c(0,0,0,0,NA)),5,5)
Qt <- diag(1,5)
ss_model <- SSModel(tempM ~ -1 + SSMcustom(Z = Zt, T = Tt, R = Rt,
Q = Qt), H = Ht)
# this gives an error
updatefn <- function(pars, model) {
model$T[1] <- pars[1]
model$R[1] <- pars[2]
model
}
fit <- fitSSM(ss_model, c(1, 0.5), updatefn, method = "L-BFGS-B",
lower = c(0, -0.99), upper = c(100, 0.99))
# end error example
# using optim (from link) -------------------
objf <- function(pars, model, estimate = TRUE) {
model$T[1] <- pars[1]
model$R[1] <- pars[2]
if (estimate) {
-logLik(model)
} else {
model
}
}
opt <- optim(c(diag(1,5),diag(1,5)), objf, method = "L-BFGS-B",
model = ss_model)
ss_model_opt <- objf(opt$par, ss_model, estimate = FALSE)
现在我得到:
ss_model_opt$R
[,1] [,2] [,3] [,4] [,5]
custom1 0 NA NA 0 0
custom2 0 NA NA 0 0
custom3 0 0 NA 0 0
custom4 0 0 0 NA 0
custom5 0 0 0 0 NA
我希望是这样的:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0.1 0.1 0.0 0.0
[2,] 0 -0.1 -0.1 0.0 0.0
[3,] 0 0.0 -0.1 0.0 0.0
[4,] 0 0.0 0.0 0.5 0.0
[5,] 0 0.0 0.0 0.0 0.5
具体来说,我想将建模数据修订中的模型:测量误差和“真实”值的动态应用于二维可观察向量。