Skip to content

Commit

Permalink
- Added parameters fix_names and data_class to the to_lm() func…
Browse files Browse the repository at this point in the history
…tion.

- Added post-estimation testing examples

- Minor documentation fixes
  • Loading branch information
Natsiopoulos committed Jul 31, 2023
1 parent 9a24034 commit e913045
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 40 deletions.
4 changes: 2 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 Down Expand Up @@ -38,5 +38,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
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
85 changes: 65 additions & 20 deletions R/to_lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@
#' 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. Default is FALSE.
#' @param data_class 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 +61,73 @@
#' 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 = c(NULL, "ts"), ...) {
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)
}
names(objmodel)[1] <- y
if (data_class == "ts") {
objmodel <- ts(objmodel, start = start(objmodel[,1]), frequency = frequency(objmodel[,1]))
}
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
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.

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.

43 changes: 41 additions & 2 deletions man/to_lm.Rd

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

Loading

0 comments on commit e913045

Please sign in to comment.