我想通过使用包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)
答案 0 :(得分:1)
解决方案是修改包plot
中的urca
函数,以获取类ur.za
的对象。可以使用
findMethods(plot)
感兴趣的函数名为ur.za#missing
(findMethods(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)
我希望它可以帮到你。