Skip to content

Commit 3320175

Browse files
authored
Merge pull request #67 from SebKrantz/development
Development
2 parents 078108c + 116eae7 commit 3320175

File tree

9 files changed

+90
-75
lines changed

9 files changed

+90
-75
lines changed

DESCRIPTION

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ Version: 0.3.0
33
Title: Dynamic Factor Models
44
Authors@R: c(person("Sebastian", "Krantz", role = c("aut", "cre"), email = "[email protected]"),
55
person("Rytis", "Bagdziunas", role = "aut"),
6-
person("Santtu", "Tikka", role = "rev"))
6+
person("Santtu", "Tikka", role = "rev"),
7+
person("Eli", "Holmes", role = "rev"))
78
Description: Efficient estimation of Dynamic Factor Models using the Expectation Maximization (EM) algorithm
8-
or Two-Step (2S) estimation, supporting datasets with missing data. The estimation options follow advances in the
9-
econometric literature: either running the Kalman Filter and Smoother once with initial values from PCA -
10-
2S estimation as in Doz, Giannone and Reichlin (2011) <doi:10.1016/j.jeconom.2011.02.012> - or via iterated
11-
Kalman Filtering and Smoothing until EM convergence - following Doz, Giannone and Reichlin (2012)
12-
<doi:10.1162/REST_a_00225> - or using the adapted EM algorithm of Banbura and Modugno (2014) <doi:10.1002/jae.2306>,
13-
allowing arbitrary patterns of missing data. The implementation makes heavy use of the 'Armadillo' 'C++' library and
14-
the 'collapse' package, providing for particularly speedy estimation. A comprehensive set of methods supports
15-
interpretation and visualization of the model as well as forecasting. Information criteria to choose the number
16-
of factors are also provided - following Bai and Ng (2002) <doi:10.1111/1468-0262.00273>.
9+
or Two-Step (2S) estimation, supporting datasets with missing data. Factors are assumed to follow a stationary VAR
10+
process of order p. The estimation options follow advances in the econometric literature: either running the Kalman
11+
Filter and Smoother once with initial values from PCA - 2S estimation as in Doz, Giannone and Reichlin (2011)
12+
<doi:10.1016/j.jeconom.2011.02.012> - or via iterated Kalman Filtering and Smoothing until EM convergence - following
13+
Doz, Giannone and Reichlin (2012) <doi:10.1162/REST_a_00225> - or using the adapted EM algorithm of Banbura and
14+
Modugno (2014) <doi:10.1002/jae.2306>, allowing arbitrary patterns of missing data. The implementation makes heavy
15+
use of the 'Armadillo' 'C++' library and the 'collapse' package, providing for particularly speedy estimation.
16+
A comprehensive set of methods supports interpretation and visualization of the model as well as forecasting.
17+
Information criteria to choose the number of factors are also provided - following Bai and Ng (2002)
18+
<doi:10.1111/1468-0262.00273>.
1719
URL: https://sebkrantz.github.io/dfms/
1820
BugReports: https://github.com/SebKrantz/dfms/issues
1921
Depends: R (>= 3.5.0)

R/DFM.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@
147147
#' VARselect(IC_small$F_pca[, 1:2])
148148
#'
149149
#' # Estimating the model with 2 factors and 3 lags
150-
#' dfm_small = DFM(BM14[, BM14_Models$small], 2, 3)
150+
#' dfm_small = DFM(BM14[, BM14_Models$small], r = 2, p = 3,
151+
#' quarterly.vars = BM14_Models %$% series[freq == "Q" & small])
151152
#'
152153
#' # Inspecting the model
153154
#' summary(dfm_small)
@@ -170,7 +171,8 @@
170171
#' VARselect(IC_medium$F_pca[, 1:3])
171172
#'
172173
#' # Estimating the model with 3 factors and 3 lags
173-
#' dfm_medium = DFM(BM14[, BM14_Models$medium], 3, 3)
174+
#' dfm_medium = DFM(BM14[, BM14_Models$medium], r = 3, p = 3,
175+
#' quarterly.vars = BM14_Models %$% series[freq == "Q" & medium])
174176
#'
175177
#' # Inspecting the model
176178
#' summary(dfm_medium)
@@ -193,7 +195,8 @@
193195
#' VARselect(IC_large$F_pca[, 1:6])
194196
#'
195197
#' # Estimating the model with 6 factors and 3 lags
196-
#' dfm_large = DFM(BM14, 6, 3)
198+
#' dfm_large = DFM(BM14, r = 6, p = 3,
199+
#' quarterly.vars = BM14_Models %$% series[freq == "Q"])
197200
#'
198201
#' # Inspecting the model
199202
#' summary(dfm_large)

R/dfms.R

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,60 @@
11
#' Dynamic Factor Models
22
#'
3-
#' *dfms* provides efficient estimation of Dynamic Factor Models via the EM Algorithm.
3+
#' @description
44
#'
5-
#' Estimation can be done in 3 different ways following:
5+
#' *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:
66
#'
7-
#' - 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. <doi:10.1016/j.jeconom.2011.02.012>
7+
#' **Information Criteria**
88
#'
9-
#' - Doz, C., Giannone, D., & Reichlin, L. (2012). A quasi-maximum likelihood approach for large, approximate dynamic factor models. *Review of Economics and Statistics, 94*(4), 1014-1024. <doi:10.1162/REST_a_00225>
10-
#'
11-
#' - Banbura, M., & Modugno, M. (2014). Maximum likelihood estimation of factor models on datasets with arbitrary pattern of missing data. *Journal of Applied Econometrics, 29*(1), 133-160. <doi:10.1002/jae.2306>
12-
#'
13-
#' The default is `em.method = "auto"`, which chooses `"BM"` following Banbura & Modugno (2014) with missing data or mixed frequency, and `"DGR"` following Doz, Giannone & Reichlin (2012) otherwise. Using `em.method = "none"` generates Two-Step estimates following Doz, Giannone & Reichlin (2011). This is extremely efficient on bigger datasets. PCA and Two-Step estimates are also reported in EM-estimation. All methods support missing data, but `em.method = "DGR"` does not model them in EM iterations.
14-
#'
15-
#' @section Package Contents:
16-
#'
17-
#' **Functions to Specify/Estimate Model and Key Methods**
18-
#'
19-
#' \code{\link[=ICr]{ICr()}} --- Information Criteria\cr
9+
#' \code{\link[=ICr]{ICr()}}\cr
2010
#'
2111
#' - \code{\link[=plot.ICr]{plot(<ICr>)}}\cr
2212
#' - \code{\link[=screeplot.ICr]{screeplot(<ICr>)}}\cr
2313
#'
24-
#' \code{\link[=DFM]{DFM()}} --- Estimate the Model\cr
14+
#' **Fit a Dynamic Factor Model**
15+
#'
16+
#' \code{\link[=DFM]{DFM()}}\cr
2517
#'
2618
#' - \code{\link[=summary.dfm]{summary(<dfm>)}}\cr
2719
#' - \code{\link[=plot.dfm]{plot(<dfm>)}}\cr
2820
#' - \code{\link[=as.data.frame.dfm]{as.data.frame(<dfm>)}}\cr
2921
#' - \code{\link[=residuals.dfm]{residuals(<dfm>)}}\cr
3022
#' - \code{\link[=fitted.dfm]{fitted(<dfm>)}}
3123
#'
32-
#' \code{\link[=predict.dfm]{predict(<dfm>)}} --- Generate Forecasts\cr
24+
#' **Generate Forecasts**
25+
#'
26+
#' \code{\link[=predict.dfm]{predict(<dfm>)}}\cr
3327
#'
3428
#' - \code{\link[=plot.dfm_forecast]{plot(<dfm_forecast>)}}\cr
3529
#' - \code{\link[=as.data.frame.dfm_forecast]{as.data.frame(<dfm_forecast>)}}\cr
3630
#'
37-
#' **Auxiliary Functions**
31+
#' **Fast Stationary Kalman Filtering and Smoothing**
3832
#'
39-
#' \code{\link[=.VAR]{.VAR()}} --- Estimate Vector Autoregression\cr
4033
#' \code{\link[=SKF]{SKF()}} --- Stationary Kalman Filter\cr
4134
#' \code{\link[=FIS]{FIS()}} --- Fixed Interval Smoother\cr
4235
#' \code{\link[=SKFS]{SKFS()}} --- Stationary Kalman Filter + Smoother\cr
36+
#'
37+
#' **Helper Functions**
38+
#'
39+
#' \code{\link[=.VAR]{.VAR()}} --- (Fast) Barebones Vector-Autoregression\cr
40+
#' \code{\link[=ainv]{ainv()}} --- Armadillo's Inverse Function\cr
41+
#' \code{\link[=apinv]{apinv()}} --- Armadillo's Pseudo-Inverse Function\cr
4342
#' \code{\link[=tsnarmimp]{tsnarmimp()}} --- Remove and Impute Missing Values in a Multivariate Time Series\cr
44-
#' \code{\link[=ainv]{ainv()}} --- Rcpp Armadillo's Inverse Function\cr
45-
#' \code{\link[=apinv]{apinv()}} --- Rcpp Armadillo's Pseudo-Inverse Function\cr
43+
#' \code{\link[=em_converged]{em_converged()}} --- Convergence Test for EM-Algorithm\cr
4644
#'
4745
#' **Data**
4846
#'
4947
#' \code{\link{BM14_M}} --- Monthly Series by Banbura and Modugno (2014)\cr
5048
#' \code{\link{BM14_Q}} --- Quarterly Series by Banbura and Modugno (2014)\cr
5149
#' \code{\link{BM14_Models}} --- Series Metadata + Small/Medium/Large Model Specifications\cr
5250
#'
51+
#' @references
52+
#' 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. <doi:10.1016/j.jeconom.2011.02.012>
53+
#'
54+
#' Doz, C., Giannone, D., & Reichlin, L. (2012). A quasi-maximum likelihood approach for large, approximate dynamic factor models. *Review of Economics and Statistics, 94*(4), 1014-1024. <doi:10.1162/REST_a_00225>
55+
#'
56+
#' Banbura, M., & Modugno, M. (2014). Maximum likelihood estimation of factor models on datasets with arbitrary pattern of missing data. *Journal of Applied Econometrics, 29*(1), 133-160. <doi:10.1002/jae.2306>
57+
#'
5358
#' @docType package
5459
#' @name dfms-package
5560
#' @aliases dfms

R/methods.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ fitted.dfm <- function(object,
457457
#' @param method character. The factor estimates to use: one of \code{"qml"}, \code{"2s"} or \code{"pca"}.
458458
#' @param standardized logical. \code{FALSE} will return data forecasts on the original scale.
459459
#' @param resFUN an (optional) function to compute a univariate forecast of the residuals.
460-
#' The function needs to have a second argument providing the forecast horizon (\code{h}) and return a vector or forecasts. See Examples.
460+
#' The function needs to have a second argument providing the forecast horizon (\code{h}) and return a vector of forecasts. See Examples.
461461
#' @param resAC numeric. Threshold for residual autocorrelation to apply \code{resFUN}: only residual series where AC1 > resAC will be forecasted.
462462
#' @param \dots further arguments to \code{resFUN}.
463463
#'

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
<!--
1818
The package is fully functional though, and you are very welcome to install it using `remotes::install_github("SebKrantz/dfms")` and give feedback. -->
1919

20-
*dfms* provides efficient estimation of Dynamic Factor Models via the EM Algorithm. Estimation can be done in 3 different ways following:
20+
*dfms* provides efficient estimation of Dynamic Factor Models via the EM Algorithm. Factors are assumed to follow a stationary VAR
21+
process of order `p`. Estimation can be done in 3 different ways following:
2122

2223
- 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. <doi:10.1016/j.jeconom.2011.02.012>
2324

@@ -27,7 +28,7 @@ The package is fully functional though, and you are very welcome to install it u
2728

2829
The default is `em.method = "auto"`, which chooses `"BM"` following Banbura & Modugno (2014) with missing data or mixed frequency, and `"DGR"` following Doz, Giannone & Reichlin (2012) otherwise. Using `em.method = "none"` generates Two-Step estimates following Doz, Giannone & Reichlin (2011). This is extremely efficient on bigger datasets. PCA and Two-Step estimates are also reported in EM-estimation. All methods support missing data, but `em.method = "DGR"` does not model them in EM iterations.
2930

30-
The package is stable, but functionality may expand in the future. In particular, mixed-frequency estimation with autoregressive errors is planned for the near future, and generation of the 'news' may be added in the further future.
31+
The package is currently stable, but functionality may expand in the future. In particular, mixed-frequency estimation with autoregressive errors is planned for the near future, and generation of the 'news' may be added in the further future.
3132

3233

3334
### Comparison with Other R Packages
@@ -56,7 +57,7 @@ install.packages('dfms', repos = c('https://sebkrantz.r-universe.dev', 'https://
5657
library(dfms)
5758

5859
# Fit DFM with 6 factors and 3 lags in the transition equation
59-
mod = DFM(diff(BM14_M), r = 6, p = 3)
60+
mod <- DFM(diff(BM14_M), r = 6, p = 3)
6061
```
6162

6263
```
@@ -139,7 +140,7 @@ plot(mod)
139140
```
140141

141142
<div class="figure">
142-
<img src="misc/figure/unnamed-chunk-1-1.png" alt="plot of chunk unnamed-chunk-1" width="100%" />
143+
<img src="https://raw.githubusercontent.com/SebKrantz/dfms/main/misc/figure/unnamed-chunk-1-1.png" alt="plot of chunk unnamed-chunk-1" width="100%" />
143144
</div>
144145

145146
```r
@@ -158,14 +159,14 @@ as.data.frame(mod) |> head()
158159

159160
```r
160161
# Forecasting 20 periods ahead
161-
fc = predict(mod, h = 20)
162+
fc <- predict(mod, h = 20)
162163

163164
# 'dfm_forecast' methods
164165
plot(fc)
165166
```
166167

167168
<div class="figure">
168-
<img src="misc/figure/unnamed-chunk-1-2.png" alt="plot of chunk unnamed-chunk-1" width="100%" />
169+
<img src="https://raw.githubusercontent.com/SebKrantz/dfms/main/misc/figure/unnamed-chunk-1-2.png" alt="plot of chunk unnamed-chunk-1" width="100%" />
169170
</div>
170171

171172
```r

man/DFM.Rd

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dfms-package.Rd

Lines changed: 23 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/predict.dfm.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)