Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
karakastarik committed Aug 23, 2021
1 parent a832ecc commit ba0f58c
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 9 deletions.
7 changes: 6 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ Authors@R:
family = "Karakas",
role = c("aut", "cre"),
email = "[email protected]")
Description: Automating time series stationarity tests. This package automates Kwiatkowski–Phillips–Schmidt–Shin (KPSS), Augmented Dickey–Fuller (ADF) and Phillips–Perron (PP) unit root tests and saves the results as a dataframe.
Description: Automating time series stationarity tests. This package automates Kwiatkowski–Phillips–Schmidt–Shin (KPSS), Augmented Dickey–Fuller (ADF) and Phillips–Perron (PP) unit root tests by calculating every possible situation for lag length, type and significance level (1%, 2.5%, 5% and 10%) and saves the results as a dataframe.
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Depends: R (>= 4.0.0), dplyr, urca
Imports: dplyr,urca
Language:en-US
License: GPL-2
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(summary_stationarity)
S3method(summary,adf)
S3method(summary,all)
S3method(summary,kpss)
S3method(summary,pp)
45 changes: 45 additions & 0 deletions R/summary-adf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' @title ADF Test Results.
#'
#' @description
#' This function computes ADF statistics and critical values and aggregate them in one dataframe for all possible situations.
#'
#' @param model fitted \code{lm(y ~ x)} object
#' @param lag Integer lag length. For example, if the value is \code{10}, results will come for lag lengths of \code{1:10}.
#' @export
#' @examples
#' summary.adf(ts_model,10)

summary.adf <- function(model,lag) {

if (lag>20) {
stop("Lag can not be greater than 20.")
}

variables <-c(as.data.frame(model$model),as.data.frame(model$residuals)) %>% as.data.frame()

adf_type <- c("none","drift","trend")
adf = list()
for (variable in names(variables)) {
for (type in adf_type) {
for (i in 1:10) {
adf_new <- ur.df(variables[[variable]],type = type, lags = i)
adf_result <- as.data.frame(cbind(Test="ADF",Type=type,Variable=variable,Lag=i,"Statistic"=adf_new@teststat[1],as.data.frame(adf_new@cval)[1,])) %>%
mutate(Statistic=as.numeric(.data[["Statistic"]]),
"1pct"=as.numeric(.data[["1pct"]]),
"5pct"=as.numeric(.data[["5pct"]]),
"10pct"=as.numeric(.data[["10pct"]]))
row.names(adf_result) <- NULL
adf_result <- adf_result %>% mutate("2.5pct"="Not avaiable for ADF") %>% relocate("2.5pct", .before = "10pct")
adf_result["10% result"] <- ifelse(abs(adf_result[["Statistic"]])>abs(adf_result[["10pct"]]),"pass","fail")
adf_result["5% result"] <- ifelse(abs(adf_result[["Statistic"]])>abs(adf_result[["5pct"]]),"pass","fail")
adf_result["2.5% result"] <- "Not available for ADF"
adf_result["1% result"] <- ifelse(abs(adf_result[["Statistic"]])>abs(adf_result[["1pct"]]),"pass","fail")
adf[[paste0(i,type,variable)]] <- adf_result # add it to list
}
}
}
adf_full <- do.call(rbind, adf)
row.names(adf_full) <- NULL
return(adf_full)

}
4 changes: 2 additions & 2 deletions R/summary_stationarity.R → R/summary-all.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#' @param lag Integer lag length. For example, if the value is \code{10}, results will come for lag lengths of \code{1:10}.
#' @export
#' @examples
#' summary_stationarity(ts_model,10)
#' summary.all(ts_model,10)

summary_stationarity <- function(model,lag) {
summary.all <- function(model,lag) {

if (lag>20) {
stop("Lag can not be greater than 20.")
Expand Down
45 changes: 45 additions & 0 deletions R/summary-kpss.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' @title KPSS Test result.
#'
#' @description
#' This function computes KPSS statistics and critical values and aggregate them in one dataframe for all possible situations.
#'
#' @param model fitted \code{lm(y ~ x)} object
#' @param lag Integer lag length. For example, if the value is \code{10}, results will come for lag lengths of \code{1:10}.
#' @export
#' @examples
#' summary.kpss(ts_model,10)

summary.kpss <- function(model,lag) {

if (lag>20) {
stop("Lag can not be greater than 20.")
}

variables <-c(as.data.frame(model$model),as.data.frame(model$residuals)) %>% as.data.frame()

kpss_type <-c("mu","tau")
kpss = list()
for (variable in names(variables)) {
for (type in kpss_type) {
for (i in 1:lag) {
kpss_new <- ur.kpss(variables[[variable]],type = type, use.lag=i)
kpss_result <- as.data.frame(cbind(Test="KPSS",Type=type,Variable=variable,Lag=i,"Statistic"=kpss_new@teststat[1],kpss_new@cval)) %>%
mutate(Statistic=as.numeric(.data[["Statistic"]]),
"1pct"=as.numeric(.data[["1pct"]]),
"5pct"=as.numeric(.data[["5pct"]]),
"10pct"=as.numeric(.data[["10pct"]]))
row.names(kpss_result) <- NULL
kpss_result[["Type"]] <- ifelse(kpss_result[["Type"]]=="mu","constant","trend")
kpss_result["10% result"] <- ifelse(kpss_result[["Statistic"]]<kpss_result[["10pct"]],"pass","fail")
kpss_result["5% result"] <- ifelse(kpss_result[["Statistic"]]<kpss_result[["5pct"]],"pass","fail")
kpss_result["2.5% result"] <- ifelse(kpss_result[["Statistic"]]<kpss_result[["2.5pct"]],"pass","fail")
kpss_result["1% result"] <- ifelse(kpss_result[["Statistic"]]<kpss_result[["1pct"]],"pass","fail")
kpss[[paste0(i,type,variable)]] <- kpss_result # add it to list
}
}
}
kpss_full <- do.call(rbind, kpss)
row.names(kpss_full) <- NULL
return(kpss_full)

}
46 changes: 46 additions & 0 deletions R/summary-pp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' @title PP Test Results.
#'
#' @description
#' This function computes PP statistics and critical values and aggregate them in one dataframe for all possible situations.
#'
#' @param model fitted \code{lm(y ~ x)} object
#' @param lag Integer lag length. For example, if the value is \code{10}, results will come for lag lengths of \code{1:10}.
#' @export
#' @examples
#' summary.pp(ts_model,10)

summary.pp <- function(model,lag) {

if (lag>20) {
stop("Lag can not be greater than 20.")
}

variables <-c(as.data.frame(model$model),as.data.frame(model$residuals)) %>% as.data.frame()

pp_type <- c("constant","trend")
pp = list()
for (variable in names(variables)) {
for (type in pp_type) {
for (i in 1:10) {
pp_new <- ur.pp(variables[[variable]],type = c("Z-tau"), model=type, use.lag=i)
pp_result <- as.data.frame(cbind(Test="PP",Type=type,Variable=variable,Lag=i,"Statistic" = pp_new@teststat,pp_new@cval)) %>%
mutate(Statistic=as.numeric(.data[["Statistic"]]),
"1pct"=as.numeric(.data[["1pct"]]),
"5pct"=as.numeric(.data[["5pct"]]),
"10pct"=as.numeric(.data[["10pct"]]))
row.names(pp_result) <- NULL
pp_result <- pp_result %>% mutate("2.5pct"="Not avaiable for PP") %>% relocate("2.5pct", .before = "10pct")
pp_result["10% result"] <- ifelse(abs(pp_result[["Statistic"]])>abs(pp_result[["10pct"]]),"pass","fail")
pp_result["5% result"] <- ifelse(abs(pp_result[["Statistic"]])>abs(pp_result[["5pct"]]),"pass","fail")
pp_result["2.5% result"] <- "Not available for PP"
pp_result["1% result"] <- ifelse(abs(pp_result[["Statistic"]])>abs(pp_result[["1pct"]]),"pass","fail")
pp[[paste0(i,type,variable)]] <- pp_result # add it to list
}
}
}
pp_full <- do.call(rbind, pp)
row.names(pp_full) <- NULL

return(pp_full)

}
19 changes: 19 additions & 0 deletions man/summary.adf.Rd

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

10 changes: 5 additions & 5 deletions man/summary_stationarity.Rd → man/summary.all.Rd

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

19 changes: 19 additions & 0 deletions man/summary.kpss.Rd

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

19 changes: 19 additions & 0 deletions man/summary.pp.Rd

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

17 changes: 17 additions & 0 deletions stationarityR.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source

0 comments on commit ba0f58c

Please sign in to comment.