Skip to content

Commit bac61bf

Browse files
committed
styling
1 parent b74ef70 commit bac61bf

28 files changed

Lines changed: 754 additions & 490 deletions

.github/CONTRIBUTING.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ For a detailed discussion on contributing to this and other tidyverse packages,
55

66
## Fixing typos
77

8-
You can fix typos, spelling mistakes, or grammatical errors in the documentation directly using the GitHub web interface, as long as the changes are made in the _source_ file.
9-
This generally means you'll need to edit [roxygen2 comments](https://roxygen2.r-lib.org/articles/roxygen2.html) in an `.R`, not a `.Rd` file.
8+
You can fix typos, spelling mistakes, or grammatical errors in the documentation directly using the GitHub web interface, as long as the changes are made in the _source_ file.
9+
This generally means you'll need to edit [roxygen2 comments](https://roxygen2.r-lib.org/articles/roxygen2.html) in an `.R`, not a `.Rd` file.
1010
You can find the `.R` file that generates the `.Rd` by reading the comment in the first line.
1111

1212
## Bigger changes
1313

14-
If you want to make a bigger change, it's a good idea to first file an issue and make sure someone from the team agrees that it’s needed.
15-
If you’ve found a bug, please file an issue that illustrates the bug with a minimal
14+
If you want to make a bigger change, it's a good idea to first file an issue and make sure someone from the team agrees that it’s needed.
15+
If you’ve found a bug, please file an issue that illustrates the bug with a minimal
1616
[reprex](https://www.tidyverse.org/help/#reprex) (this will also help you write a unit test, if needed).
1717
See our guide on [how to create a great issue](https://code-review.tidyverse.org/issues/) for more advice.
1818

1919
### Pull request process
2020

2121
* Fork the package and clone onto your computer. If you haven't done this before, we recommend using `usethis::create_from_github("p-wegmueller/reviser", fork = TRUE)`.
2222

23-
* Install all development dependencies with `devtools::install_dev_deps()`, and then make sure the package passes R CMD check by running `devtools::check()`.
24-
If R CMD check doesn't pass cleanly, it's a good idea to ask for help before continuing.
23+
* Install all development dependencies with `devtools::install_dev_deps()`, and then make sure the package passes R CMD check by running `devtools::check()`.
24+
If R CMD check doesn't pass cleanly, it's a good idea to ask for help before continuing.
2525
* Create a Git branch for your pull request (PR). We recommend using `usethis::pr_init("brief-description-of-change")`.
2626

2727
* Make your changes, commit to git, and then create a PR by running `usethis::pr_push()`, and following the prompts in your browser.
@@ -32,13 +32,13 @@ See our guide on [how to create a great issue](https://code-review.tidyverse.org
3232

3333
### Code style
3434

35-
* New code should follow the tidyverse [style guide](https://style.tidyverse.org).
36-
You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply these styles, but please don't restyle code that has nothing to do with your PR.
35+
* New code should follow the tidyverse [style guide](https://style.tidyverse.org).
36+
You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply these styles, but please don't restyle code that has nothing to do with your PR.
3737

38-
* We use [roxygen2](https://cran.r-project.org/package=roxygen2), with [Markdown syntax](https://cran.r-project.org/web/packages/roxygen2/vignettes/rd-formatting.html), for documentation.
38+
* We use [roxygen2](https://cran.r-project.org/package=roxygen2), with [Markdown syntax](https://cran.r-project.org/web/packages/roxygen2/vignettes/rd-formatting.html), for documentation.
3939

40-
* We use [testthat](https://cran.r-project.org/package=testthat) for unit tests.
41-
Contributions with test cases included are easier to accept.
40+
* We use [testthat](https://cran.r-project.org/package=testthat) for unit tests.
41+
Contributions with test cases included are easier to accept.
4242

4343
## Code of Conduct
4444

.github/workflows/pkgdown.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ jobs:
4848
clean: false
4949
branch: gh-pages
5050
folder: docs
51-
51+

CODE_OF_CONDUCT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ representative at an online or offline event.
5959
## Enforcement
6060

6161
Instances of abusive, harassing, or otherwise unacceptable behavior may be
62-
reported to the community leaders responsible for enforcement at marc.burri91@gmail.com.
62+
reported to the community leaders responsible for enforcement at marc.burri91@gmail.com.
6363
All complaints will be reviewed and investigated promptly and fairly.
6464

6565
All community leaders are obligated to respect the privacy and security of the

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Version: 0.1.0.9000
55
Authors@R: c(
66
person(given = "Marc", family = "Burri", email = "marc.burri91@gmail.com", role = c("aut", "cre", "cph"),
77
comment = c(ORCID = "0000-0001-8974-9090")),
8-
person(given = "Philipp", family = "Wegmueller",
8+
person(given = "Philipp", family = "Wegmueller",
99
email = "philipp.wemueller@seco.admin.ch", role = c("aut", "cph"))
1010
)
1111
Description: Provides tools to analyze revision properties in real-time time series data.
@@ -14,7 +14,7 @@ Maintainer: Marc Burri <marc.burri91@gmail.com>
1414
License: MIT + file LICENSE
1515
Encoding: UTF-8
1616
LazyData: true
17-
Depends:
17+
Depends:
1818
R (>= 2.10)
1919
RoxygenNote: 7.3.3
2020
Roxygen: list (markdown = TRUE, roclets = c ("namespace", "rd", "srr::srr_stats_roclet"))
@@ -34,7 +34,7 @@ Imports:
3434
lubridate,
3535
KFAS,
3636
numDeriv
37-
Suggests:
37+
Suggests:
3838
tsbox,
3939
testthat (>= 3.0.0),
4040
knitr,

R/data-vintages.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#' * JP:
3232
#' * Japan
3333
#' * Sources: Cabinet Office (Japan), OECD
34-
#'
34+
#'
3535
#'
3636
#' @srrstats {G1.0} academic literature
3737
#'

R/graphs.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ plot_vintages <- function(
320320
#' @param ... Additional arguments passed to plot_vintages.
321321
#'
322322
#' @return A ggplot2 object.
323-
#' @srrstats {TS5.0} Implements default plot methods for implemented class system
323+
#' @srrstats {TS5.0} Implements default plot methods for implemented
324+
#' class system
324325
#' @srrstats {TS4.2} Explicitly documents the type and class of return values
325326
#' @method plot tbl_pubdate
326327
#' @examples
@@ -338,7 +339,8 @@ plot.tbl_pubdate <- function(x, ...) {
338339
#' @param ... Additional arguments passed to plot_vintages.
339340
#'
340341
#' @return A ggplot2 object.
341-
#' @srrstats {TS5.0} Implements default plot methods for implemented class system
342+
#' @srrstats {TS5.0} Implements default plot methods for implemented
343+
#' class system
342344
#' @srrstats {TS4.2} Explicitly documents the type and class of return values
343345
#' @method plot tbl_release
344346
#' @examples
@@ -359,9 +361,12 @@ plot.tbl_release <- function(x, ...) {
359361
#' @param ... Additional arguments passed to theme_reviser.
360362
#' @srrstats {G1.4a} Internal function documented with @noRd tag
361363
#' @srrstats {TS5.0} Implements plot methods for class system
362-
#' @srrstats {TS5.7} Includes model (input) values in plot with forecast (output) values (in-sample vs out-of-sample)
363-
#' @srrstats {TS5.8} Provides clear visual distinction between model and forecast values (different colors/samples)
364-
#' @srrstats {TS5.6} Indicates distributional limits (confidence intervals) on plot by default
364+
#' @srrstats {TS5.7} Includes model (input) values in plot with forecast
365+
#' (output) values (in-sample vs out-of-sample)
366+
#' @srrstats {TS5.8} Provides clear visual distinction between model and
367+
#' forecast values (different colors/samples)
368+
#' @srrstats {TS5.6} Indicates distributional limits (confidence
369+
#' intervals) on plot by default
365370
#' @return ggplot object
366371
#'
367372
#' @keywords internal

R/jvn.R

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,20 @@
4646
#' \item{bic}{Bayesian Information Criterion}
4747
#' \item{data}{Input data}
4848
#' }
49-
#'
49+
#'
5050
#' @srrstats {G1.0} Primary reference: Jacobs & Van Norden (2011)
51-
#' @srrstats {G1.1} First implementation in R of the Jacobs-Van Norden state-space model
52-
#' @srrstats {G1.3} Statistical terminology clearly defined (news, noise, spillovers)
53-
#' @srrstats {G2.0} Input assertions on lengths (e, h, ar_order must be single values)
54-
#' @srrstats {G2.0a} Documents expectations on input lengths in parameter descriptions
51+
#' @srrstats {G1.1} First implementation in R of the Jacobs-Van Norden
52+
#' state-space model
53+
#' @srrstats {G1.3} Statistical terminology clearly defined (news, noise,
54+
#' spillovers)
55+
#' @srrstats {G2.0} Input assertions on lengths (e, h, ar_order must be
56+
#' single values)
57+
#' @srrstats {G2.0a} Documents expectations on input lengths in parameter
58+
#' descriptions
5559
#' @srrstats {G2.1} Input type assertions (checks for list, numeric types)
5660
#' @srrstats {G2.1a} Documents data type expectations for all inputs
57-
#' @srrstats {G2.2} Restricts multivariate input to univariate parameters (e, h, ar_order)
61+
#' @srrstats {G2.2} Restricts multivariate input to univariate parameters
62+
#' (e, h, ar_order)
5863
#' @srrstats {G2.3a} Uses match.arg() equivalent for method parameter validation
5964
#' @srrstats {G2.3b} Documents case-sensitivity requirements
6065
#' @srrstats {G2.4} Type conversion mechanisms (round() for ar_order)
@@ -71,18 +76,21 @@
7176
#' @srrstats {TS1.0} Uses explicit time series class systems
7277
#' @srrstats {TS1.1} Documents types/classes of input data
7378
#' @srrstats {TS1.2} Validation routines for input classes (vintages_check)
74-
#' @srrstats {TS1.3} Pre-processing to validate and transform input (vintages_wide)
79+
#' @srrstats {TS1.3} Pre-processing to validate and transform input
80+
#' (vintages_wide)
7581
#' @srrstats {TS1.4} Maintains time/date components of input data
7682
#' @srrstats {TS1.5} Ensures strict ordering of time index
7783
#' @srrstats {TS1.6} Catches ordering violations in pre-processing
78-
#' @srrstats {TS1.8} Explicit about monthly time intervals (frequency calculation)
84+
#' @srrstats {TS1.8} Explicit about monthly time intervals (frequency
85+
#' calculation)
7986
#' @srrstats {TS2.0} Handles explicit vs implicit missing values
8087
#' @srrstats {TS2.1} Options for handling missing data
8188
#' @srrstats {TS4.0b} Returns unique class-defined format (jvn_model)
8289
#' @srrstats {TS4.2} Explicitly documents return value types/classes
8390
#' @srrstats {TS4.3} Return values include time scales
8491
#' @srrstats {TS4.6b} Forecasting returns first- and second-order moments
85-
#' @srrstats {TS4.6c} Error indication for forecast estimates (confidence intervals)
92+
#' @srrstats {TS4.6c} Error indication for forecast estimates (confidence
93+
#' intervals)
8694
#' @srrstats {TS4.7c} Distinguishes model vs forecast values (sample column)
8795
#'
8896
#' @references Jacobs, Jan P.A.M. and Van Norden, Simon, "Modeling Data
@@ -167,6 +175,11 @@ jvn_nowcast <- function(
167175
}
168176

169177
# Check input e
178+
if (!is.numeric(e) || length(e) != 1 || is.na(e) || e %% 1 != 0) {
179+
rlang::abort("'e' must be a single whole number greater than 0.")
180+
}
181+
e <- as.integer(e)
182+
170183
if (e == 0) {
171184
rlang::abort("The initial release is already efficient, 'e' is equal to 0!")
172185
}
@@ -209,8 +222,22 @@ jvn_nowcast <- function(
209222
if (is.list(df) && !is.data.frame(df)) df <- df[[1]]
210223
}
211224

225+
required_release_cols <- paste0("release_", 0:(e - 1))
226+
missing_release_cols <- setdiff(required_release_cols, colnames(df))
227+
if (length(missing_release_cols) > 0) {
228+
rlang::abort(paste0(
229+
"'df' must contain release columns ",
230+
paste(required_release_cols, collapse = ", "),
231+
" for e = ",
232+
e,
233+
". Missing: ",
234+
paste(missing_release_cols, collapse = ", ")
235+
))
236+
}
237+
212238
# Arrange data
213-
df_intern <- df[, 1:(e + 1)]
239+
df_intern <- df %>%
240+
dplyr::select("time", dplyr::all_of(required_release_cols))
214241

215242
y_mat <- as.matrix(dplyr::select(df_intern, -"time"))
216243
rownames(y_mat) <- as.character(df$time)
@@ -698,7 +725,7 @@ jvn_nowcast <- function(
698725
frequency <- unique((round(as.numeric(diff(df$time)) / 30)))
699726
if (length(frequency) > 1) {
700727
rlang::abort(
701-
"The time series seems not to be regular,
728+
"The time series seems not to be regular,
702729
please provide a regular time series!"
703730
)
704731
}
@@ -821,7 +848,7 @@ jvn_nowcast <- function(
821848
#'
822849
#' Constructs the state-space matrices Z, T, R, H, Q according to the
823850
#' Jacobs & Van Norden (2011) specification.
824-
#'
851+
#'
825852
#' @srrstats {G1.4a} Internal function documented with @noRd tag
826853
#' @srrstats {G2.0} Input assertions on parameter dimensions
827854
#' @srrstats {G2.1} Type checking for parameters
@@ -1149,6 +1176,49 @@ jvn_negloglik <- function(params, model_struct, data, transform_se = TRUE) {
11491176
)
11501177
}
11511178

1179+
#' Create stable default starting values for the JVN model
1180+
#' @keywords internal
1181+
#' @noRd
1182+
jvn_default_params <- function(model_struct, transform_se = TRUE) {
1183+
info <- model_struct$param_info
1184+
params <- numeric(model_struct$n_params)
1185+
1186+
if (!is.null(info$ar_coef_idx)) {
1187+
ar_default <- 0.4 / seq_along(info$ar_coef_idx)
1188+
params[info$ar_coef_idx] <- ar_default
1189+
}
1190+
1191+
if (!is.null(info$sigma_e_idx)) {
1192+
params[info$sigma_e_idx] <- 0.2
1193+
}
1194+
if (!is.null(info$sigma_nu_idx)) {
1195+
params[info$sigma_nu_idx] <- 0.15
1196+
}
1197+
if (!is.null(info$sigma_zeta_idx)) {
1198+
params[info$sigma_zeta_idx] <- 0.1
1199+
}
1200+
if (!is.null(info$spill_nu_idx)) {
1201+
params[info$spill_nu_idx] <- 0.3
1202+
}
1203+
if (!is.null(info$spill_zeta_idx)) {
1204+
params[info$spill_zeta_idx] <- 0.2
1205+
}
1206+
1207+
if (transform_se) {
1208+
if (!is.null(info$sigma_e_idx)) {
1209+
params[info$sigma_e_idx] <- log(params[info$sigma_e_idx])
1210+
}
1211+
if (!is.null(info$sigma_nu_idx)) {
1212+
params[info$sigma_nu_idx] <- log(params[info$sigma_nu_idx])
1213+
}
1214+
if (!is.null(info$sigma_zeta_idx)) {
1215+
params[info$sigma_zeta_idx] <- log(params[info$sigma_zeta_idx])
1216+
}
1217+
}
1218+
1219+
params
1220+
}
1221+
11521222
#' Initialize Parameters with Data-Driven Starting Values
11531223
#' @keywords internal
11541224
#' @noRd
@@ -1163,7 +1233,7 @@ jvn_init_params <- function(model_struct, data, transform_se = TRUE) {
11631233

11641234
if (nrow(data_clean) < ar_order + 10) {
11651235
warning("Insufficient data for smart initialization. Using default values.")
1166-
return(jvn_init_params(model_struct, transform_se))
1236+
return(jvn_default_params(model_struct, transform_se))
11671237
}
11681238

11691239
# ===== 1. AR COEFFICIENTS AND SHOCK from final vintage =====
@@ -1301,7 +1371,7 @@ jvn_init_params <- function(model_struct, data, transform_se = TRUE) {
13011371
# ===== 6. VALIDATION =====
13021372
if (any(!is.finite(params))) {
13031373
warning("Some starting values are non-finite. Falling back to defaults.")
1304-
return(jvn_init_params(model_struct, transform_se))
1374+
return(jvn_default_params(model_struct, transform_se))
13051375
}
13061376

13071377
# Ensure AR stationarity
@@ -1483,7 +1553,7 @@ print.jvn_model <- function(x, ...) {
14831553
#' @param state String. The name of the state to visualize.
14841554
#' @param type String. Type of estimate to plot: "filtered" or "smoothed".
14851555
#' @param ... Additional arguments passed to theme_reviser.
1486-
#'
1556+
#'
14871557
#' @srrstats {TS5.0} Implements default plot methods for class system
14881558
#' @srrstats {TS5.1} Time axis labeling (delegates to base method)
14891559
#' @srrstats {TS5.2} Time on horizontal axis (delegates to base method)

R/kk.R

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@
101101
#' @srrstats {TS4.6} Time Series Software which implements or otherwise
102102
#' enables forecasting should return either:
103103
#' @srrstats {TS4.6b} filtered/forecasted point estimates
104-
#' @srrstats {TS4.6c} Error indication for forecast estimates (confidence intervals)
104+
#' @srrstats {TS4.6c} Error indication for forecast estimates (confidence
105+
#' intervals)
105106
#' @srrstats {TS4.7c} Distinguishes model vs forecast values (sample column)
106107
#' @srrstats {TS4.7} forecast values and models separately returned
107108
#' @srrstats {TS4.7a} only forecast values returned
@@ -160,6 +161,11 @@ kk_nowcast <- function(
160161
}
161162

162163
# Check input e
164+
if (!is.numeric(e) || length(e) != 1 || is.na(e) || e %% 1 != 0) {
165+
rlang::abort("'e' must be a single whole number greater than 0.")
166+
}
167+
e <- as.integer(e)
168+
163169
if (e == 0) {
164170
rlang::abort("The initial release is already efficient, 'e' is equal to 0!")
165171
}
@@ -269,6 +275,19 @@ kk_nowcast <- function(
269275
if (is.list(df) && !is.data.frame(df)) df <- df[[1]]
270276
}
271277

278+
required_release_cols <- paste0("release_", 0:e)
279+
missing_release_cols <- setdiff(required_release_cols, colnames(df))
280+
if (length(missing_release_cols) > 0) {
281+
rlang::abort(paste0(
282+
"'df' must contain release columns ",
283+
paste(required_release_cols, collapse = ", "),
284+
" for e = ",
285+
e,
286+
". Missing: ",
287+
paste(missing_release_cols, collapse = ", ")
288+
))
289+
}
290+
272291
# Define state and observable variable names
273292
z_names <- c(paste0("release_", e, "_lag_", (e):0))
274293
y_names <- c(paste0("release_", e:0, "_lag_", e:0))
@@ -873,7 +892,7 @@ kk_nowcast <- function(
873892
frequency <- unique((round(as.numeric(diff(df$time)) / 30)))
874893
if (length(frequency) > 1) {
875894
rlang::abort(
876-
"The time series seems not to be regular,
895+
"The time series seems not to be regular,
877896
please provide a regular time series!"
878897
)
879898
}

0 commit comments

Comments
 (0)