简体   繁体   中英

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:

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

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).

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

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). the 1 means the scores of N 1 ones. p=1, n=number of rows; 2rows of data which gives the variance. the red colour denotes the variance which is the diagonal of the matrix. Na is the value of the variance. 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. type the error message,ssmodel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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