Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
annasmyk committed Jul 12, 2024
2 parents ceb97cc + 16fcbd5 commit 52cf6a4
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 149 deletions.
15 changes: 10 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
Package: rjd3nowcasting
Type: Package
Title: Nowcasting with 'JDemetra+ 3.0'
Version: 2.0.2
Authors@R:
Version: 3.0.3
Authors@R: c(
person(given = "Corentin",
family = "Lemasson",
role = c("aut", "cre"),
email = "[email protected]")
email = "[email protected]"),
person(given = "Tanguy",
family = "Barthelemy",
role = c("ctb", "art"),
email ="[email protected]")
)
Description: Interface around 'JDemetra+ 3.x'
(<https://github.com/jdemetra/jdplus-nowcasting>), TSACE project.
It defines and estimates Dynamic Factor Models with the
Expand All @@ -16,8 +21,8 @@ Depends:
Imports:
rJava (>= 1.0-6),
rjd3toolkit (>= 3.2.2)
Remotes:
github::rjdverse/rjd3toolkit@*release
Remotes:
github::rjdverse/rjd3toolkit
SystemRequirements: Java (>= 17)
License: EUPL
URL: https://github.com/rjdverse/rjd3nowcasting,
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [3.0.3] - 2024-07-12


## [2.0.0] - 2024-06-04

## [1.0.0] - 2023-12-21
Expand All @@ -16,6 +19,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

* update version number

[Unreleased]: https://github.com/rjdverse/rjd3nowcasting/compare/v2.0.0...HEAD
[Unreleased]: https://github.com/rjdverse/rjd3nowcasting/compare/v3.0.3...HEAD
[3.0.3]: https://github.com/rjdverse/rjd3nowcasting/compare/v2.0.0...v3.0.3
[2.0.0]: https://github.com/rjdverse/rjd3nowcasting/compare/v1.0.0...v2.0.0
[1.0.0]: https://github.com/rjdverse/rjd3nowcasting/releases/tag/v1.0.0
34 changes: 17 additions & 17 deletions R/estimation.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ create_model<-function(nfactors, nlags, factors_type, factors_loading, var_init
jvar_coefficients <- rjd3toolkit::.r2jd_matrix(var_coefficients)
jvar_errors_variance <- rjd3toolkit::.r2jd_matrix(var_errors_variance)
jmeasurement_coefficients <- rjd3toolkit::.r2jd_matrix(measurement_coefficients)
if(is.null(measurement_errors_variance)){
if (is.null(measurement_errors_variance)){
jmeasurement_errors_variance <- .jnull("[D")
}else{
} else {
jmeasurement_errors_variance <- .jarray(as.numeric(measurement_errors_variance))
}

Expand Down Expand Up @@ -226,14 +226,14 @@ create_model<-function(nfactors, nlags, factors_type, factors_loading, var_init
estimate_pca<-function(dfm, data, standardized = FALSE,
input_standardization = NULL, re_estimate = TRUE){

if(re_estimate){
if (re_estimate){
jdfm<-.r2jd_dfm(dfm)
freq<-stats::frequency(data)
start<-start(data)
jdata<-rjd3toolkit::.r2jd_matrix(data)
if(is.null(input_standardization)){
if (is.null(input_standardization)){
standardization_mean <- standardization_stdev <- .jnull(class = "[D")
} else{
} else {
standardization_mean <- input_standardization[,1]
standardization_stdev <- input_standardization[,2]
}
Expand Down Expand Up @@ -261,8 +261,8 @@ estimate_pca<-function(dfm, data, standardized = FALSE,
hessian<-rjd3toolkit::result(jest,"hessian")
has_converged<-rjd3toolkit::result(jest,"has_converged")

}else{
if(!standardized && is.null(input_standardization)){
} else {
if (!standardized && is.null(input_standardization)){
stop("Since you chose not to re-estimate your model, you must also
fill the argument 'input_standardization' with the original mean
and standard deviation that was previously used to standardize your
Expand Down Expand Up @@ -340,14 +340,14 @@ estimate_em<-function(dfm, data, standardized = FALSE, input_standardization = N
pca_init = TRUE, max_iter = 100, eps = 1e-9,
re_estimate = TRUE){

if(re_estimate){
if (re_estimate){
jdfm<-.r2jd_dfm(dfm)
freq<-stats::frequency(data)
start<-start(data)
jdata<-rjd3toolkit::.r2jd_matrix(data)
if(is.null(input_standardization)){
if (is.null(input_standardization)){
standardization_mean <- standardization_stdev <- .jnull(class = "[D")
} else{
} else {
standardization_mean <- input_standardization[,1]
standardization_stdev <- input_standardization[,2]
}
Expand Down Expand Up @@ -378,8 +378,8 @@ estimate_em<-function(dfm, data, standardized = FALSE, input_standardization = N
hessian<-rjd3toolkit::result(jest,"hessian")
has_converged<-rjd3toolkit::result(jest,"has_converged")

}else{
if(!standardized && is.null(input_standardization)){
} else {
if (!standardized && is.null(input_standardization)){
stop("Since you chose not to re-estimate your model, you must also
fill the argument 'input_standardization' with the original mean
and standard deviation that was previously used to standardize your
Expand Down Expand Up @@ -479,14 +479,14 @@ estimate_ml<-function(dfm, data, standardized = FALSE, input_standardization = N
simpl_model_iter = 15, independent_var_shocks = FALSE,
mixedEstimation = TRUE, eps=1e-9, re_estimate = TRUE){

if(re_estimate){
if (re_estimate){
jdfm<-.r2jd_dfm(dfm)
freq<-stats::frequency(data)
start<-start(data)
jdata<-rjd3toolkit::.r2jd_matrix(data)
if(is.null(input_standardization)){
if (is.null(input_standardization)){
standardization_mean <- standardization_stdev <- .jnull(class = "[D")
} else{
} else {
standardization_mean <- input_standardization[,1]
standardization_stdev <- input_standardization[,2]
}
Expand Down Expand Up @@ -524,8 +524,8 @@ estimate_ml<-function(dfm, data, standardized = FALSE, input_standardization = N
hessian<-rjd3toolkit::result(jest,"hessian")
has_converged<-rjd3toolkit::result(jest,"has_converged")

}else{
if(!standardized && is.null(input_standardization)){
} else {
if (!standardized && is.null(input_standardization)){
stop("Since you chose not to re-estimate your model, you must also
fill the argument 'input_standardization' with the original mean
and standard deviation that was previously used to standardize your
Expand Down
20 changes: 10 additions & 10 deletions R/news.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ DFMNEWS<-'JD3_DfmNews'
colnames(fcsts)<-fcst_periods
rownames(fcsts)<-c("old_forecasts", "revised_forecasts", "new_forecasts")

if(target_factor_type == "Q"){
if (target_factor_type == "Q"){
nc<-ncol(fcsts)
for(j in nc:1){
nkj<-nchar(fcst_periods[j])
mth<-as.numeric(substr(fcst_periods[j],1,nkj-5))
if(mth%%3 == 0){
if (mth%%3 == 0){
yr_str<-substr(fcst_periods[j],nkj-4,nkj)
colnames(weights)[j+5]<-paste0("weights(Q", mth/3, yr_str,")")
colnames(impacts)[j+5]<-paste0("Impacts(Q", mth/3, yr_str,")")
colnames(fcsts)[j]<-paste0("Q", mth/3, yr_str)
}else{
} else {
weights<-weights[,-(j+5)]
impacts<-impacts[,-(j+5)]
fcsts<-fcsts[,-j,drop=FALSE]
Expand Down Expand Up @@ -82,23 +82,23 @@ get_news<-function(dfm_estimates, new_data, target_series = NULL, n_fcst = 3){
old_data<-dfm_estimates$data
series_names<-colnames(old_data)

if(!is.null(target_series)){
if (!is.null(target_series)){
target_series_index<-which(colnames(old_data) == target_series) - 1

if(length(target_series_index) == 0){
if (length(target_series_index) == 0){
target_series_index<-0
warning("Target series not found. The first series was considered by default instead.")
}
}else{
} else {
target_series<-series_names[1]
target_series_index<-0
}

if(n_fcst < 1){
if (n_fcst < 1){
n_fcst<-3
}
target_factor_type<-dfm_estimates$dfm$factors_type[target_series_index + 1]
if(target_factor_type == "Q"){
if (target_factor_type == "Q"){
n_fcst <- n_fcst*3
}

Expand Down Expand Up @@ -153,9 +153,9 @@ plot.JD3_DfmNews <- function(x, ...){
impacts_tot<-as.numeric(x_impacts[nr,start_i:(start_i+nc_fcst-1)])
colnames(impacts)<-colnames(x_fcsts)
rownames(impacts)<-x_impacts[-nr,1]
if(nr <= 10){
if (nr <= 10){
plot_colors<-grDevices::palette.colors(nr-1)
}else{
} else {
plot_colors<-grDevices::colorRampPalette(grDevices::palette.colors(8))(nr-1)
}
legend_txt<-paste0(x_impacts[-nr,1], " (", x_impacts[-nr,2], ")")
Expand Down
26 changes: 11 additions & 15 deletions R/results.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ DFMFORECASTS<-'JD3_DfmForecasts'
freq<-stats::frequency(data)
start<-start(data)
jdata<-rjd3toolkit::.r2jd_matrix(data)
if(is.null(input_standardization)){
if (is.null(input_standardization)){
standardization_mean <- standardization_stdev <- .jnull(class = "[D")
} else{
} else {
standardization_mean <- input_standardization[,1]
standardization_stdev <- input_standardization[,2]
}
Expand Down Expand Up @@ -54,9 +54,9 @@ DFMFORECASTS<-'JD3_DfmForecasts'
freq <- stats::frequency(data)
start <- start(data)
jdata <- rjd3toolkit::.r2jd_matrix(data)
if(is.null(input_standardization)){
if (is.null(input_standardization)){
standardization_mean <- standardization_stdev <- .jnull(class = "[D")
} else{
} else {
standardization_mean <- input_standardization[,1]
standardization_stdev <- input_standardization[,2]
}
Expand Down Expand Up @@ -124,7 +124,7 @@ DFMFORECASTS<-'JD3_DfmForecasts'
k <- 1
for(i in 1:nlags){
for(j in 1:nfactors){
if(i == 1) var_coef_rnames[j]<-paste0("F",j)
if (i == 1) var_coef_rnames[j]<-paste0("F",j)
var_coef_cnames[k]<-paste0("F",j,"[",-i,"]")
k<-k+1
}
Expand Down Expand Up @@ -279,7 +279,7 @@ print.JD3_DfmResults <- function(x, ...){
#'
get_forecasts <- function(dfm_estimates, n_fcst = 3){

if(n_fcst < 1) n_fcst<-1
if (n_fcst < 1) n_fcst<-1
data<-dfm_estimates$data
dfm<-dfm_estimates$dfm
is_standardized<-dfm_estimates$is_standardized
Expand Down Expand Up @@ -317,10 +317,10 @@ get_forecasts <- function(dfm_estimates, n_fcst = 3){
fcsts_only<-stats::window(fcsts, start = c(strt_yr, strt_mth))
fcsts_only_stderr<-stats::window(fcsts_stderr, start = c(strt_yr, strt_mth))

if(nf_max > 0){
if (nf_max > 0){
for(j in 1:nc){
n_na<-nf_max-nf[j]
if(n_na>0) fcsts_only[1:n_na,j]<-fcsts_only_stderr[1:n_na,j]<-NA
if (n_na>0) fcsts_only[1:n_na,j]<-fcsts_only_stderr[1:n_na,j]<-NA
}
}

Expand Down Expand Up @@ -360,16 +360,16 @@ plot.JD3_DfmForecasts <- function(x, series_name = NULL, ...){
fcst_stdev<-x$forecasts_stdev
fcst_only<-x$forecasts_only

if(is.null(series_name)){
if (is.null(series_name)){
series_name<-colnames(fcst)[1]
}

if(series_name %in% colnames(fcst)){
if (series_name %in% colnames(fcst)){
s<-fcst[,series_name]
s_lb<-s - 1.28 * fcst_stdev[,series_name]
s_ub<-s + 1.28 * fcst_stdev[,series_name]
sf<-fcst_only[,series_name]
}else{
} else {
stop("series name not found!")
}

Expand All @@ -378,7 +378,3 @@ plot.JD3_DfmForecasts <- function(x, series_name = NULL, ...){
graphics::axis(1, at=seq(stats::start(s)[1], stats::end(s)[1], by = 1), las=2)
graphics::legend("topleft", legend=c("series", "forecasts", "80% PI"), col=c("black","red","orange"), lty=c(1, 1, 3), cex=0.8)
}




Loading

0 comments on commit 52cf6a4

Please sign in to comment.