Skip to content

Instantly share code, notes, and snippets.

@phrmendes
Created January 12, 2023 17:14
Show Gist options
  • Save phrmendes/2c0102980736d4e501365bf4eb02c982 to your computer and use it in GitHub Desktop.
Save phrmendes/2c0102980736d4e501365bf4eb02c982 to your computer and use it in GitHub Desktop.
Unit root test in R
library(urca)
has_unit_root <- function(serie) {
trend_model <- urca::ur.df(serie, type = "trend", selectlags = "BIC")
summary_model <- summary(trend_model)
test_value <- summary_model@teststat[1]
tau_3 <- summary_model@cval[1, 2]
if (test_value < tau_3) {
return(FALSE)
} else {
test_value <- summary_model@teststat[3]
phi_3 <- summary_model@cval[3, 2]
if (test_value > phi_3) {
return(FALSE)
} else {
drift_model <- urca::ur.df(serie, type = "drift", selectlags = "BIC")
summary_model <- summary(drift_model)
test_value <- summary_model@teststat[1]
tau_2 <- summary_model@cval[1, 2]
if (test_value < tau_2) {
return(FALSE)
} else {
test_value <- summary_model@teststat[2]
phi_1 <- summary_model@cval[2, 2]
if (test_value > phi_1) {
return(FALSE)
} else {
basic_model <- urca::ur.df(serie, type = "none", selectlags = "BIC")
summary_model <- summary(basic_model)
test_value <- summary_model@teststat[1]
tau_1 <- summary_model@cval[1, 2]
if (test_value < tau_1) {
return(FALSE)
} else {
return(TRUE)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment