diff --git a/.Rbuildignore b/.Rbuildignore index ee15852..3468cb6 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,2 +1,5 @@ ^rjd3nowcasting\.Rproj$ ^\.Rproj\.user$ +^\.github$ +^LICENSE$ +^README\.Rmd$ diff --git a/DESCRIPTION b/DESCRIPTION index 882aaf0..403c83e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,7 @@ Imports: rjd3toolkit (>= 3.2.1) Remotes: github::rjdemetra/rjd3toolkit -SystemRequirements: Java JRE 17 or higher +SystemRequirements: Java (>= 17) License: EUPL Encoding: UTF-8 Collate: @@ -26,4 +26,4 @@ Collate: 'news.R' 'results.R' 'zzz.R' -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 diff --git a/README.Rmd b/README.Rmd new file mode 100644 index 0000000..a4c1439 --- /dev/null +++ b/README.Rmd @@ -0,0 +1,112 @@ +--- +output: github_document +--- + + + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + fig.path = "man/figures/README-", + out.width = "100%", + fig.align = "center", + fig.dim = c(7,4) * 1.4 +) +``` + +# rjd3nowcasting + + + + +## Overview + +Nowcasting is often defined as the prediction of the present, the very near future and the very recent past. + +rjd3nowcasting provides helps to operationalize the process of nowcasting. This first version can be used to specify and estimate dynamic factor models. +A later version is expected to include the concept of "news" similar to the [Nowcasting plugin](https://github.com/nbbrd/jdemetra-nowcasting/tree/master) +of the Graphical User Interface of JDemetra+ v2. + +## Installation + +To get the current stable version (from the latest release): + +```{r, echo = TRUE, eval = FALSE} +# install.packages("remotes") +remotes::install_github("rjdemetra/rjd3toolkit@*release") +remotes::install_github("rjdemetra/rjd3nowcasting@*release") +``` + +To get the current development version from GitHub: + +```{r, echo = TRUE, eval = FALSE} +# install.packages("remotes") +remotes::install_github("rjdemetra/rjd3nowcasting") +``` + + +## Usage + +```{r, echo = TRUE, eval = TRUE} +library("rjd3nowcasting") +``` + + +### Input + +```{r, echo = TRUE, eval = TRUE} +set.seed(100) +data <- ts( + data = matrix(rnorm(500), 100, 5), + frequency = 12, + start = c(2010, 1) +) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +``` + + +### Model + +```{r, echo = TRUE, eval = TRUE} +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +``` + + +### Estimation + +```{r, echo = TRUE, eval = TRUE} +rslt_ml <- estimate_ml(dfm_model, data) +# or rslt_em<-estimate_em(dfm_model, data) +# or rslt_pca<-estimate_pca(dfm_model, data) +``` + + +### Results + +```{r, echo = TRUE, eval = TRUE} +fcst <- get_forecasts(rslt_ml, nf = 2, forecasts_only = TRUE) +params <- get_parameters(rslt_ml) +factors <- get_factors(rslt_ml) +# ... + +print(rslt_ml) +summary(rslt_ml) +plot(rslt_ml) +``` + + +## Contributing + +Any contribution is welcome and should be done through pull requests and/or issues. + + +## Licensing + +The code of this project is licensed under the [European Union Public Licence (EUPL)](https://joinup.ec.europa.eu/page/eupl-text-11-12). diff --git a/README.md b/README.md index 78ad291..7bd73ee 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,22 @@ + + + +# rjd3nowcasting + + + + ## Overview -Nowcasting is often defined as the prediction of the present, the very near future and the very recent past. +Nowcasting is often defined as the prediction of the present, the very +near future and the very recent past. -rjd3nowcasting provides helps to operationalize the process of nowcasting. This first version can be used to specify and estimate dynamic factor models. -A later version is expected to include the concept of "news" similar to the [Nowcasting plugin](https://github.com/nbbrd/jdemetra-nowcasting/tree/master) -of the Graphical User Interface of JDemetra+ v2. +rjd3nowcasting provides helps to operationalize the process of +nowcasting. This first version can be used to specify and estimate +dynamic factor models. A later version is expected to include the +concept of “news” similar to the [Nowcasting +plugin](https://github.com/nbbrd/jdemetra-nowcasting/tree/master) of the +Graphical User Interface of JDemetra+ v2. ## Installation @@ -25,38 +37,91 @@ remotes::install_github("rjdemetra/rjd3nowcasting") ## Usage +``` r +library("rjd3nowcasting") +``` + ### Input + ``` r set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA +data <- ts( + data = matrix(rnorm(500), 100, 5), + frequency = 12, + start = c(2010, 1) +) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA ``` ### Model + ``` r -dfm_model <- rjd3nowcasting::model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) ``` ### Estimation + ``` r -rslt_ml<-rjd3nowcasting::estimate_ml(dfm_model, data) -# or rslt_em<-rjd3nowcasting::estimate_em(dfm_model, data) -# or rslt_pca<-rjd3nowcasting::estimate_pca(dfm_model, data) +rslt_ml <- estimate_ml(dfm_model, data) +# or rslt_em<-estimate_em(dfm_model, data) +# or rslt_pca<-estimate_pca(dfm_model, data) ``` ### Results + ``` r -fcst<-rjd3nowcasting::get_forecasts(rslt_ml,nf = 2,forecasts_only = TRUE) -params<-rjd3nowcasting::get_parameters(rslt_ml) -factors<-rjd3nowcasting::get_factors(rslt_ml) -#... +fcst <- get_forecasts(rslt_ml, nf = 2, forecasts_only = TRUE) +params <- get_parameters(rslt_ml) +factors <- get_factors(rslt_ml) +# ... print(rslt_ml) +#> Measurement: +#> Sample mean Sample Stdev Coeff. of normalized factor F1 +#> Series 1 0.01480 1.01376 0.11234 +#> Series 2 0.01656 0.79045 -0.07066 +#> Series 3 0.01279 1.03407 0.00171 +#> Series 4 -0.08325 1.07544 -0.00624 +#> Series 5 -0.21205 1.06909 -0.00302 +#> Coeff. of normalized factor F2 Idiosyncratic variance +#> Series 1 0.55341 0.41091 +#> Series 2 -0.06925 0.93488 +#> Series 3 0.04849 0.98831 +#> Series 4 0.03340 0.99792 +#> Series 5 -0.33934 0.00000 +#> +#> State: +#> VAR coefficients: +#> F1[-1] F2[-1] F1[-2] F2[-2] +#> F1 1.44836 -0.97181 -0.76117 1.23216 +#> F2 -0.02442 -0.59011 -0.02834 -0.09459 +#> +#> Innovative variance: +#> F1 F2 +#> F1 1 -1 +#> F2 -1 1 summary(rslt_ml) +#> Nowcasted values (only): +#> Series 1 Series 2 Series 3 Series 4 Series 5 +#> Mar 2018 NA 0.3198988965 NA NA NA +#> Apr 2018 0.1827459 0.0001724063 NA NA -2.427725 plot(rslt_ml) ``` + + +## Contributing + +Any contribution is welcome and should be done through pull requests +and/or issues. + +## Licensing + +The code of this project is licensed under the [European Union Public +Licence (EUPL)](https://joinup.ec.europa.eu/page/eupl-text-11-12). diff --git a/man/estimate_em.Rd b/man/estimate_em.Rd index 916d44b..2d70e25 100644 --- a/man/estimate_em.Rd +++ b/man/estimate_em.Rd @@ -45,13 +45,15 @@ Estimate DFM with Expectations-Maximization algorithm } \examples{ set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") -rslt_em<-estimate_em(dfm_model, data) +data <- ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010, 1)) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +rslt_em <- estimate_em(dfm_model, data) } diff --git a/man/estimate_ml.Rd b/man/estimate_ml.Rd index d0a2dc2..0bba776 100644 --- a/man/estimate_ml.Rd +++ b/man/estimate_ml.Rd @@ -78,13 +78,15 @@ Estimate DFM with Maximum Likelihood } \examples{ set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") -rslt_ml<-estimate_ml(dfm_model, data) +data <- ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010, 1)) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +rslt_ml <- estimate_ml(dfm_model, data) } diff --git a/man/estimate_pca.Rd b/man/estimate_pca.Rd index b1d4831..cccd1ec 100644 --- a/man/estimate_pca.Rd +++ b/man/estimate_pca.Rd @@ -27,13 +27,15 @@ Estimate DFM with Principal components Analysis } \examples{ set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") -rslt_pca<-estimate_pca(dfm_model, data) +data <- ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010, 1)) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +rslt_pca <- estimate_pca(dfm_model, data) } diff --git a/man/figures/README-unnamed-chunk-7-1.png b/man/figures/README-unnamed-chunk-7-1.png new file mode 100644 index 0000000..2994b2d Binary files /dev/null and b/man/figures/README-unnamed-chunk-7-1.png differ diff --git a/man/figures/README-unnamed-chunk-8-1.png b/man/figures/README-unnamed-chunk-8-1.png new file mode 100644 index 0000000..a821bcf Binary files /dev/null and b/man/figures/README-unnamed-chunk-8-1.png differ diff --git a/man/get_factors.Rd b/man/get_factors.Rd index ef120a2..e6ff301 100644 --- a/man/get_factors.Rd +++ b/man/get_factors.Rd @@ -17,14 +17,16 @@ Get estimates of the factors } \examples{ set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") -rslt_em<-estimate_em(dfm_model, data) +data <- ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010, 1)) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +rslt_em <- estimate_em(dfm_model, data) get_factors(rslt_em) } diff --git a/man/get_forecasts.Rd b/man/get_forecasts.Rd index d1d27ec..4acf480 100644 --- a/man/get_forecasts.Rd +++ b/man/get_forecasts.Rd @@ -23,14 +23,16 @@ Get forecasts } \examples{ set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") -rslt_em<-estimate_em(dfm_model, data) +data <- ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010, 1)) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +rslt_em <- estimate_em(dfm_model, data) get_forecasts(rslt_em, nf = 3, forecasts_only = TRUE) } diff --git a/man/get_loglikelihood.Rd b/man/get_loglikelihood.Rd index eaaeff4..811605f 100644 --- a/man/get_loglikelihood.Rd +++ b/man/get_loglikelihood.Rd @@ -17,14 +17,16 @@ Get log-likelihood } \examples{ set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") -rslt_em<-estimate_em(dfm_model, data) +data <- ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010, 1)) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +rslt_em <- estimate_em(dfm_model, data) get_loglikelihood(rslt_em) } diff --git a/man/get_parameters.Rd b/man/get_parameters.Rd index 640c706..aad8973 100644 --- a/man/get_parameters.Rd +++ b/man/get_parameters.Rd @@ -17,14 +17,16 @@ Get estimated parameters } \examples{ set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") -rslt_em<-estimate_em(dfm_model, data) +data <- ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010, 1)) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +rslt_em <- estimate_em(dfm_model, data) get_parameters(rslt_em) } diff --git a/man/get_preprocessing.Rd b/man/get_preprocessing.Rd index dfe75fe..4057fd0 100644 --- a/man/get_preprocessing.Rd +++ b/man/get_preprocessing.Rd @@ -17,14 +17,16 @@ Get information about the preprocessing and the input data } \examples{ set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") -rslt_em<-estimate_em(dfm_model, data) +data <- ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010, 1)) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +rslt_em <- estimate_em(dfm_model, data) get_preprocessing(rslt_em) } diff --git a/man/get_residuals.Rd b/man/get_residuals.Rd index c60fcae..9fab59c 100644 --- a/man/get_residuals.Rd +++ b/man/get_residuals.Rd @@ -21,14 +21,16 @@ standardized residuals. } \examples{ set.seed(100) -data<-ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010,1)) -data[100,1]<-data[99:100,2]<-data[(1:100)[-seq(3,100,3)],5]<-NA -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") -rslt_em<-estimate_em(dfm_model, data) +data <- ts(matrix(rnorm(500), 100, 5), frequency = 12, start = c(2010, 1)) +data[100, 1] <- data[99:100, 2] <- data[(1:100)[-seq(3, 100, 3)], 5] <- NA +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) +rslt_em <- estimate_em(dfm_model, data) get_residuals(rslt_em) } diff --git a/man/model.Rd b/man/model.Rd index 3963b8f..052abbe 100644 --- a/man/model.Rd +++ b/man/model.Rd @@ -58,10 +58,12 @@ an object of class 'JD3_DfmModel' Create Dynamic Factor Model } \examples{ -dfm_model <- model(nfactors=2, - nlags=2, - factors_type = c("M", "M", "YoY", "M", "Q"), - factors_loading = matrix(data=TRUE, 5, 2), - var_init = "Unconditional") +dfm_model <- model( + nfactors = 2, + nlags = 2, + factors_type = c("M", "M", "YoY", "M", "Q"), + factors_loading = matrix(data = TRUE, 5, 2), + var_init = "Unconditional" +) } diff --git a/rjd3nowcasting.Rproj b/rjd3nowcasting.Rproj index 516e3fe..1b5d418 100644 --- a/rjd3nowcasting.Rproj +++ b/rjd3nowcasting.Rproj @@ -2,7 +2,7 @@ Version: 1.0 RestoreWorkspace: No SaveWorkspace: No -AlwaysSaveHistory: Default +AlwaysSaveHistory: No EnableCodeIndexing: Yes UseSpacesForTab: Yes