我正在尝试绘制通过自定义函数修改的多个ACF图。但是,无论何时打开JPEG,都只能正确绘制两个图。其他两个都是使用基本ACF功能生成的,没有进行任何调整(参见图片)。
我通过以下代码段调用绘图和JPEG设备
#EndoVars is a DF with 5 columns/variables
#dVar is an estimated VAR model of which I need to extract the residuals
#Check how many rows we need to plot every variable
if (length(colnames(EndoVars)) %% 2 == 0) {
plotrows <- length(colnames(EndoVars)) / 2
} else {
plotrows <- ((length(colnames(EndoVars)))+1) / 2
}
#Open JPEG device and plot the variables
jpeg('acfplot.jpg')
par(mfrow = c(plotrows,2))
for (m in 1:length(colnames(EndoVars))) {
name <- colnames(EndoVars)[m]
resids <- dVar[['varresult']][[name]][['residuals']]
acfplot(resids, name)
}
dev.off()
其中acfplot()的定义如下
acfplot <- function(var, cmain){
a <- acf(var)
#build the plot
plot(a$acf[2:13],
type = 'h',
main = cmain,
xlab = 'Lag',
ylab = 'ACF',
ylim = c(-0.5, 0.5),
las = 1,
xaxt = 'n')
abline(h = 0)
x <- c(1:12)
y <- c(1:12)
axis(1, at = x, labels = y)
critval <- qnorm(1.95 / 2) / sqrt(length(var))
#add approximated 5% critical levels
abline(h = c(
(critval), - (critval)), lty = c(2, 2))
}
我碰到了this帖子,以了解如何调用修改后的绘图功能,但是除了用功能和循环替换解决方案中的单个绘图外,我看不到任何区别。 / p>
在执行此操作时,我还应该考虑其他事项吗?