x <- rnorm(100)
LjungBox(x)
##############################################################
## Measurements of the annual flow of the river Nile at Aswan
## from the years 1871 to 1970:
##############################################################
fit <- arima(Nile, c(1, 0, 1))
lags <- c(5, 10, 20)
## Apply the univariate test statistic on the fitted model
LjungBox(fit, lags) ## Correct
LjungBox(fit, lags, order = 2) ## Correct
## Apply the test statistic on the residuals and set order = 2
res <- resid(fit)
LjungBox(res, lags) ## Wrong
LjungBox(res, lags, order = 2) ## Correct
#to save time the other examples are not run
## Not run:
# ##############################################################
# ## Quarterly, west German investment, income, and consumption
# ## from first quarter of 1960 to fourth quarter of 1982:
# ##############################################################
# data(WestGerman)
# DiffData <- matrix(numeric(3 * 91), ncol = 3)
# for (i in 1:3)
# DiffData[, i] <- diff(log(WestGerman[, i]), lag = 1)
# fit <- ar.ols(DiffData, intercept = TRUE, order.max = 2)
# lags <- c(5,10)
# ## Apply the test statistic on the fitted model
# LjungBox(fit,lags) ## Correct
# ## Apply the test statistic on the residuals where order = 2
# res <- ts((fit$resid)[-(1:2), ])
# LjungBox(res,lags) ## Wrong
# LjungBox(res,lags,order = 2) ## Correct
# ##############################################################
# ## Monthly log stock returns of Intel corporation data
# ## Test for ARCH Effects
# ##############################################################
# monthintel <- as.ts(monthintel)
# LjungBox(monthintel) ## Usual test
# LjungBox(monthintel,SquaredQ=TRUE) ## Test for ARCH effects
# ##############################################################
# ## Write a function to fit a model
# ## Apply portmanteau test on fitted obj with class "list"
# ##############################################################
# ## Example 1
# library("FitAR")
# FitModel <- function(data){
# fit <- FitAR(z=data,p=2)
# p <- length(fit$phiHat)
# order <- p
# res <- fit$res
# list(res=res,order=order)
# }
# Fit <- FitModel(Nile)
# LjungBox(Fit)
# ##
# ## Example 2
# library("TSA")
# FitModel <- function(data){
# fit <- TSA::tar(y=log(data),p1=4,p2=4,d=3,a=0.1,b=0.9,print=FALSE)
# res <- ts(fit$std.res)
# p1 <- fit$p1
# p2 <- fit$p2
# order <- max(p1, p2)
# parSpec <- list(res=res,order=order)
# parSpec
# }
# data(prey.eq)
# Fit <- FitModel(prey.eq)
# LjungBox(Fit)
# ## End(Not run)
Run the code above in your browser using DataLab