Skip to content

Commit bfd1907

Browse files
committed
New arguments
1 parent 9d04aa7 commit bfd1907

File tree

8 files changed

+122
-41
lines changed

8 files changed

+122
-41
lines changed

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ Suggests:
4343
knitr,
4444
rlang,
4545
rmarkdown,
46-
scales,
4746
survey,
4847
testthat,
4948
tibble,

R/project.R

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
#' fund members over the previous 5 years and 7.9\% growth over the
4646
#' previous ten years.
4747
#'
48+
#' @param r_generic (Present from version 2024.1.0) The factor to inflate other
49+
#' columns. Subject to change in future versions. If \code{NULL}, the default,
50+
#' an internal factor is used.
51+
#'
4852
#'
4953
#' @return A sample file with the same number of rows as \code{sample_file} but
5054
#' with inflated values as a forecast for the sample file in \code{to_fy}.
@@ -86,7 +90,7 @@ project <- function(sample_file,
8690
check_fy_sample_file = TRUE,
8791
differentially_uprate_Sw = NA,
8892
r_super_balance = 1.05,
89-
r_generic = 1) {
93+
r_generic = NULL) {
9094
if (length(h) != 1L) {
9195
stop("`h` had length-", length(h), ", ",
9296
"but must be a length-1 positive integer.")
@@ -134,6 +138,8 @@ project <- function(sample_file,
134138
}
135139

136140

141+
142+
137143
if (check_fy_sample_file) {
138144
# It's been a common error of mine to switch sample files
139145
# without updating the fy.year.of.sample.file
@@ -212,16 +218,32 @@ project <- function(sample_file,
212218
H <- h
213219
current.fy <- fy.year.of.sample.file
214220

215-
to.fy <- yr2fy(fy2yr(current.fy) + h)
221+
to.fy <- yr2fy(to.yr <- (fy2yr(current.fy) + h))
222+
223+
224+
216225

217226
if (is.null(wage.series)){
218-
wage.inflator <- wage_inflator(from = current.fy, to = to.fy)
227+
wage.inflator <- wage_inflator(from = current.fy, to = to.fy,
228+
series = grattanInflators::wpi_original(FORECAST = TRUE))
219229
} else {
220-
wage.inflator <- wage_inflator(from = current.fy, to = to.fy)
230+
wage.inflator <- wage_inflator(from = current.fy, to = to.fy,
231+
series = wage.series)
221232
}
222-
233+
dont_inflate_WEIGHT <- FALSE
223234
if (is.null(lf.series)) {
224-
lf.inflator <- lf_inflator(from = current.fy, to = to.fy)
235+
n_taxpayers_2022_2034 <-
236+
c(16216182, 16543257, 16826686, 17076658, 17326098, 17573757,
237+
18006023, 18438289, 18870556, 19144750, 19409635, 19669543, 19917470)
238+
if (to.yr %in% 2022:2034) {
239+
dont_inflate_WEIGHT <- TRUE
240+
WEIGHT <- NULL
241+
sample_file[, WEIGHT := as.double(WEIGHT)]
242+
set(sample_file, j = "WEIGHT", value = as.double(n_taxpayers_2022_2034[to.yr - 2021] / nrow(sample_file)))
243+
} else {
244+
lf.inflator <- lf_inflator(from = current.fy, to = to.fy,
245+
series = grattanInflators::lfi_original(FORECAST = TRUE))
246+
}
225247
} else {
226248
if (is.data.table(lf.series)) {
227249
stop("lf.series should be a series as defined by lf_inflator.")
@@ -231,7 +253,14 @@ project <- function(sample_file,
231253
series = lf.series)
232254
}
233255

234-
cpi.inflator <- cpi_inflator(from = current.fy, to = to.fy)
256+
cpi.inflator <- cpi_inflator(from = current.fy, to = to.fy,
257+
series = grattanInflators::cpi_seasonal(FORECAST = TRUE))
258+
259+
if (is.null(r_generic)) {
260+
r_generic <- cpi.inflator
261+
} else if (!is.numeric(r_generic) || length(r_generic) != 1 || !is.finite(r_generic)) {
262+
stop("r_generic must be NULL or a length-one numeric.") # nocov
263+
}
235264

236265
if (!is.logical(.recalculate.inflators)) {
237266
stop("`.recalculate.inflators` was type ", typeof(.recalculate.inflators), ", ",
@@ -355,24 +384,24 @@ project <- function(sample_file,
355384
derived.cols,
356385
Not.Inflated)]
357386

358-
if (.recalculate.inflators) {
359-
generic.inflators <-
360-
generic_inflator(vars = generic.cols,
361-
h = h,
362-
fy.year.of.sample.file = fy.year.of.sample.file,
363-
estimator = forecast.dots$estimator,
364-
pred_interval = forecast.dots$pred_interval)
365-
} else {
366-
generic.inflators <-
367-
switch(current.fy,
368-
"2012-13" = generic_inflators_1213,
369-
"2013-14" = generic_inflators_1314,
370-
"2014-15" = generic_inflators_1415,
371-
"2015-16" = generic_inflators_1516,
372-
"2016-17" = generic_inflators_1617,
373-
stop("Precalculated inflators only available when projecting from ",
374-
"2012-13, 2013-14, 2014-15, 2015-16, and 2016-17."))
375-
}
387+
# if (.recalculate.inflators) {
388+
# generic.inflators <-
389+
# generic_inflator(vars = generic.cols,
390+
# h = h,
391+
# fy.year.of.sample.file = fy.year.of.sample.file,
392+
# estimator = forecast.dots$estimator,
393+
# pred_interval = forecast.dots$pred_interval)
394+
# } else {
395+
# generic.inflators <-
396+
# switch(current.fy,
397+
# "2012-13" = generic_inflators_1213,
398+
# "2013-14" = generic_inflators_1314,
399+
# "2014-15" = generic_inflators_1415,
400+
# "2015-16" = generic_inflators_1516,
401+
# "2016-17" = generic_inflators_1617,
402+
# stop("Precalculated inflators only available when projecting from ",
403+
# "2012-13, 2013-14, 2014-15, 2015-16, and 2016-17."))
404+
# }
376405

377406
## Inflate:
378407

@@ -430,6 +459,9 @@ project <- function(sample_file,
430459
if (j %chin% Not.Inflated) {
431460
next
432461
}
462+
if (j == "WEIGHT" && dont_inflate_WEIGHT) {
463+
next
464+
}
433465
v <- .subset2(sample_file, j)
434466
v_new <-
435467
switch(inflator_switch(j),
@@ -459,15 +491,16 @@ project <- function(sample_file,
459491
CG.inflator * v
460492
},
461493
"generic" = {
462-
if (.recalculate.inflators) {
463-
if (nrow(generic.inflators)) {
464-
generic.inflators[variable == j]$inflator * v
465-
} else {
466-
v
467-
}
468-
} else {
469-
generic.inflators[.(H, j), inflator] * v
470-
}
494+
r_generic * v
495+
# if (.recalculate.inflators) {
496+
# if (nrow(generic.inflators)) {
497+
# generic.inflators[variable == j]$inflator * v
498+
# } else {
499+
# v
500+
# }
501+
# } else {
502+
# generic.inflators[.(H, j), inflator] * v
503+
# }
471504
},
472505
"super" = {
473506
{r_super_balance ^ h} * v

R/utils.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,48 @@ is_testing <- function() {
215215
requireNamespace("testthat", quietly = TRUE) && testthat::is_testing()
216216
}
217217

218+
doubleExponentialSmoothing <- function(x, alpha, beta, h = 5) {
219+
# x: Numeric vector representing the time series data
220+
# alpha: Smoothing parameter for the level
221+
# beta: Smoothing parameter for the trend
222+
# h: Forecast horizon
223+
if (!is.numeric(h) || length(h) != 1 || !is.finite(h) || h < 1) {
224+
stop("h must be a positive integer.")
225+
}
226+
227+
# Validate inputs
228+
if (alpha <= 0 || alpha >= 1) {
229+
stop("alpha must be between 0 and 1")
230+
}
231+
if (beta <= 0 || beta >= 1) {
232+
stop("beta must be between 0 and 1")
233+
}
234+
if (length(x) < 2) {
235+
stop("Time series must have at least two observations")
236+
}
237+
238+
n <- length(x)
239+
level <- numeric(n)
240+
trend <- numeric(n)
241+
forecast <- numeric(n + h)
242+
243+
# Initialize components
244+
level[1] <- x[1]
245+
trend[1] <- x[2] - x[1]
246+
247+
# Apply Double Exponential Smoothing
248+
for(t in 2:n) {
249+
level[t] <- alpha * x[t] + (1 - alpha) * (level[t-1] + trend[t-1])
250+
trend[t] <- beta * (level[t] - level[t-1]) + (1 - beta) * trend[t-1]
251+
}
252+
253+
# Generate forecasts
254+
for (i in 1:h) {
255+
forecast[n + i] <- level[n] + i * trend[n]
256+
}
257+
258+
# Return only the forecasts
259+
return(forecast[(n+1):(n+h)])
260+
}
261+
262+

man/apply_super_caps_and_div293.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.

man/inverse_average_rate.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.

man/model_new_caps_and_div293.Rd

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

man/project.Rd

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

tests/testthat/test_zbenchmark.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test_that("Performance regression: wage_inflator", {
3333
identical(Sys.getenv("TRAVIS_R_VERSION_STRING"), "devel")),
3434
Sys.getenv("TRAVIS_PULL_REQUEST") != "true")))
3535
set.seed(19992014)
36-
from_fys10K <- sample(yr2fy(1999:2014), size = 10e3, replace = TRUE)
36+
from_fys10K <- sample(grattan::yr2fy(1999:2014), size = 10e3, replace = TRUE)
3737
from_fys100M <- rep(from_fys10K, times = 100e6/10e3)
3838
wage_infl_time10K <- system.time(wage_inflator(from = from_fys10K,
3939
to = "2015-16"))

0 commit comments

Comments
 (0)