如果是打算绘制像Eviews或者stata里的单位圆来判断VAR系统的稳定性,可以使用vars包里的roots()函数计算出伴随矩阵的特征值,就可以进行判断了。如果打算绘制单位圆,可使用如下的自编函数实现。
### VAR系统稳定性判别图的绘制
##compute the unit roots of VAR model
varstability <- function(x) {
# Calculate the eigenvalues & Modulus of the VAR model.
Eigenvalue<-roots(x, modulus = FALSE)
Modulus<-roots(x, modulus = TRUE)
res <- data.frame(Eigenvalue, Modulus)
class(res) <- c("varstability", "data.frame")
return(res)
}
## plot VAR stability
plot.varstability <- function(x, ...) {
x1 <- seq(-1,1,length=1000)
y1 <- sqrt(1 - x1^2)
y2 <- -sqrt(1 - x1^2)
dfroots <- data.frame(Real=Re(x$Eigenvalue),
Imaginary=Im(x$Eigenvalue))
limits <- max(abs(dfroots))
if (limits < 1) {
limits <- c(-1,1)
} else {
limits <- c(-ceiling(limits), ceiling(limits))
}
ggplot2::ggplot(data.frame(x=x1,y1=y1,y2=y2)) + #Using the results from earlier
ggplot2::geom_hline(ggplot2::aes(yintercept=0)) +
ggplot2::geom_vline(ggplot2::aes(xintercept=0)) +
ggplot2::geom_line(ggplot2::aes(x=x,y=y1)) + # top of circle
ggplot2::geom_line(ggplot2::aes(x=x,y=y2)) +
ggplot2::geom_point(ggplot2::aes_(x=~Real, y=~Imaginary),
dfroots, size = 2,col="blue") +
ggplot2::scale_y_continuous("Imaginary", limits=c(-1,1)) +
ggplot2::scale_x_continuous("Real", limits=c(-1,1)) + ggplot2::ggtitle("Roots of the companion matrix")
}
var_stab<-varstability(out.var2)
plot(var_stab)
可以绘制出单位圆如下,其中out.vars是使用VAR()函数生成的VAR对象。
|