Skip to content

Commit

Permalink
Merge pull request #21 from Natsiopoulos/dev
Browse files Browse the repository at this point in the history
Changes for release 0.2.4
  • Loading branch information
Natsiopoulos authored Aug 29, 2023
2 parents 9a24034 + 69849ef commit 0ccbf32
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 42 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Description: Creates complex autoregressive distributed lag (ARDL) models and
equation. The validity and the accuracy of this package have been verified
by successfully replicating the results of Pesaran et al. (2001) in
Natsiopoulos and Tzeremes (2022) <doi:10.1002/jae.2919>.
Version: 0.2.3
Version: 0.2.4
Authors@R: c(
person("Kleanthis", "Natsiopoulos",
email = "klnatsio@gmail.com", role = c("aut", "cre"),
Expand All @@ -25,6 +25,8 @@ LazyData: true
Depends:
R (>= 3.5.0)
Suggests:
strucchange,
tseries,
qpcR,
sandwich,
testthat (>= 3.0.0)
Expand All @@ -38,5 +40,5 @@ Imports:
msm,
stringr,
zoo
RoxygenNote: 7.2.0
RoxygenNote: 7.2.3
Config/testthat/edition: 3
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# ARDL 0.2.4

### New features

* Added new parameters to the `to_lm()` function. These are `fix_names` which
removes special functions such as `d()` and `L()` from the variable names, and
`data_class` which converts the data class to `ts`.

### Documentation update

* Added example of how to apply post-estimation tests, combining other packages.

---

# ARDL 0.2.3

### New features
Expand Down
5 changes: 5 additions & 0 deletions R/ardl.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
#' ardl_3132$full_formula
#' m2 <- dynlm(ardl_3132$full_formula, data = ardl_3132$data)
#' identical(m$coefficients, m2$coefficients)
#'
#' ## Post-estimation testing ---------------------------------------------
#'
#' # See examples in the help file of the uecm() function
#'

ardl <- function(...) {
UseMethod("ardl")
Expand Down
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' \item{IBO}{bond rate}
#' \item{IDE}{bank deposit rate}
#' }
#' @source \url{http://web.math.ku.dk/~sjo/data/danish_data.html}
#' @source \url{https://onlinelibrary.wiley.com/doi/10.1111/j.1468-0084.1990.mp52002003.x}
#' @references Johansen, S. and Juselius, K. (1990), Maximum Likelihood
#' Estimation and Inference on Cointegration -- with Applications to the Demand
#' for Money, \emph{Oxford Bulletin of Economics and Statistics}, \bold{52, 2},
Expand Down
15 changes: 8 additions & 7 deletions R/multipliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
#' that the sum of the interim multipliers (for long enough periods) and thus
#' a distant enough interim multiplier match the long-run multipliers.
#'
#' The delay (interim) multiplier can be interpreted as the effect in period
#' t+s, of an instant (sustained) shock in period t.
#' The delay (interim) multiplier can be interpreted as the effect on the
#' dependent variable in period t+s, resulting from an instant (sustained) shock
#' to an independent variable in period t.
#'
#' The delta method is used for approximating the standard errors (and thus the
#' t-statistics and p-values) of the estimated long-run and delay multipliers.
Expand All @@ -35,11 +36,11 @@
#' \code{\link[sandwich]{vcovHAC}}).
#' @param se A logical indicating whether you want standard errors for delay
#' multipliers to be provided. The default is FALSE. Note that this parameter
#' does not refer to the standard errors for the long-run multipliers, which are
#' always calculated. IMPORTANT: Calculating standard errors for long periods of
#' delays may cause your computer to run out of memory and terminate your R
#' session, losing important unsaved work. As a rule of thumb, try not to exceed
#' \code{type = 19} when \code{se = TRUE}.
#' does not refer to the standard errors for the long-run and short-run
#' multipliers, for which are always calculated. IMPORTANT: Calculating standard
#' errors for long periods of delays may cause your computer to run out of
#' memory and terminate your R session, losing important unsaved work. As a rule
#' of thumb, try not to exceed \code{type = 19} when \code{se = TRUE}.
#'
#' @return \code{multipliers} returns (for long and short run multipliers) a
#' data.frame containing the independent variables (including possibly
Expand Down
2 changes: 1 addition & 1 deletion R/plot_delay.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @param interval If FALSE (default), no uncertainty intervals are drawn. If a
#' positive integer, the intervals are this number times the standard error. If
#' a number between 0 and 1 (e.g. 0.95), the equivalent confidence interval is
#' drawn (e.g. 95% CI). In case of the confidence intervals, they are based on
#' drawn (e.g. 95\% CI). In case of the confidence intervals, they are based on
#' the Gaussian distribution.
#' @param interval_color The color of the uncertainty intervals. Default is
#' "blue".
Expand Down
5 changes: 5 additions & 0 deletions R/recm.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
#' # through the coefficient of the term L(y, 1) (where y is the dependent variable).
#' tail(recm_3132$coefficients, 1)
#' uecm_3132$coefficients[2]
#'
#' ## Post-estimation testing ---------------------------------------------
#'
#' # See examples in the help file of the uecm() function
#'

recm <- function(object, case) {
# no visible binding for global variable NOTE solution
Expand Down
88 changes: 69 additions & 19 deletions R/to_lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@
#' as the \code{\link[stats]{predict}} function to forecast.
#'
#' @param object An object of \code{\link[base]{class}} 'ardl', 'uecm' or 'recm'.
#' @param fix_names A logical, indicating whether the variable names should be
#' rewritten without special functions and character in the names such as "d()"
#' or "L()". When \code{fix_names = TRUE}, the characters "(", and "," are
#' replaces with ".", and ")" and spaces are deleted. The name of the dependent
#' variable is always transformed, regardless of the value of this parameter.
#' Default is FALSE.
#' @param data_class If "ts", it converts the data class to
#' \code{\link[stats]{ts}} (see examples for its usage). The default is
#' \code{\link[base]{NULL}}, which uses the same data provided in the original
#' object.
#' @param ... Currently unused argument.
#'
#' @return \code{to_lm} returns an object of \code{\link[base]{class}}
#' \code{"lm"}.
#' \code{"lm"}.
#'
#' @seealso \code{\link{ardl}}, \code{\link{uecm}}, \code{\link{recm}}
#' @author Kleanthis Natsiopoulos, \email{klnatsio@@gmail.com}
Expand Down Expand Up @@ -54,35 +64,75 @@
#' plot(denmark$LRM, lwd=4) #The input dependent variable
#' lines(ardl_3132$fitted.values, lwd=4, col="blue") #The fitted values
#' lines(predicted_values, lty=2, lwd=2, col="red") #The predicted values
#'
#' ## Convert to lm for post-estimation testing ---------------------------
#'
#' # Ramsey's RESET test for functional form
#' library(lmtest) # for resettest()
#' library(strucchange) # for efp(), and sctest()
#'
#' \dontrun{
#' # This produces an error.
#' # resettest() cannot use data of class 'zoo' such as the 'denmark' data
#' # used to build the original model
#' resettest(uecm_3132, type = c("regressor"))
#' }
#'
#' uecm_3132_lm <- to_lm(uecm_3132, data_class = "ts")
#' resettest(uecm_3132_lm, power = 2)
#'
#' # CUSUM test for structural change detection
#' \dontrun{
#' # This produces an error.
#' # efp() does not understand special functions such as "d()" and "L()"
#' efp(uecm_3132$full_formula, data = uecm_3132$model)
#' }
#'
#' uecm_3132_lm_names <- to_lm(uecm_3132, fix_names = TRUE)
#' fluctuation <- efp(uecm_3132_lm_names$full_formula,
#' data = uecm_3132_lm_names$model)
#' sctest(fluctuation)
#' plot(fluctuation)
#'

to_lm <- function(object, ...) {
to_lm <- function(object, fix_names = FALSE, data_class = NULL, ...) {
objmodel <- object$model
dep_var <- names(object$model)[1]
diff_var <- grepl("d(", dep_var, fixed=TRUE)
lag_var <- grepl("L(", dep_var, fixed=TRUE)
if (lag_var | diff_var) {
if (lag_var) {
y <- gsub(" ", "", dep_var) %>%
sub("(", ".", ., fixed=TRUE) %>%
sub(")", "", ., fixed=TRUE) %>%
sub(",", ".", ., fixed=TRUE)
} else if (diff_var) {
y <- gsub(" ", "", dep_var) %>%
sub("(", ".", ., fixed=TRUE) %>%
sub(")", "", ., fixed=TRUE)
if (!is.null(data_class)) {
if (data_class == "ts") {
objmodel <- stats::ts(objmodel, start = stats::start(objmodel[,1]), frequency = stats::frequency(objmodel[,1]))
}
names(objmodel)[1] <- y
}
dep_var <- colnames(objmodel)[1]
fix_names_fun <- function(text) {
text <- gsub(" ", "", text) %>%
gsub("(", ".", ., fixed = TRUE) %>%
gsub(")", "", ., fixed = TRUE) %>%
gsub(",", ".", ., fixed = TRUE)
return(text)
}
y <- fix_names_fun(dep_var)

if (fix_names) {
colnames(objmodel) <- sapply(colnames(objmodel), fix_names_fun)
if (attr(object$terms,"intercept") == 0) {
formula <- formula(paste0(y, " ~ . -1"))
} else {
formula <- formula(paste0(y, " ~ ."))
}

full_formula <- as.character(object$full_formula)
lm_model <- stats::lm(formula, data = objmodel)
lm_model$full_formula <- stats::formula(paste0(y, "~", fix_names_fun(full_formula[3])))

return(lm_model)
} else {
colnames(objmodel)[1] <- y
if (attr(object$terms,"intercept") == 0) {
formula <- formula(paste0(dep_var, " ~ . -1"))
formula <- formula(paste0(y, " ~ . -1"))
} else {
formula <- formula(paste0(dep_var, " ~ ."))
formula <- formula(paste0(y, " ~ ."))
}

return(stats::lm(formula, data = objmodel))
}
return(stats::lm(formula, data = objmodel))
}
40 changes: 40 additions & 0 deletions R/uecm.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,46 @@
#' uecm_3132_ <- uecm(LRM ~ LRY + IBO + IDE, data = denmark, order = c(3,1,3,2))
#' identical(uecm_3132, uecm_3132_)
#' summary(uecm_3132)
#'
#' ## Post-estimation testing ---------------------------------------------
#'
#' library(lmtest) # for bgtest(), bptest(), and resettest()
#' library(tseries) # for jarque.bera.test()
#' library(strucchange) # for efp(), and sctest()
#'
#' # Breusch-Godfrey test for higher-order serial correlation
#' bgtest(uecm_3132, order = 4)
#'
#' # Breusch-Pagan test against heteroskedasticity
#' bptest(uecm_3132)
#'
#' # Ramsey's RESET test for functional form
#' \dontrun{
#' # This produces an error.
#' # resettest() cannot use data of class 'zoo' such as the 'denmark' data
#' # used to build the original model
#' resettest(uecm_3132, type = c("regressor"))
#' }
#'
#' uecm_3132_lm <- to_lm(uecm_3132, data_class = "ts")
#' resettest(uecm_3132_lm, power = 2)
#'
#' # Jarque-Bera test for normality
#' jarque.bera.test(residuals(uecm_3132))
#'
#' # CUSUM test for structural change detection
#' \dontrun{
#' # This produces an error.
#' # efp() does not understand special functions such as "d()" and "L()"
#' efp(uecm_3132$full_formula, data = uecm_3132$model)
#' }
#'
#' uecm_3132_lm_names <- to_lm(uecm_3132, fix_names = TRUE)
#' fluctuation <- efp(uecm_3132_lm_names$full_formula,
#' data = uecm_3132_lm_names$model)
#' sctest(fluctuation)
#' plot(fluctuation)
#'

uecm <- function(...) {
UseMethod("uecm")
Expand Down
2 changes: 1 addition & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ There were no ERRORs, WARNINGs or NOTEs.
I have also run R CMD check successfully on the downstream dependency package bootCT.

## Test environments
* local Linux Mint 20.1 (Ubuntu based), R 4.2.2
* local Linux Mint 20.1 (Ubuntu based), R 4.3.1
* win-builder (devel, release, old)
* R-hub builder macOS, R 4.3.0
5 changes: 5 additions & 0 deletions man/ardl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/denmark.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions man/multipliers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plot_delay.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/recm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0ccbf32

Please sign in to comment.