[R-sig-finance] fSeries - adfTest / unitrootTest

Diethelm Wuertz wuertz at itp.phys.ethz.ch
Mon Dec 13 01:07:14 CET 2004



# Here comes the solution:

require(fSeries)

# A time series which contains no unit-root:
x = rnorm(1000)
x.ts = as.ts(x) 

# A time series which contains a unit-root:
y = diffinv(x)
y.ts = diffinv(x.ts)

# Use: tsadfTest() in Rmetrics this is Adrian Trapletti's implementation:

tsadfTest(x)
# or:
tsadfTest(y)


tsadfTest(x.ts)
# or:
tsadfTest(y.ts)


# Rmetrics Functions:


# First, adfTest() considers in contrast to tsadfTest() all three
# types "nc", "c" and "ct". This is the main difference!

# Second, unitrootTest() does the same as adfTest() but uses the
# statistic as implemented by McKinnon (Response Surface Approach).

# Unfortunately, the list of the Return Value of both functions assigns
# a non-defined variable to the slot data.name:
#
# new("fURTEST", call = CALL, data = as.data.frame(x),
#      data.name = test$DNAME, test = test, title = as.character(title),
#      description = as.character(description))
#
# should read:
#
# new("fURTEST", call = CALL, data = as.data.frame(x),
#      data.name = test$data.name, test = test, title = 
as.character(title),
#      description = as.character(description))


# here are the correct versions:


adfTest =
function (x, type = c("nc", "c", "ct"), lags = 1)
{
    if (ncol(as.matrix(x)) > 1)
        stop("x is not a vector or univariate time series")
    if (any(is.na(x)))
        stop("NAs in x")
    if (lags < 0)
        stop("lags negative")
    doprint = FALSE
    CALL = match.call()
    DNAME = deparse(substitute(x))
    type = type[1]
    x.name = deparse(substitute(x))
    lags = lags + 1
    y = diff(x)
    n = length(y)
    z = embed(y, lags)
    y.diff = z[, 1]
    y.lag.1 = x[lags:n]
    tt = lags:n
    if (lags > 1) {
        y.diff.lag = z[, 2:lags]
        if (type == "nc") {
            res = lm(y.diff ~ y.lag.1 - 1 + y.diff.lag)
        }
        if (type == "c") {
            res = lm(y.diff ~ y.lag.1 + 1 + y.diff.lag)
        }
        if (type == "ct") {
            res = lm(y.diff ~ y.lag.1 + 1 + tt + y.diff.lag)
        }
    }
    else {
        if (type == "nc") {
            res = lm(y.diff ~ y.lag.1 - 1)
        }
        if (type == "c") {
            res = lm(y.diff ~ y.lag.1 + 1)
        }
        if (type == "ct") {
            res = lm(y.diff ~ y.lag.1 + 1 + tt)
        }
    }
    res.sum = summary(res)
    if (doprint)
        print(res.sum)
    if (type == "nc")
        coefNum = 1
    else coefNum = 2
    STAT = res.sum$coefficients[coefNum, 1]/res.sum$coefficients[coefNum,
        2]
    if (type == "nc")
        table = cbind(c(-2.66, -2.26, -1.95, -1.6, +0.92, +1.33,
            +1.7, +2.16), c(-2.62, -2.25, -1.95, -1.61, +0.91,
            +1.31, +1.66, +2.08), c(-2.6, -2.24, -1.95, -1.61,
            +0.9, +1.29, +1.64, +2.03), c(-2.58, -2.23, -1.95,
            -1.62, +0.89, +1.29, +1.63, +2.01), c(-2.58, -2.23,
            -1.95, -1.62, +0.89, +1.28, +1.62, +2), c(-2.58,
            -2.23, -1.95, -1.62, +0.89, +1.28, +1.62, +2))
    if (type == "c")
        table = cbind(c(-3.75, -3.33, -3, -2.63, -0.37, +0, +0.34,
            +0.72), c(-3.58, -3.22, -2.93, -2.6, -0.4, -0.03,
            +0.29, +0.66), c(-3.51, -3.17, -2.89, -2.58, -0.42,
            -0.05, +0.26, +0.63), c(-3.46, -3.14, -2.88, -2.57,
            -0.42, -0.06, +0.24, +0.62), c(-3.44, -3.13, -2.87,
            -2.57, -0.43, -0.07, +0.24, +0.61), c(-3.43, -3.12,
            -2.86, -2.57, -0.44, -0.07, +0.23, +0.6))
    if (type == "ct")
        table = cbind(c(-4.38, -3.95, -3.6, -3.24, -1.14, -0.8,
            -0.5, -0.15), c(-4.15, -3.8, -3.5, -3.18, -1.19,
            -0.87, -0.58, -0.24), c(-4.04, -3.73, -3.45, -3.15,
            -1.22, -0.9, -0.62, -0.28), c(-3.99, -3.69, -3.43,
            -3.13, -1.23, -0.92, -0.64, -0.31), c(-3.98, -3.68,
            -3.42, -3.13, -1.24, -0.93, -0.65, -0.32), c(-3.96,
            -3.66, -3.41, -3.12, -1.25, -0.94, -0.66, -0.33))
    table = t(table)
    tablen = dim(table)[2]
    tableT = c(25, 50, 100, 250, 500, 1e+05)
    tablep = c(0.01, 0.025, 0.05, 0.1, 0.9, 0.95, 0.975, 0.99)
    tableipl = numeric(tablen)
    for (i in (1:tablen)) tableipl[i] = approx(tableT, table[,
        i], n, rule = 2)$y
    PVAL = approx(tableipl, tablep, STAT, rule = 2)$y
    if (is.na(approx(tableipl, tablep, STAT, rule = 1)$y)) {
        if (PVAL == min(tablep)) {
            warning("p-value smaller than printed p-value")
        }
        else {
            warning("p-value greater than printed p-value")
        }
    }
    PARAMETER = lags - 1
    names(PARAMETER) = "Lag order"
    METHOD = "Augmented Dickey-Fuller Test"
    names(STAT) = "Dickey-Fuller"
    test = list(statistic = STAT, parameter = PARAMETER, p.value = PVAL,
        method = METHOD, data.name = DNAME)
    class(test) = c("list", "htest")
    title = test$method
    description = date()
    # BUG FIXED IMN THE FOLLOWING LINE:
    new("fURTEST", call = CALL, data = as.data.frame(x), data.name = 
test$data.name,
        test = test, title = as.character(title), description = 
as.character(description))
}



unitrootTest =
function (x, trend = c("nc", "c", "ct"), statistic = c("t", "n"),
    method = "adf", lags = 1)
{
    if (class(x) == "timeSeries")
        x = x at Data
    CALL = match.call()
    test = .unitrootADF(x = x, trend = trend[1], statistic = statistic[1],
        lags = lags)
    class(test) = c("list", "htest")
    title = test$method
    description = date()
    # BUG FIXED IMN THE FOLLOWING LINE:
    new("fURTEST", call = CALL, data = as.data.frame(x), data.name = 
test$data.name,
        test = test, title = as.character(title), description = 
as.character(description))
}


# Now try:

adfTest(x)
unitrootTest(x)

# The Bug will be fixed in the next Version of Rmetrics.


# I also highly recommend the package urca written by Bernhard Pfaff.

# Best Regards
# Diethelm Wuertz



badegeeter at zonnet.nl wrote:

>Hello,
>
>I'm sorry if I'm asking a very basic question, I am not so familiar with
>R..At the moment I have a script which uses ADF test from the tseries
>package. Now I am trying to adjust my script to use the fSeries package.
>So, I would like to use the function adfTest and/or unitrootTest.
>As an input for these functions I have a vector 'x' with observations. When
>trying the tests on 'x' I get the following message:
>
>'Error in validObject(.Object) : Invalid "fURTEST" object: Invalid object
>for slot "data.name" in class "fURTEST": got class "NULL", should be or
>extend class "character"'
>
>What am I doing wrong ?
>
>greetings,
>
>Bastiaan
>
>_______________________________________________
>R-sig-finance at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>



More information about the R-sig-finance mailing list