我想從tsdiag函式中檢索并保存到檔案中的三個繪圖中的一個。

我怎樣才能做到這一點?我一直在嘗試使用其他引數,which但繪圖功能仍在回傳整個診斷。
tsdiag(arima_model, which = 1) # does not work
我在兩者的檔案中stats都找不到任何內容。手動復制這些圖很容易,但最好只得到其中一個。
uj5u.com熱心網友回復:
R是開源的,因此可以查看源代碼并檢查是否可以創建自己的用戶修改函式。
該tsdiag函式可在包stats 中找到,源檔案ama0.R. 這是一個支持which-argument的黑客版本:
## modified from R package stats, file `ama0.R`
tsdiag.Arima <- tsdiag.arima0 <- function(object, gof.lag = 10, which = 1L:3L, ...) {
## plot standardized residuals, acf of residuals, Ljung-Box p-values
oldpar <- par(mfrow = c(length(which), 1))
on.exit(par(oldpar))
rs <- object$residuals
if (1L %in% which) {
stdres <- rs/sqrt(object$sigma2)
plot(stdres, type = "h", main = "Standardized Residuals", ylab = "")
abline(h = 0)
}
if (2L %in% which) {
acf(object$residuals, plot = TRUE, main = "ACF of Residuals",
na.action = na.pass)
}
if (3L %in% which) {
nlag <- gof.lag
pval <- numeric(nlag)
for(i in 1L:nlag) pval[i] <- Box.test(rs, i, type="Ljung-Box")$p.value
plot(1L:nlag, pval, xlab = "lag", ylab = "p value", ylim = c(0,1),
main = "p values for Ljung-Box statistic")
abline(h = 0.05, lty = 2, col = "blue")
}
}
現在我們可以測驗一下:
library("forecast") # contains auto.arima
library("boot") # contains manaus data set
m <- auto.arima(manaus)
tsdiag(m) # all plots
tsdiag(m, which=1) # only 2nd
tsdiag(m, which=2:3) # plot 2 and 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/364199.html
