Skip to content

Commit f71ae09

Browse files
committed
add tests for jvn_nowcast
1 parent 7c39c26 commit f71ae09

12 files changed

Lines changed: 227 additions & 108 deletions

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ S3method(plot,jvn_model)
44
S3method(plot,kk_model)
55
S3method(print,jvn_model)
66
S3method(summary,jvn_model)
7-
S3method(summary,kk_model)
87
S3method(summary,lst_efficient)
98
export(colors_reviser)
109
export(get_days_to_release)

R/jvn.R

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
#' Econometrics, 2011.
5353
#'
5454
#' @examples
55-
#' # Simulate data
5655
#' gdp <- dplyr::filter(
5756
#' tsbox::ts_pc(
5857
#' reviser::gdp
@@ -1228,6 +1227,27 @@ jvn_param_table <- function(params, se, param_info) {
12281227
#'
12291228
#' @return The function returns the input \code{object} invisibly.
12301229
#' @method summary jvn_model
1230+
#' @examples
1231+
#' gdp <- dplyr::filter(
1232+
#' tsbox::ts_pc(
1233+
#' reviser::gdp
1234+
#' ), id %in% c("EA"),
1235+
#' time >= min(pub_date),
1236+
#' time <= as.Date("2020-01-01")
1237+
#' )
1238+
#' gdp <- tidyr::drop_na(gdp)
1239+
#' df <- get_nth_release(gdp, n = 0:4)
1240+
#'
1241+
#' # Estimate model
1242+
#' result <- jvn_nowcast(
1243+
#' df = df,
1244+
#' e = 3,
1245+
#' ar_order = 2,
1246+
#' h = 4,
1247+
#' include_news = TRUE,
1248+
#' include_noise = TRUE
1249+
#' )
1250+
#' summary(result)
12311251
#'
12321252
#' @family revision nowcasting
12331253
#' @export
@@ -1259,6 +1279,27 @@ summary.jvn_model <- function(object, ...) {
12591279
#'
12601280
#' @return The function returns the input \code{x} invisibly.
12611281
#' @method print jvn_model
1282+
#' @examples
1283+
#' gdp <- dplyr::filter(
1284+
#' tsbox::ts_pc(
1285+
#' reviser::gdp
1286+
#' ), id %in% c("EA"),
1287+
#' time >= min(pub_date),
1288+
#' time <= as.Date("2020-01-01")
1289+
#' )
1290+
#' gdp <- tidyr::drop_na(gdp)
1291+
#' df <- get_nth_release(gdp, n = 0:4)
1292+
#'
1293+
#' # Estimate model
1294+
#' result <- jvn_nowcast(
1295+
#' df = df,
1296+
#' e = 3,
1297+
#' ar_order = 2,
1298+
#' h = 4,
1299+
#' include_news = TRUE,
1300+
#' include_noise = TRUE
1301+
#' )
1302+
#' result
12621303
#'
12631304
#' @family revision nowcasting
12641305
#' @export
@@ -1269,11 +1310,32 @@ print.jvn_model <- function(x, ...) {
12691310
#' Plot JVN Model Results
12701311
#'
12711312
#' @param x An object of class 'jvn_model'
1272-
#' @param state String. The name of the state to visualize (e.g., "state_1").
1313+
#' @param state String. The name of the state to visualize.
12731314
#' @param type String. Type of estimate to plot: "filtered" or "smoothed".
12741315
#' @param ... Additional arguments passed to theme_reviser.
12751316
#'
12761317
#' @return A ggplot2 object visualizing the specified state estimates.
1318+
#' @examples
1319+
#' gdp <- dplyr::filter(
1320+
#' tsbox::ts_pc(
1321+
#' reviser::gdp
1322+
#' ), id %in% c("EA"),
1323+
#' time >= min(pub_date),
1324+
#' time <= as.Date("2020-01-01")
1325+
#' )
1326+
#' gdp <- tidyr::drop_na(gdp)
1327+
#' df <- get_nth_release(gdp, n = 0:4)
1328+
#'
1329+
#' # Estimate model
1330+
#' result <- jvn_nowcast(
1331+
#' df = df,
1332+
#' e = 3,
1333+
#' ar_order = 2,
1334+
#' h = 4,
1335+
#' include_news = TRUE,
1336+
#' include_noise = TRUE
1337+
#' )
1338+
#' plot(result)
12771339
#'
12781340
#' @family revision nowcasting
12791341
#' @export

R/kk.R

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@
7979
#'
8080
#' e <- 1 # Number of efficient release
8181
#' h <- 2 # Forecast horizon
82-
#' model_result <- kk_nowcast(df, e, h = h, model = "Kishor-Koenig")
82+
#' result <- kk_nowcast(df, e, h = h, model = "Kishor-Koenig")
8383
#'
84-
#' model_result$params
84+
#' result$params
8585
#'
8686
#' @references Kishor, N. Kundan and Koenig, Evan F., "VAR Estimation and
8787
#' Forecasting When Data Are Subject to Revision", Journal of Business and
@@ -1213,53 +1213,32 @@ kk_to_ss <- function(FF, GG, V, W, epsilon = 1e-6) {
12131213
return(list(Z = Z, Tmat = Tmat, H = H, Q = Q, R = R))
12141214
}
12151215

1216-
#' Summarize the results of a kk_model object.
1217-
#'
1218-
#' This function calculates and prints the Mean Squared Error (MSE), Root Mean
1219-
#' Squared Error (RMSE), and Mean Absolute Error (MAE) of the filtered state
1220-
#' variables against both the final release and the true efficient release.
1221-
#'
1222-
#' @param object A list of class 'kk_model' produced by the
1223-
#' \code{\link{kk_nowcast}} function.
1224-
#' @param ... Additional arguments (not used).
1225-
#'
1226-
#' @return A list containing two data frames:
1227-
#' \item{final_release_metrics}{A data frame with MSE, RMSE, and MAE against
1228-
#' the final release.}
1229-
#' \item{true_efficient_release_metrics}{A data frame with MSE, RMSE, and
1230-
#' MAE against the true efficient release.}
1231-
#'
1232-
#' @examples
1233-
#' # Assuming 'kk_model_obj' is the result of kk_nowcast(your_data, ...)
1234-
#' # and 'your_data' is the original data frame.
1235-
#' # results <- summary.kk_model(kk_model_obj, your_data)
1236-
#'
1237-
#' @family revision nowcasting
1238-
#' @export
1239-
summary.kk_model <- function(object, ...) {
1240-
cat("\n=== Kishor-Koenig Model ===\n\n")
1241-
cat("Convergence:", ifelse(object$convergence == 0, "Success", "Failed"), "\n")
1242-
cat("Log-likelihood:", round(object$loglik, 2), "\n")
1243-
cat("AIC:", round(object$aic, 2), "\n")
1244-
cat("BIC:", round(object$bic, 2), "\n\n")
1245-
cat("Parameter Estimates:\n")
1246-
#print(object$params, digits = 2, row.names = FALSE)
1247-
df_print <- object$params
1248-
df_print$Estimate <- sprintf("%.3f", df_print$Estimate)
1249-
df_print$Std.Error <- sprintf("%.3f", df_print$Std.Error)
1250-
print(df_print, row.names = FALSE, quote = FALSE)
1251-
cat("\n")
1252-
invisible(object)
1253-
}
12541216

1255-
#' Plot JVN Model Results
1217+
#' Plot Kishor-Koenig Model Results
12561218
#'
1257-
#' @param x An object of class 'jvn_model'
1258-
#' @param state String. The name of the state to visualize (e.g., "state_1").
1219+
#' @param x An object of class 'kk_model'
1220+
#' @param state String. The name of the state to visualize.
12591221
#' @param type String. Type of estimate to plot: "filtered" or "smoothed".
12601222
#' @param ... Additional arguments passed to theme_reviser.
12611223
#'
12621224
#' @return A ggplot2 object visualizing the specified state estimates.
1225+
#' df <- get_nth_release(
1226+
#' tsbox::ts_span(
1227+
#' tsbox::ts_pc(
1228+
#' dplyr::filter(reviser::gdp, id=="US")
1229+
#' ),
1230+
#' start = "1980-01-01"
1231+
#' ),
1232+
#' n = 0:1
1233+
#' )
1234+
#' df <- dplyr::select(df, -c(id, pub_date))
1235+
#' df <- na.omit(df)
1236+
#'
1237+
#' e <- 1 # Number of efficient release
1238+
#' h <- 2 # Forecast horizon
1239+
#' result <- kk_nowcast(df, e, h = h, model = "Kishor-Koenig")
1240+
#'
1241+
#' plot(result)
12631242
#'
12641243
#' @family revision nowcasting
12651244
#' @export

man/jvn_nowcast.Rd

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

man/kk_nowcast.Rd

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

man/plot.jvn_model.Rd

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

man/plot.kk_model.Rd

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

man/print.jvn_model.Rd

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

0 commit comments

Comments
 (0)