编辑:
我的第一种方法已经被实现在'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
}
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, ...))
}
if (is.element("forecast", class(object))) {
object <- object$model
}
if (is.null(object) | !showtest) {
return(invisible())
}
freq <- frequency(residuals)
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") {
BGtest <- lmtest::bgtest(object, order = lag)
BGtest$data.name <- main
print(BGtest)
return(BGtest)
}
else {
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文件中获得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-value = 0.4367"
使用grep命令可以在不更改函数的情况下提取p值。由于我不熟悉grep,因此无法对此任务提供适当的答案。