【问题标题】:Estimation of custom State Space model using KFAS R package with T and R matxices使用带有 T 和 R 矩阵的 KFAS R 包估计自定义状态空间模型
【发布时间】:2018-02-10 19:04:22
【问题描述】:

我想用一些特定的假设来估计一个状态空间模型。我查看了How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation? 以解决 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

具体来说,我想将来自Modeling data revisions: Measurement error and dynamics of “true” values 的模型应用于二维可观察向量。

【问题讨论】:

    标签: r time-series state-space


    【解决方案1】:

    您的模型中有 13 个 NA 值,但您只在目标函数中修改了其中两个。试试这个:

    updatefn <- function(pars, model) {
      diag(model$T[,,1]) <- pars[1:5]
      model$R[is.na(model$R)] <- pars[6:13]
      model
    }
    
    fit <- fitSSM(ss_model, rep(0.5,13), updatefn, method = "L-BFGS-B",
                  lower = c(0, -0.99), upper = c(100, 0.99))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-09
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 2015-10-24
      • 2013-12-26
      • 2023-01-28
      相关资源
      最近更新 更多