|
2 | 2 | .EM_DGR <- quote(EMstepDGR(X, A, C, Q, R, F_0, P_0, cpX, n, r, sr, TT, rQi, rRi)) |
3 | 3 | .EM_BM <- quote(EMstepBMOPT(X, A, C, Q, R, F_0, P_0, XW0, W, n, r, sr, TT, dgind, dnkron, dnkron_ind, rQi, rRi)) |
4 | 4 | .EM_BM_idio <- quote(EMstepBMidio(X, A, C, Q, R, F_0, P_0, XW0, W, dgind, dnkron, dnkron_ind, r, p, n, sr, TT, rQi, rRi)) |
| 5 | +.EMstepBMMQ <- quote(EMstepBMMQ(X, A, C, Q, R, F_0, P_0, XW0, NW, dgind, dnkron, dnkron_ind, r, p, R_mat, n, nq, sr, TT, rQi, rRi)) |
5 | 6 | .KFS <- quote(SKFS(X, A, C, Q, R, F_0, P_0)) |
6 | 7 |
|
7 | 8 |
|
|
15 | 16 | #' @param p integer. number of lags in factor VAR. |
16 | 17 | #' @param \dots (optional) arguments to \code{\link{tsnarmimp}}. |
17 | 18 | #' @param idio.ar1 logical. Model observation errors as AR(1) processes: \eqn{e_t = \rho e_{t-1} + v_t}{e(t) = rho e(t-1) + v(t)}. \emph{Note} that this substantially increases computation time, and is generaly not needed if \code{n} is large (>30). See theoretical vignette for details. |
| 19 | +#' @param quarterly.vars character. Names of quarterly variables in \code{X} (if any). Monthly variables should be to the left of the quarterly variables in the data matrix and quarterly observations should be provided every 3rd period. |
18 | 20 | #' @param rQ character. restrictions on the state (transition) covariance matrix (Q). |
19 | 21 | #' @param rR character. restrictions on the observation (measurement) covariance matrix (R). |
20 | 22 | #' @param em.method character. The implementation of the Expectation Maximization Algorithm used. The options are: |
|
115 | 117 | #' Stock, J. H., & Watson, M. W. (2016). Dynamic Factor Models, Factor-Augmented Vector Autoregressions, and Structural Vector Autoregressions in Macroeconomics. \emph{Handbook of Macroeconomics, 2}, 415–525. https://doi.org/10.1016/bs.hesmac.2016.04.002 |
116 | 118 | #' |
117 | 119 | #' @useDynLib dfms, .registration = TRUE |
118 | | -#' @importFrom collapse fscale qsu fvar fmedian fmedian.default qM unattrib na_omit %=% %-=% %+=% %/=% %*=% %r*% whichv setColnames setDimnames |
| 120 | +#' @importFrom collapse fscale qsu fvar flag fmedian fmedian.default qM unattrib na_omit %=% %-=% %+=% %/=% %*=% %r*% whichv whichNA vec setColnames setDimnames |
119 | 121 | #' @importFrom grDevices rainbow |
120 | 122 | #' @importFrom graphics abline legend lines par |
121 | 123 | #' @importFrom stats filter residuals rnorm spline ts.plot |
@@ -312,6 +314,7 @@ DFM <- function(X, r, p = 1L, ..., |
312 | 314 | P_2s = setDimnames(kfs_res$P_smooth[sr, sr,, drop = FALSE], list(fnam, fnam, NULL)), |
313 | 315 | anyNA = anymiss, # || length(rm.rows), # This is for internal missing values only |
314 | 316 | rm.rows = rm.rows, |
| 317 | + quarterly.vars = quarterly.vars, |
315 | 318 | em.method = if(is.na(BMl)) "none" else if(BMl) "BM" else "DGR", |
316 | 319 | call = match.call()) |
317 | 320 |
|
@@ -347,11 +350,22 @@ DFM <- function(X, r, p = 1L, ..., |
347 | 350 | converged <- FALSE |
348 | 351 |
|
349 | 352 | if(BMl) { |
350 | | - expr <- if(idio.ar1) .EM_BM_idio else .EM_BM |
351 | | - dnkron <- matrix(1, r, r) %x% diag(n) # Used to be inside EMstep, taken out to speed up the algorithm |
352 | | - dnkron_ind <- whichv(dnkron, 1) |
353 | | - XW0 <- X_imp |
| 353 | + if(MFl) { |
| 354 | + nm <- n - nq |
| 355 | + rpC <- r * max(p, 5L) |
| 356 | + rpC1nq <- (rpC+1L):(rpC+nq) |
| 357 | + NW <- !W |
| 358 | + R_mat <- kronecker(Rcon, diag(r)) # TODO: autsource this |
| 359 | + expr <- if(idio.ar1) stop("not implemented yet") else .EMstepBMMQ |
| 360 | + dnkron <- matrix(1, r, r) %x% diag(nm) # Used to be inside EMstep, taken out to speed up the algorithm |
| 361 | + dnkron_ind <- whichv(dnkron, 1) |
| 362 | + } else { |
| 363 | + expr <- if(idio.ar1) .EM_BM_idio else .EM_BM |
| 364 | + dnkron <- matrix(1, r, r) %x% diag(n) # Used to be inside EMstep, taken out to speed up the algorithm |
| 365 | + dnkron_ind <- whichv(dnkron, 1) |
| 366 | + } |
354 | 367 | dgind <- 0:(n-1) * n + 1:n |
| 368 | + XW0 <- X_imp |
355 | 369 | if(anymiss) XW0[W] <- 0 else W <- is.na(X) # TODO: think about this... |
356 | 370 | } else { |
357 | 371 | expr <- .EM_DGR |
@@ -391,7 +405,9 @@ DFM <- function(X, r, p = 1L, ..., |
391 | 405 | A = setDimnames(em_res$A[sr, seq_len(rp), drop = FALSE], lagnam(fnam, p)), |
392 | 406 | C = setDimnames(em_res$C[, sr, drop = FALSE], list(Xnam, fnam)), |
393 | 407 | Q = setDimnames(em_res$Q[sr, sr, drop = FALSE], list(unam, unam)), |
394 | | - R = setDimnames(if(idio.ar1) em_res$Q[-seq_len(rp), -seq_len(rp), drop = FALSE] else em_res$R, list(Xnam, Xnam)), |
| 408 | + R = setDimnames(if(MFl && idio.ar1) stop("not implemented yet") else if(MFl) |
| 409 | + `[<-`(em_res$R, (nm+1):n, (nm+1):n, value = em_res$Q[rpC1nq, rpC1nq]) else if(idio.ar1) |
| 410 | + em_res$Q[-seq_len(rp), -seq_len(rp), drop = FALSE] else em_res$R, list(Xnam, Xnam)), |
395 | 411 | e = if(idio.ar1) setColnames(kfs_res$F_smooth[, -seq_len(rp), drop = FALSE], Xnam) else NULL, |
396 | 412 | rho = if(idio.ar1) setNames(diag(em_res$A)[-seq_len(rp)], Xnam) else NULL, |
397 | 413 | loglik = if(num_iter == max.iter) loglik_all else loglik_all[seq_len(num_iter)], |
|
0 commit comments