编辑:
我下面的第一个方法是在“checkresiduals()”函数上实现的。现在该函数默认返回输出值。
旧答案:
不幸的是,函数checkresiduals() 不返回值,只有prints() 它们。您可以通过不带括号的checkresiduals 来查看该功能。或者你查看开发者的github。
您可以通过在其中添加return() 来重写该函数。我只是复制粘贴函数并将其插入到最后:
checkresiduals <- function(object, lag, df=NULL, test, plot=TRUE, ...) {
showtest <- TRUE
if (missing(test)) {
if (is.element("lm", class(object))) {
test <- "BG"
} else {
test <- "LB"
}
showtest <- TRUE
}
else if (test != FALSE) {
test <- match.arg(test, c("LB", "BG"))
showtest <- TRUE
}
else {
showtest <- FALSE
}
# Extract residuals
if (is.element("ts", class(object)) | is.element("numeric", class(object))) {
residuals <- object
object <- list(method = "Missing")
}
else {
residuals <- residuals(object)
}
if (length(residuals) == 0L) {
stop("No residuals found")
}
if ("ar" %in% class(object)) {
method <- paste("AR(", object$order, ")", sep = "")
} else if (!is.null(object$method)) {
method <- object$method
} else if ("HoltWinters" %in% class(object)) {
method <- "HoltWinters"
} else if ("StructTS" %in% class(object)) {
method <- "StructTS"
} else {
method <- try(as.character(object), silent = TRUE)
if ("try-error" %in% class(method)) {
method <- "Missing"
} else if (length(method) > 1 | base::nchar(method[1]) > 50) {
method <- "Missing"
}
}
if (method == "Missing") {
main <- "Residuals"
} else {
main <- paste("Residuals from", method)
}
if (plot) {
suppressWarnings(ggtsdisplay(residuals, plot.type = "histogram", main = main, ...))
}
# Check if we have the model
if (is.element("forecast", class(object))) {
object <- object$model
}
if (is.null(object) | !showtest) {
return(invisible())
}
# Seasonality of data
freq <- frequency(residuals)
# Find model df
if(grepl("STL \\+ ", method)){
warning("The fitted degrees of freedom is based on the model used for the seasonally adjusted data.")
}
df <- modeldf(object)
if (missing(lag)) {
lag <- ifelse(freq > 1, 2 * freq, 10)
lag <- min(lag, round(length(residuals)/5))
lag <- max(df+3, lag)
}
if (!is.null(df)) {
if (test == "BG") {
# Do Breusch-Godfrey test
BGtest <- lmtest::bgtest(object, order = lag)
BGtest$data.name <- main
print(BGtest)
return(BGtest)
}
else {
# Do Ljung-Box test
LBtest <- Box.test(zoo::na.approx(residuals), fitdf = df, lag = lag, type = "Ljung")
LBtest$method <- "Ljung-Box test"
LBtest$data.name <- main
names(LBtest$statistic) <- "Q*"
print(LBtest)
cat(paste("Model df: ", df, ". Total lags used: ", lag, "\n\n", sep = ""))
return(LBtest)
}
}
}
您还需要来自github file 的modeldf() 函数
modeldf <- function(object, ...){
UseMethod("modeldf")
}
modeldf.Arima <- function(object, ...){
length(object$coef)
}
通过此解决方案,您可以使用原始的 checkresiduals 函数。现在您可以使用以下命令调用 p.value:
res_values <- checkresiduals(TS_FORECAST, plot = TRUE)
res_values$p.value
您也可以自己使用Ljung-Box 和Breusch-Godfrey test 而忽略checkresiduals() 函数,因为这是checkresiduals() 所做的。
我认为编辑checkresiduals() 函数是一种更方便的方法,因此您可以像习惯一样使用它。您可以将其粘贴到您的代码中,它应该可以完成工作。只要确保在调用函数之前声明了modeldf() 和modeldf().Arima。它也可以测试功能。
选项 2
因为有可能:
您可以使用capture.output() 捕获输出
capture.output(checkresiduals(TS_FORECAST, plot = FALSE))[5]
“Q* = 4.8322, df = 5, p 值 = 0.4367”
使用 grep 命令应该可以在不更改函数的情况下提取 p 值。由于我不熟悉 grep,因此我无法就此任务提供正确的答案。