仅保存 tsdiag 中的一个图
Save only one plot from tsdiag
我只想从 tsdiag
函数中检索并保存三个绘图中的一个。
我怎样才能做到这一点?我一直在尝试使用附加参数 which
但绘图函数仍在返回整个诊断。
tsdiag(arima_model, which = 1) # does not work
我在 stats
的文档中也找不到任何内容。手动重现这些图很容易,但如果只得到其中一个就更好了。
R 是开源的,因此可以查看源代码并检查是否可以创建自己的用户修改函数。
tsdiag
函数可在包 stats、源文件 ama0.R
中找到。这是一个支持 which
参数的黑客版本:
## 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
您可以考虑包 sarima
中的 tsdiag.Sarima()
(https://cran.r-project.org/package=sarima), which provides the requested and further functionality, see https://geobosh.github.io/sarima/reference/tsdiag.Sarima.html。tsdiag.Sarima()
的另一个特点是它使用正确的自由度来计算 p 值,这是使用它的另一个原因。tsdiag.Sarima()
还提供了一些替代测试,请参阅其帮助页面或上面的 link。
例如,将 ARIMA 模型拟合到内置 AirPassengers
数据:
ap.arima <- arima(log(AirPassengers), order = c(0,1,1), seasonal = c(0,1,1))
这显示了一个类似于默认 tsdiag
的图表,但带有 d.f。根据 Ljung-Box 测试的要求:
tsdiag.Sarima(ap.arima)
这也显示了 Li-McLeod 检验(此处丢弃了残差):
tsdiag.Sarima(ap.arima, plot = 2:4)
参数 layout
可用于定义绘图的不同布局,有关详细信息,请参阅 ?layout
。在最简单的情况下,它是一个只有 1 列的矩阵。
这显示了自相关和 LB p 值:
tsdiag.Sarima(ap.arima, plot = 2:3, layout = list(matrix(1:2, nrow = 2)))
plot
当然可以指定不连续的地块。
这显示了残差的自相关和部分自相关
tsdiag.Sarima(ap.arima, plot = c(2,6), layout = list(matrix(1:2, nrow = 2)))
如果参数 plot
指定的地块多于布局可以容纳的地块,您将获得一个菜单来选择所需的地块:
tsdiag.Sarima(ap.arima, plot = 1:6, layout = list(matrix(1:2, nrow = 2)))
呈现如下内容:
Select a plot number or 0 to exit
1: residuals
2: acf of residuals
3: p values for Ljung-Box statistic
4: p values for Li-McLeod statistic
5: p values for Box-Pierce statistic
6: pacf of residuals
Selection:
layout
也允许您为地块分配不同数量的 space。这将垂直 space (40%) 的 2/(1+2+2) 分配给第二个和第三个图,只有 20% 分配给第一个(标准化残差):
tsdiag.Sarima(ap.arima, plot = c(1,2,6),
layout = list(matrix(1:3, nrow = 3),
heights = c(1,2,2)))
以上也适用于从 auto.arima
获得的对象。对上一个答案示例的修改使用 tsdiag.Sarima
:
library("forecast") # contains auto.arima
data(manaus, package = "boot")
m <- auto.arima(manaus)
tsdiag.Sarima(m) # all plots
tsdiag.Sarima(m, plot = 2, layout = list(1)) # only 2nd
tsdiag.Sarima(m, plot = 2:3, layout = list(matrix(1:2, nrow = 2))) # plot 2 & 3
我只想从 tsdiag
函数中检索并保存三个绘图中的一个。
我怎样才能做到这一点?我一直在尝试使用附加参数 which
但绘图函数仍在返回整个诊断。
tsdiag(arima_model, which = 1) # does not work
我在 stats
的文档中也找不到任何内容。手动重现这些图很容易,但如果只得到其中一个就更好了。
R 是开源的,因此可以查看源代码并检查是否可以创建自己的用户修改函数。
tsdiag
函数可在包 stats、源文件 ama0.R
中找到。这是一个支持 which
参数的黑客版本:
## 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
您可以考虑包 sarima
中的 tsdiag.Sarima()
(https://cran.r-project.org/package=sarima), which provides the requested and further functionality, see https://geobosh.github.io/sarima/reference/tsdiag.Sarima.html。tsdiag.Sarima()
的另一个特点是它使用正确的自由度来计算 p 值,这是使用它的另一个原因。tsdiag.Sarima()
还提供了一些替代测试,请参阅其帮助页面或上面的 link。
例如,将 ARIMA 模型拟合到内置 AirPassengers
数据:
ap.arima <- arima(log(AirPassengers), order = c(0,1,1), seasonal = c(0,1,1))
这显示了一个类似于默认 tsdiag
的图表,但带有 d.f。根据 Ljung-Box 测试的要求:
tsdiag.Sarima(ap.arima)
这也显示了 Li-McLeod 检验(此处丢弃了残差):
tsdiag.Sarima(ap.arima, plot = 2:4)
参数 layout
可用于定义绘图的不同布局,有关详细信息,请参阅 ?layout
。在最简单的情况下,它是一个只有 1 列的矩阵。
这显示了自相关和 LB p 值:
tsdiag.Sarima(ap.arima, plot = 2:3, layout = list(matrix(1:2, nrow = 2)))
plot
当然可以指定不连续的地块。
这显示了残差的自相关和部分自相关
tsdiag.Sarima(ap.arima, plot = c(2,6), layout = list(matrix(1:2, nrow = 2)))
如果参数 plot
指定的地块多于布局可以容纳的地块,您将获得一个菜单来选择所需的地块:
tsdiag.Sarima(ap.arima, plot = 1:6, layout = list(matrix(1:2, nrow = 2)))
呈现如下内容:
Select a plot number or 0 to exit
1: residuals
2: acf of residuals
3: p values for Ljung-Box statistic
4: p values for Li-McLeod statistic
5: p values for Box-Pierce statistic
6: pacf of residuals
Selection:
layout
也允许您为地块分配不同数量的 space。这将垂直 space (40%) 的 2/(1+2+2) 分配给第二个和第三个图,只有 20% 分配给第一个(标准化残差):
tsdiag.Sarima(ap.arima, plot = c(1,2,6),
layout = list(matrix(1:3, nrow = 3),
heights = c(1,2,2)))
以上也适用于从 auto.arima
获得的对象。对上一个答案示例的修改使用 tsdiag.Sarima
:
library("forecast") # contains auto.arima
data(manaus, package = "boot")
m <- auto.arima(manaus)
tsdiag.Sarima(m) # all plots
tsdiag.Sarima(m, plot = 2, layout = list(1)) # only 2nd
tsdiag.Sarima(m, plot = 2:3, layout = list(matrix(1:2, nrow = 2))) # plot 2 & 3