diff --git a/.Rbuildignore b/.Rbuildignore index abced86..f9af947 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,6 +1,7 @@ ^dfms\.Rproj$ ^\.Rproj\.user$ misc +^README\.html$ .RData LICENSE ^\.github$ diff --git a/.gitignore b/.gitignore index 25e33b2..5dd5866 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,4 @@ vignettes/*.pdf .Rproj.user # docs inst/doc +docs diff --git a/NAMESPACE b/NAMESPACE index cf878dc..ac182c6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,7 +2,9 @@ S3method(as.data.frame,dfm) S3method(as.data.frame,dfm_forecast) +S3method(coef,dfm) S3method(fitted,dfm) +S3method(logLik,dfm) S3method(plot,ICr) S3method(plot,dfm) S3method(plot,dfm_forecast) diff --git a/R/DFM.R b/R/DFM.R index 3582d86..ab148cc 100644 --- a/R/DFM.R +++ b/R/DFM.R @@ -11,14 +11,14 @@ #' Efficient estimation of a Dynamic Factor Model via the EM Algorithm - on stationary data #' with time-invariant system matrices and classical assumptions, while permitting missing data. #' -#' @param X a \code{T x n} numeric data matrix or frame of stationary time series. May contain missing values. -#' @param r integer. number of factors. -#' @param p integer. number of lags in factor VAR. -#' @param \dots (optional) arguments to \code{\link{tsnarmimp}}. -#' @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. +#' @param X a \code{T x n} numeric data matrix or frame of stationary time series. May contain missing values. \emph{Note} that data is internally standardized (scaled and centered) before estimation. +#' @param r integer. Number of factors. +#' @param p integer. Number of lags in factor VAR. +#' @param \dots (optional) arguments to \code{\link{tsnarmimp}}. The default settings impute internal missing values with a cubic spline and the edges with the median and a 3-period moving average. +#' @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 generally not needed if \code{n} is large (>30). See theoretical vignette for details. #' @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. -#' @param rQ character. restrictions on the state (transition) covariance matrix (Q). -#' @param rR character. restrictions on the observation (measurement) covariance matrix (R). +#' @param rQ character. Restrictions on the state (transition) covariance matrix (Q). +#' @param rR character. Restrictions on the observation (measurement) covariance matrix (R). #' @param em.method character. The implementation of the Expectation Maximization Algorithm used. The options are: #' \tabular{llll}{ #' \code{"auto"} \tab\tab Automatic selection: \code{"BM"} if \code{anyNA(X)}, else \code{"DGR"}. \cr\cr @@ -77,9 +77,11 @@ #' \eqn{\textbf{R}}{R} \tab\tab \eqn{n \times n}{n x n} observation covariance matrix. It is diagonal by assumption 2 and identical to \eqn{\textbf{R}}{R} as stated in the dynamic form.\cr\cr #' } # that \eqn{E[\textbf{f}_t|\textbf{F}_{t-1}] = E[\textbf{f}_t|\textbf{f}_{t-1}] = \textbf{A}_1 \textbf{f}_{t-1}}{E[f(t)|F(t-1)] = E[f(t)|f(t-1)] = A1 f(t-1)} (all relationships between lagged factors are captured in \eqn{\textbf{A}_1}{A1}).\cr\cr +#' The filter is initialized with PCA estimates on the imputed dataset---see \code{\link{SKFS}} for a complete code example. +#' #' #' @returns A list-like object of class 'dfm' with the following elements: -#' \item{\code{X_imp}}{\eqn{T \times n}{T x n} matrix with the imputed and standardized (scaled and centered) data - with attributes attached allowing reconstruction of the original data: +#' \item{\code{X_imp}}{\eqn{T \times n}{T x n} matrix with the imputed and standardized (scaled and centered) data---after applying \code{\link{tsnarmimp}}. It has attributes attached allowing for reconstruction of the original data: #' \tabular{llll}{ #' \code{"stats"} \tab\tab is a \eqn{n \times 5}{n x 5} matrix of summary statistics of class \code{"qsu"} (see \code{\link[collapse]{qsu}}).\cr\cr #' \code{"missing"} \tab\tab is a \eqn{T \times n}{T x n} logical matrix indicating missing or infinite values in the original data (which are imputed in \code{X_imp}).\cr\cr @@ -104,6 +106,7 @@ #' \item{\code{converged}}{single logical valued indicating whether the EM algorithm converged (within \code{max.iter} iterations subject to \code{tol}).} #' \item{\code{anyNA}}{single logical valued indicating whether there were any (internal) missing values in the data (determined after removal of rows with too many missing values). If \code{FALSE}, \code{X_imp} is simply the original data in matrix form, and does not have the \code{"missing"} attribute attached.} #' \item{\code{rm.rows}}{vector of any cases (rows) that were removed beforehand (subject to \code{max.missing} and \code{na.rm.method}). If no cases were removed the slot is \code{NULL}. } +#' \item{\code{quarterly.vars}}{names of the quarterly variables (if any).} #' \item{\code{em.method}}{The EM method used.} #' \item{\code{call}}{call object obtained from \code{match.call()}.} #' diff --git a/R/dfms.R b/R/dfms.R index db5dfc6..f6aecf2 100644 --- a/R/dfms.R +++ b/R/dfms.R @@ -2,9 +2,9 @@ #' #' @description #' -#' *dfms* provides efficient estimation of Dynamic Factor Models via the EM Algorithm --- following Doz, Giannone & Reichlin (2011, 2012) and Banbura & Modugno (2014). The package has the following contents: +#' *dfms* provides efficient estimation of Dynamic Factor Models via the EM Algorithm --- following Doz, Giannone & Reichlin (2011, 2012) and Banbura & Modugno (2014). Contents: #' -#' **Information Criteria** +#' **Information Criteria to Determine the Number of Factors** #' #' \code{\link[=ICr]{ICr()}}\cr #' diff --git a/R/methods.R b/R/methods.R index 78a1722..30ebfe2 100644 --- a/R/methods.R +++ b/R/methods.R @@ -43,8 +43,8 @@ #' #' @title DFM Summary Methods #' -#' @description Summary and print methods for class 'dfm'. \code{print.dfm} just prints basic model information and the factor transition matrix \eqn{\textbf{A}}{A}, -#' \code{summary.dfm} returns all system matrices and additional residual and goodness of fit statistics - with a print method allowing full or compact printout. +#' @description Summary and print methods for class 'dfm'. \code{print.dfm} just prints basic model information and the factor transition matrix \eqn{\textbf{A}}{A}, \code{coef.dfm} returns \eqn{\textbf{A}}{A} and \eqn{\textbf{C}}{C} in a plain list, whereas +#' \code{summary.dfm} returns all system matrices and additional residual and goodness of fit statistics---with a print method allowing full or compact printout. #' #' @param x,object an object class 'dfm'. #' @param digits integer. The number of digits to print out. @@ -73,10 +73,18 @@ print.dfm <- function(x, digits = 4L, ...) { return(invisible(x)) } +#' @rdname summary.dfm +#' @export +coef.dfm <- function(object, ...) list(A = object$A, C = object$C) + +#' @rdname summary.dfm +#' @export +logLik.dfm <- function(object, ...) object$loglik[length(object$loglik)] + #' @rdname summary.dfm #' @param method character. The factor estimates to use: one of \code{"qml"}, \code{"2s"} or \code{"pca"}. #' @param \dots not used. -#' @return Summary information following a dynamic factor model estimation. +#' @return Summary information following a dynamic factor model estimation. \code{coef()} returns \eqn{\textbf{A}}{A} and \eqn{\textbf{C}}{C}. #' @importFrom stats cov #' @importFrom collapse pwcov #' @export diff --git a/R/my_RcppExports.R b/R/my_RcppExports.R index 7080c2d..1087352 100644 --- a/R/my_RcppExports.R +++ b/R/my_RcppExports.R @@ -4,7 +4,7 @@ Estep <- function(X, A, C, Q, R, F_0, P_0) { #' (Fast) Stationary Kalman Filter #' -#' @description A simple and fast C++ implementation of the Kalman Filter for stationary data with time-invariant system matrices and missing data. +#' @description A simple and fast C++ implementation of the Kalman Filter for stationary data (or random walks - data should be mean zero and without a trend) with time-invariant system matrices and missing data. #' @param X numeric data matrix (\eqn{T \times n}{T x n}). #' @param A transition matrix (\eqn{rp \times rp}{rp x rp}). #' @param C observation matrix (\eqn{n \times rp}{n x rp}). @@ -103,7 +103,7 @@ FIS <- function(A, F, F_pred, P, P_pred, F_0 = NULL, P_0 = NULL) { #' @inheritParams SKF #' #' @returns All results from \code{\link{SKF}} and \code{\link{FIS}}, and additionally -#' a \eqn{rp \times rp \times T}{rp x rp x T} matrix \code{PPm_smooth}, which is equal to the estimate of \eqn{Cov(F^smooth_t, F^smooth_{t-1} | T)}{Cov(F_smooth(t), F_smooth(t-1) | T)} and needed for EM iterations. +#' a \eqn{rp \times rp \times T}{rp x rp x T} matrix \code{PPm_smooth}, which is equal to the estimate of \eqn{Cov(F^{smooth}_t, F^{smooth}_{t-1} | T)}{Cov(F_smooth(t), F_smooth(t-1) | T)} and needed for EM iterations. #' See 'Property 6.3: The Lag-One Covariance Smoother' in Shumway & Stoffer (2017). #' #' diff --git a/_pkgdown.yml b/_pkgdown.yml index dce20c7..ee385e6 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -93,10 +93,10 @@ external-articles: articles: - title: Introduction to dfms - desc: Provides a walk-through of all main features + desc: Introduces the package, including a walk-through of all main features. contents: - introduction - title: Dynamic Factor Models - A Very Short Introduction - desc: Provides a short theoretical overview of dynamics factor models as used in the economics literature + desc: Provides a short overview of dynamics factor models as used in the economics literature. contents: - dynamic_factor_models diff --git a/man/DFM.Rd b/man/DFM.Rd index 2f4a277..f316666 100644 --- a/man/DFM.Rd +++ b/man/DFM.Rd @@ -22,21 +22,21 @@ DFM( ) } \arguments{ -\item{X}{a \code{T x n} numeric data matrix or frame of stationary time series. May contain missing values.} +\item{X}{a \code{T x n} numeric data matrix or frame of stationary time series. May contain missing values. \emph{Note} that data is internally standardized (scaled and centered) before estimation.} -\item{r}{integer. number of factors.} +\item{r}{integer. Number of factors.} -\item{p}{integer. number of lags in factor VAR.} +\item{p}{integer. Number of lags in factor VAR.} -\item{\dots}{(optional) arguments to \code{\link{tsnarmimp}}.} +\item{\dots}{(optional) arguments to \code{\link{tsnarmimp}}. The default settings impute internal missing values with a cubic spline and the edges with the median and a 3-period moving average.} -\item{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.} +\item{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 generally not needed if \code{n} is large (>30). See theoretical vignette for details.} \item{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.} -\item{rQ}{character. restrictions on the state (transition) covariance matrix (Q).} +\item{rQ}{character. Restrictions on the state (transition) covariance matrix (Q).} -\item{rR}{character. restrictions on the observation (measurement) covariance matrix (R).} +\item{rR}{character. Restrictions on the observation (measurement) covariance matrix (R).} \item{em.method}{character. The implementation of the Expectation Maximization Algorithm used. The options are: \tabular{llll}{ @@ -60,7 +60,7 @@ This yields significant performance gains over the iterative methods. Final syst } \value{ A list-like object of class 'dfm' with the following elements: -\item{\code{X_imp}}{\eqn{T \times n}{T x n} matrix with the imputed and standardized (scaled and centered) data - with attributes attached allowing reconstruction of the original data: +\item{\code{X_imp}}{\eqn{T \times n}{T x n} matrix with the imputed and standardized (scaled and centered) data---after applying \code{\link{tsnarmimp}}. It has attributes attached allowing for reconstruction of the original data: \tabular{llll}{ \code{"stats"} \tab\tab is a \eqn{n \times 5}{n x 5} matrix of summary statistics of class \code{"qsu"} (see \code{\link[collapse]{qsu}}).\cr\cr \code{"missing"} \tab\tab is a \eqn{T \times n}{T x n} logical matrix indicating missing or infinite values in the original data (which are imputed in \code{X_imp}).\cr\cr @@ -85,6 +85,7 @@ A list-like object of class 'dfm' with the following elements: \item{\code{converged}}{single logical valued indicating whether the EM algorithm converged (within \code{max.iter} iterations subject to \code{tol}).} \item{\code{anyNA}}{single logical valued indicating whether there were any (internal) missing values in the data (determined after removal of rows with too many missing values). If \code{FALSE}, \code{X_imp} is simply the original data in matrix form, and does not have the \code{"missing"} attribute attached.} \item{\code{rm.rows}}{vector of any cases (rows) that were removed beforehand (subject to \code{max.missing} and \code{na.rm.method}). If no cases were removed the slot is \code{NULL}. } +\item{\code{quarterly.vars}}{names of the quarterly variables (if any).} \item{\code{em.method}}{The EM method used.} \item{\code{call}}{call object obtained from \code{match.call()}.} } @@ -133,6 +134,7 @@ where \eqn{\textbf{Q}}{Q} \tab\tab \eqn{rp \times rp}{rp x rp} state covariance matrix. The top \eqn{r \times r}{r x r} part gives the contemporaneous relationships, the rest are zeros by assumption 4.\cr\cr \eqn{\textbf{R}}{R} \tab\tab \eqn{n \times n}{n x n} observation covariance matrix. It is diagonal by assumption 2 and identical to \eqn{\textbf{R}}{R} as stated in the dynamic form.\cr\cr } +The filter is initialized with PCA estimates on the imputed dataset---see \code{\link{SKFS}} for a complete code example. } \examples{ library(magrittr) diff --git a/man/SKF.Rd b/man/SKF.Rd index c824cbe..3e67744 100644 --- a/man/SKF.Rd +++ b/man/SKF.Rd @@ -32,7 +32,7 @@ Predicted and filtered state vectors and covariances. \item{\code{loglik}}{value of the log likelihood. } } \description{ -A simple and fast C++ implementation of the Kalman Filter for stationary data with time-invariant system matrices and missing data. +A simple and fast C++ implementation of the Kalman Filter for stationary data (or random walks - data should be mean zero and without a trend) with time-invariant system matrices and missing data. } \details{ The underlying state space model is: diff --git a/man/SKFS.Rd b/man/SKFS.Rd index 0a6bdce..1f203aa 100644 --- a/man/SKFS.Rd +++ b/man/SKFS.Rd @@ -25,7 +25,7 @@ SKFS(X, A, C, Q, R, F_0, P_0, loglik = FALSE) } \value{ All results from \code{\link{SKF}} and \code{\link{FIS}}, and additionally -a \eqn{rp \times rp \times T}{rp x rp x T} matrix \code{PPm_smooth}, which is equal to the estimate of \eqn{Cov(F^smooth_t, F^smooth_{t-1} | T)}{Cov(F_smooth(t), F_smooth(t-1) | T)} and needed for EM iterations. +a \eqn{rp \times rp \times T}{rp x rp x T} matrix \code{PPm_smooth}, which is equal to the estimate of \eqn{Cov(F^{smooth}_t, F^{smooth}_{t-1} | T)}{Cov(F_smooth(t), F_smooth(t-1) | T)} and needed for EM iterations. See 'Property 6.3: The Lag-One Covariance Smoother' in Shumway & Stoffer (2017). } \description{ diff --git a/man/dfms-package.Rd b/man/dfms-package.Rd index fafe4ff..94cbee3 100644 --- a/man/dfms-package.Rd +++ b/man/dfms-package.Rd @@ -6,9 +6,9 @@ \alias{dfms} \title{Dynamic Factor Models} \description{ -\emph{dfms} provides efficient estimation of Dynamic Factor Models via the EM Algorithm --- following Doz, Giannone & Reichlin (2011, 2012) and Banbura & Modugno (2014). The package has the following contents: +\emph{dfms} provides efficient estimation of Dynamic Factor Models via the EM Algorithm --- following Doz, Giannone & Reichlin (2011, 2012) and Banbura & Modugno (2014). Contents: -\strong{Information Criteria} +\strong{Information Criteria to Determine the Number of Factors} \code{\link[=ICr]{ICr()}}\cr \itemize{ diff --git a/man/summary.dfm.Rd b/man/summary.dfm.Rd index 9d1be87..8e88ac9 100644 --- a/man/summary.dfm.Rd +++ b/man/summary.dfm.Rd @@ -4,10 +4,16 @@ \alias{summary.dfm} \alias{print.dfm} \alias{print.dfm_summary} +\alias{coef.dfm} +\alias{logLik.dfm} \title{DFM Summary Methods} \usage{ \method{print}{dfm}(x, digits = 4L, ...) +\method{coef}{dfm}(object, ...) + +\method{logLik}{dfm}(object, ...) + \method{summary}{dfm}(object, method = switch(object$em.method, none = "2s", "qml"), ...) \method{print}{dfm_summary}(x, digits = 4L, compact = sum(x$info["n"] > 15, x$info["n"] > 40), ...) @@ -24,11 +30,11 @@ \item{compact}{integer. Display a more compact printout: \code{0} prints everything, \code{1} omits the observation matrix \eqn{\textbf{C}}{C} and residual covariance matrix \code{cov(resid(model))}, and \code{2} omits all disaggregated information on the input data. Sensible default are chosen for different sizes of the input dataset so as to limit large printouts.} } \value{ -Summary information following a dynamic factor model estimation. +Summary information following a dynamic factor model estimation. \code{coef()} returns \eqn{\textbf{A}}{A} and \eqn{\textbf{C}}{C}. } \description{ -Summary and print methods for class 'dfm'. \code{print.dfm} just prints basic model information and the factor transition matrix \eqn{\textbf{A}}{A}, -\code{summary.dfm} returns all system matrices and additional residual and goodness of fit statistics - with a print method allowing full or compact printout. +Summary and print methods for class 'dfm'. \code{print.dfm} just prints basic model information and the factor transition matrix \eqn{\textbf{A}}{A}, \code{coef.dfm} returns \eqn{\textbf{A}}{A} and \eqn{\textbf{C}}{C} in a plain list, whereas +\code{summary.dfm} returns all system matrices and additional residual and goodness of fit statistics---with a print method allowing full or compact printout. } \examples{ mod = DFM(diff(BM14_Q), 2, 3) diff --git a/vignettes/introduction.Rmd b/vignettes/introduction.Rmd index a94d777..57c7e9f 100644 --- a/vignettes/introduction.Rmd +++ b/vignettes/introduction.Rmd @@ -88,15 +88,15 @@ Estimation can then simply be done using the `DFM()` function with parameters `r ```{r} # Estimating the model with 4 factors and 3 lags using BM14's EM algorithm -model1 <- DFM(BM14_M_diff, r = 4, p = 3) -print(model1) -plot(model1) +model_m <- DFM(BM14_M_diff, r = 4, p = 3) +print(model_m) +plot(model_m) ``` The model can be investigated using `summary()`, which returns an object of class 'dfm_summary' containing the system matrices and summary statistics of the factors and the residuals in the measurement equation, as well as the R-Squared of the factor model for individual series. The print method automatically adjusts the amount of information printed to the data size. For large databases with more than 40 series, no series-level statistics are printed. ```{r} -dfm_summary <- summary(model1) +dfm_summary <- summary(model_m) print(dfm_summary) # Large model with > 40 series: defaults to compact = 2 # Can request more detailed printouts @@ -107,8 +107,8 @@ print(dfm_summary) # Large model with > 40 series: defaults to compact = 2 Apart from the model summary, the *dfm* methods `residuals()` and `fitted()` return observation residuals and fitted values from the model. The default format is a plain matrix, but the functions also have an argument to return data in the original (input) format. ```{r} -plot(resid(model1, orig.format = TRUE)) -plot(fitted(model1, orig.format = TRUE)) +plot(resid(model_m, orig.format = TRUE)) +plot(fitted(model_m, orig.format = TRUE)) ``` @@ -116,25 +116,25 @@ plot(fitted(model1, orig.format = TRUE)) Another way to examine the factor model visually is to plot the Quasi-Maximum-Likelihood (QML) factor estimates against PCA and Two-Step estimates following Doz, Giannone and Reichlin (2011)^[Doz, C., Giannone, D., & Reichlin, L. (2011). A two-step estimator for large approximate dynamic factor models based on Kalman filtering. *Journal of Econometrics, 164*(1), 188-205.], where the Kalman Filter and Smoother is run only once. Both estimates are also computed by `DFM()` during EM estimation and can also be visualized with `plot.dfm`. ```{r} -plot(model1, method = "all", type = "individual") +plot(model_m, method = "all", type = "individual") ``` -The plot with the various estimates shows that the QML estimates are more volatile in the initial periods where there are many missing series, but less volatile in the latter periods. In general, QML estimates may now always be superior across the entire data range to Two-Step and PCA estimates. Often, Two-Step estimates also provide similar forecasting performance, and are much faster to estimate using `DFM(BM14_M_diff, r = 4, p = 3, em.method = "none")`. +The plot with the various estimates shows that the QML estimates are more volatile in the initial periods where there are many missing series, but less volatile in the latter periods. In general, QML estimates may not always be superior across the entire data range to Two-Step and PCA estimates. Often, Two-Step estimates also provide similar forecasting performance, and are much faster to estimate using `DFM(BM14_M_diff, r = 4, p = 3, em.method = "none")`. The factor estimates themselves can be extracted in a data frame using `as.data.frame()`, which also provides various options regarding the estimates retained and the format of the frame. It is also possible to add a time variable from the original data (the default is a sequence of integers). ```{r} # Default: all estimates in long format -head(as.data.frame(model1, time = index(BM14_M_diff))) +head(as.data.frame(model_m, time = index(BM14_M_diff))) ``` ## Forecasting -DFM forecasts can be obtained with the `predict()` method, which dynamically forecasts the factors using the transition equation (default 10 periods), and then also predicts data forecasts using the observation equation. Objects are of class 'dfm_forecast' +DFM forecasts can be obtained with the `predict()` method, which dynamically forecasts the factors using the transition equation (default 10 periods), and then also predicts data forecasts using the observation equation. Objects are of class 'dfm_forecast'. ```{r} # 12-period ahead DFM forecast -fc <- predict(model1, h = 12) +fc <- predict(model_m, h = 12) print(fc) ``` @@ -142,12 +142,11 @@ These forecasts can also be visualized using a plot method. By default the entir ```{r} # Setting an appropriate plot range to see the forecast plot(fc, xlim = c(320, 370)) -# Predicting with Two-Step estimates -# plot(predict(model1, h = 12, method = "2s"), xlim = c(320, 370)) ``` +By default, `predict()` uses the QML factor estimates (if available). We can however also predict with PCA or TwoStep estimates using, e.g., `predict(model_m, h = 12, method = "2s")`. -The forecasts can also be retrieved in data frame using `as.data.frame()`. Again the method has various arguments to control the forecasts retained (factors, data or both, default factors), and the format of the frame. +The forecasts can be retrieved in data frame using `as.data.frame()`. Again the method has various arguments to control the output (factors, data, or both --- default factors) and the format of the frame. ```{r} # Factor forecasts in wide format @@ -156,7 +155,7 @@ head(as.data.frame(fc, pivot = "wide")) ## Estimation with Mixed Frequency -Since v0.3.0 *dfms* allows monthly and quarterly mixed frequency estimation following Mariano & Murasawa (2003) and Banbura & Modugno (2014). Quarterly variables should be to the right of the monthly variables in the data matrix and need to be indicated using the `quarterly.vars` argument. Quarterly observations should be provided every 3rd period. +Since v0.3.0, *dfms* allows monthly and quarterly mixed frequency estimation following Mariano & Murasawa (2003) and Banbura & Modugno (2014). Quarterly variables should be to the right of the monthly variables in the data matrix and need to be indicated using the `quarterly.vars` argument. Quarterly observations should be provided every 3rd period (months 3, 6, 9, and 12). Below, I estimate the mixed frequency DFM, adding a factor to capture any idiosynchratic dynamics in the quarterly series. -```{r, eval = FALSE, include=FALSE} +```{r} # Quarterly series from BM14 head(BM14_Q, 3) # Pre-processing the data -BM14_Q[, BM14_Models[BM14_Models$freq == "Q", ]$log_trans] %<>% log() +BM14_Q[, BM14_Models$log_trans[BM14_Models$freq == "Q"]] %<>% log() BM14_Q_diff <- diff(BM14_Q) # Merging to monthly data -BM14_diff <- cbind(BM14_M_diff, BM14_Q_diff) +BM14_diff <- merge(BM14_M_diff, BM14_Q_diff) # Estimating the model with 5 factors and 3 lags using BM14's EM algorithm -model2 <- DFM(BM14_diff, r = 5, p = 3, quarterly.vars = colnames(BM14_Q)) -print(model2) -plot(model2) +model_mq <- DFM(BM14_diff, r = 5, p = 3, quarterly.vars = colnames(BM14_Q)) +print(model_mq) +plot(model_mq) ```