简体   繁体   English

如何使用 R 中的 KFAS 包进行动态因子分析

[英]How to conduct Dynamic Factor Analysis using KFAS package in R

I am attempting to fit this model into a multivariate time series data using the package KFAS in R:我正在尝试使用 R 中的 KFAS 包将此模型拟合到多变量时间序列数据中:

y_t = Zx_t + a + v_t, v_t ~ MVN(0,R) y_t = Zx_t + a + v_t, v_t ~ MVN(0,R)

x_t = x_(t-1) + w_t, w_t ~ MVN(0,Q) x_t = x_(t-1) + w_t, w_t ~ MVN(0,Q)

This is a dynamic factor model.这是一个动态因素模型。 I need to estimate as well some parameters, namely the matrix of factor loadings Z, and the variance-covariance matrix of observation disturbance, R. I am well aware that this type of model can be ran using MARSS package however I would still need to run it using a more flexible package as I would modify the state equations later on (to include seasonal decomposition).我还需要估计一些参数,即因子载荷矩阵 Z 和观测扰动的方差-协方差矩阵 R。我很清楚这种类型的模型可以使用 MARSS 包运行,但我仍然需要使用更灵活的包运行它,因为我稍后会修改状态方程(包括季节性分解)。

This is the code that I used (using a simulated data instead of the actual data I intend to run):这是我使用的代码(使用模拟数据而不是我打算运行的实际数据):

library(KFAS)
library(mAr)

set.seed(100)

w=c(0.25,0.1)
C=rbind(c(1,0.5),c(0.5,1.5))
A=rbind(c(0.1,0,0,0),c(0.3,0,0,0))
data=as.matrix(mAr.sim(w,A,C,N=300))

N.ts = dim(data)[2]
N.ls = 1

#ASSUMING 1 FACTOR
Z.vals = matrix(NA,N.ts,N.ls)
Zt = matrix(Z.vals, nrow=N.ts, ncol=N.ls, byrow=TRUE) #MATRIX OF LOADINGS, N X P
Ht <- diag(NA,N.ts) #VAR-COV MATRIX OF OBS ERROR, N x N
Tt <- diag(N.ls) #SLOPE OF LATENT STATE AT T-1, P X P
Rt <- diag(N.ls) #SLOPE OF THE LATENT STATE DISTURBANCES, P X P
Qt <- diag(N.ls) #VAR-COV MATRIX OF THE LATENT STATE DISTURBANCES, P X P

ss_model <- SSModel(data ~ 
                  -1 + SSMcustom(Z = Zt, T = Tt, R = Rt, Q = Qt),
                  H=Ht
                  )

objf <- function(pars, model, estimate = TRUE) {
   model$Z[1] <- pars[1]
   model$H[1] <- pars[2]
  if (estimate) {
    -logLik(model)
  } else {
    model
  }
}

opt <- optim(par = rep(1,50), fn = objf, method = "L-BFGS-B", 
             model = ss_model)

ss_model_opt <- objf(opt$par, ss_model, estimate = FALSE)

updatefn <- function(pars, model) {
  model$Z[1] <- pars[1]
  model$H[1] <- pars[2]
  model
}

fit <- fitSSM(ss_model, rep(1,50), updatefn, method = "L-BFGS-B")

If I look at the model specification, it seems correct to me:如果我查看模型规范,对我来说似乎是正确的:

Call:
SSModel(formula = data ~ -1 + SSMcustom(Z = Zt, T = Tt, R = Rt, 
    Q = Qt), H = Ht)

State space model object of class SSModel

Dimensions:
[1] Number of time points: 300
[1] Number of time series: 2
[1] Number of disturbances: 1
[1] Number of states: 1
Names of the states:
[1]  custom1
Distributions of the time series:
[1]  gaussian

Object is a valid object of class SSModel.

However it's returning this error message: Error in is.SSModel(do.call(updatefn, args = c(list(inits, model), update_args)), : System matrices (excluding Z) contain NA or infinite values, covariance matrices contain values larger than 1e+07但是它返回此错误消息: is.SSModel(do.call(updatefn, args = c(list(inits, model), update_args)) 中的错误:系统矩阵(不包括 Z)包含 NA 或无限值,协方差矩阵包含大于 1e+07 的值

Hope that someone can guide me doing this.希望有人可以指导我这样做。 Thanks a lot!非常感谢!

you could look up variance covariance matrix on google.你可以在谷歌上查找方差协方差矩阵。 the seasonal component;季节性成分; a'a is applied after finding the vector/deviation of the scores matrix a=A-11A(1/n).在找到分数矩阵 a=A-11A(1/n) 的向量/偏差后应用 a'a。 the 1 means the scores of N 1 ones. 1 表示 N 1 个的分数 p=1, n=number of rows; p=1,n=行数; 2rows of data which gives the variance. 2行数据给出方差。 the red colour denotes the variance which is the diagonal of the matrix.红色表示作为矩阵对角线的方差。 Na is the value of the variance. Na 是方差的值。 we don't know the missing elements in the matrix, so we assume that T-1=N p ones where p=1, for a vector scores of matrix, we fill the latent ones with T-1=N*p ones where p=1.我们不知道矩阵中的缺失元素,所以我们假设 T-1=N p 个,其中 p=1,对于矩阵的向量分数,我们用 T-1=N*p 个填充潜在的,其中p=1。 type the error message,ssmodel输入错误信息,ssmodel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM