I am attempting to write an R function to aid in time series diagnostics.
The tsdiag() works well, but I would prefer to get a plot with ACF, PACF,
and Ljung-Box p-values of residuals. In my attempt to get to that point, I
am writing a function to calculate Ljung-Box p-values for residuals at
various lag distances.
ljung<-function(fit)
for(i in c(1:24,36,48))
{box<-(Box.test(fit$residuals, lag=i, type="Ljung-Box"))
print(c(i, box$p.value))}
This is one of my first R function writing attempts, so my apologies if
there is an obvious mistake. The above function produces the desired effect
in printing the lags and p-values to be plotted [where fit is the result of
arima()]; however I cannot seem to get the output stored in a data.frame for
subsequent plotting. I have tried storing the output using various methods
including data.frame, write.table, cat, capture.output, all with no success.
e.g:
ljung.out<-capture.output(print(c(i, box$p.value)))
#I saw a suggestion similar to this one in a previous post to the list...
Any hints on how I can get the output stored so that I may plot it later? I
am using v 1.9.1, RGui for Windows.
Thanks,
Andrew Kniss
Assistant Research Scientist
University of Wyoming
Dept. 3354
1000 E. University Ave.
Laramie, WY 82071
(307) 766-3949
akniss at uwyo.edu
Below is an arima fit that I have used in testing the function.