Reputation: 1609
I have written Augmented Dickey-Fuller test code that takes into account the usage of same (Common) sub-Sample for all of the lag orders for the autoregressive process (Tech info: In all of the lag selection procedures in econometrics, same sub-sample must be used to determine the correct optimal minimum lag). However, even though the max parameter seems to be arbitrary, in reality it is not so since a programming trouble: The following code works only for max=14. Because:
I could not achieve how to place:
(for max=3) "x1d, x1l, x1d1l, x1d2l, x1d3l"
(for max=4) "x1d, x1l, x1d1l, x1d2l, x1d3l, x1d4l"
(for max=6), "x1d, x1l, x1d1l, x1d2l, x1d3l, x1d4l, x1d5l, x1d6l", etc. in the following code.
I want a string in certain pattern; and the length of the string will be determined by user (via max).
For those who are interested in the variables:
x1d := 1st difference of time series x
x1l := 1st lag of time series x
x1d1l := 1st lag of 1st difference of time series x (x's 1st-difference's 1st-lag!)
....
x3d5l := 5th lag of 3rd difference of x
x2l5d := 5th difference of 2nd lag of x
i.e. lag and difference operators are applied from left to right in variable naming.
adfcs <- function (t, max = 14, type = c("c")) {
x <- ts(t)
x1d = diff(x, differences=1)
x1l = lag(x, -1)
for (i in as.integer(1:max)) { # Here, I obtained all the lags that will be used in ADF regression
assign(paste(paste("x1d", i, sep=""), "l", sep=""), lag(x1d, -i))}
DLDlag = ts.intersect(x1d, x1l, x1d1l, x1d2l, x1d3l, x1d4l, x1d5l, x1d6l, x1d7l, x1d8l, x1d9l, x1d10l, x1d11l, x1d12l, x1d13l, x1d14l) # Here is the problem.
DLDlag.df = data.frame(ts.intersect(x1d, x1l, x1d1l, x1d2l, x1d3l, x1d4l, x1d5l, x1d6l, x1d7l, x1d8l, x1d9l, x1d10l, x1d11l, x1d12l, x1d13l, x1d14l), obspts =c(time(DLDlag))) # Here is the problem.
DifferenceLags = as.vector(names(DLDlag.df), mode="any")[3: (length(DLDlag.df)-1)]
lmresults = array(list())
SBCvalues = array(list())
AICvalues = array(list())
for (i in as.integer(0:max)) {
if (i == 0) { lmresults[[max+1]] = lm(as.formula(paste("x1d ~x1l")),data=DLDlag.df)
SBCvalues[[max+1]] = BIC(lmresults[[max+1]])
AICvalues[[max+1]] = AIC(lmresults[[max+1]]) }
if (i > 0) { lmresults[[i]] = lm(as.formula(paste("x1d ~ x1l+", paste(DifferenceLags[1:i], collapse="+"))),data=DLDlag.df)
SBCvalues[[i]] = BIC(lmresults[[i]])
AICvalues[[i]] = AIC(lmresults[[i]]) }
}
list(which.min(SBCvalues), which.min(AICvalues))
as.data.frame(cbind(SBCvalues, AICvalues))
if (which.min(SBCvalues)==max+1) {
scs=(max+2)-(0+1)
adfcs = unitrootTest(x[scs:length(x)], lags = 0, type = c("c"))
} else {
scs=(max+2)-(which.min(SBCvalues)+1)
adfcs = unitrootTest(x[scs:length(x)], lags =which.min(SBCvalues), type = c("c"))
}
adfcs
}
Note: in Eviews (version <=7.2), the same (common) sub-sample is not used in ADF tests and hence ADF of Eviews is wrong! The properly and correctly realization of above code in Eviews is that:
1. Double click a variable; View; unit root test; ADF; "level,intercept"; Lag Length: Automatic Selection Schwarz: Maximum Lags=14". (14 changes according to dataset)
2. Arrange sub-sample by removing first samples: Assume data is 1960Q1 2009Q4 (200 obs). Then first 14+1 obs are removed from the sample: Sample - sample range pairs: 1963Q4 2009Q4. After providing the same sub-sample will be used, the ADF is performed.
3. Double click the same variable once again; View; unit root test; ADF; "level,intercept"; Lag Length: Automatic Selection Schwarz: Maximum Lags=14". (Here a different number may appear other than 14 due to the effect of sub-sampling; delete that number in the box, and type 14). press OK.
Any help will be greatly appreciated?
Upvotes: 1
Views: 122
Reputation: 3473
you need some of R's "computing on the language" stuff to do this type of thing (i.e., programmatically construct a call to another function):
Try this:
x <- ts(cumsum(rnorm(100)))
max <- 5
x1d <- diff(x, differences=1)
x1l <- lag(x, -1)
x_names <- c("x1d", "x1l",
sapply(1:max, function(i) paste("x1d", i, "l", sep="")))
for (i in as.integer(1:max)) {
assign(x_names[i+2], lag(x1d, -i))
}
DLDlag <- do.call(ts.intersect, sapply(x_names, as.symbol))
DLDlag.df <- data.frame(DLDlag, obspts = c(time(DLDlag)))
Upvotes: 1