Zivot和安德鲁测试情节

时间:2017-06-11 00:27:30

标签: r

我想通过使用包urca制作ur.za的图,但是它在x轴上给出数值而不是年数,无论如何都要在x轴上放几年。这是(urca)的例子封装

library(urca)
data(nporg)
gnp <- na.omit(nporg[, "gnp.r"])
za.gnp <- ur.za(gnp, model="both", lag=2)
summary(za.gnp)
plot(za.gnp)

enter image description here

1 个答案:

答案 0 :(得分:1)

解决方案是修改包plot中的urca函数,以获取类ur.za的对象。可以使用

找到此功能
findMethods(plot)

感兴趣的函数名为ur.za#missingfindMethods(plot)给出的列表中的第9位):

findMethods(plot)@names
findMethods(plot)@.Data[[9]]

这是功能:

Method Definition:

function (x, y, ...) 
{
    .local <- function (x) 
    {
        oldpar <- par(no.readonly = TRUE)
        on.exit(par(oldpar))
        par(mfrow = c(1, 1))
        yvals <- sort(c(x@cval, x@tstats))
        n <- length(x@y)
        xvals <- pretty(1:n)
        plot.ts(x@tstats, main = "Zivot and Andrews Unit Root Test", 
            ylab = "t-statistics for lagged endogenous variable", 
            ylim = c(min(yvals), max(yvals)))
        abline(h = x@cval, col = c("red", "blue", "seagreen"))
        if (x@teststat < x@cval[3]) {
            abline(v = x@bpoint, col = "red", lty = 2)
        }
        mtext(paste("Model type:", x@model, sep = " "), side = 1, 
            line = 4)
        legend(x = n, y = max(yvals), c("1% c.v.", "2.5% c.v.", 
            "5% c.v."), col = c("red", "blue", "seagreen"), xjust = 1, 
            yjust = 1, lty = 1, horiz = TRUE, cex = 0.66, bty = "n")
    }
    .local(x, ...)
}
<environment: namespace:urca>

Signatures:
        x       y        
target  "ur.za" "missing"
defined "ur.za" "missing"

以下是应解决问题的plot函数的修改版本:

plot.ur.za <- function (Time, x, ...) 
{
        oldpar <- par(no.readonly = TRUE)
        on.exit(par(oldpar))
        par(mfrow = c(1, 1))
        yvals <- sort(c(x@cval, x@tstats))
        #xvals <- pretty(1:n)
        plot.ts(Time, x@tstats, main = "Zivot and Andrews Unit Root Test", 
            ylab = "t-statistics for lagged endogenous variable", type="l",
            ylim = c(min(yvals), max(yvals)), xy.labels=F, xy.lines=T)
        abline(h = x@cval, col = c("red", "blue", "seagreen"))
        if (x@teststat < x@cval[3]) {
            abline(v = Time[x@bpoint], col = "red", lty = 2)
        }
        mtext(paste("Model type:", x@model, sep = " "), side = 1, 
            line = 4)
        n <- length(Time)
        legend(x = Time[n], y = max(yvals), c("1% c.v.", "2.5% c.v.", 
            "5% c.v."), col = c("red", "blue", "seagreen"), xjust = 1, 
            yjust = 1, lty = 1, horiz = TRUE, cex = 0.66, bty = "n")
}

和生成情节的代码:

library(urca)
data(nporg)
gnp <- na.omit(nporg[, c("gnp.r","year")])
za.gnp <- ur.za(gnp$gnp.r, model="both", lag=2)
plot(za.gnp)

yrs <- gnp$year[-length(gnp$year)]
plot.ur.za(Time=yrs, x=za.gnp)

enter image description here

我希望它可以帮到你。