x <- rnorm(100)
MahdiMcLeod(x) ## univariate test
x <- cbind(rnorm(100),rnorm(100))
MahdiMcLeod(x) ## multivariate test
##
##
## Annual flow of the river Nile at Aswan - 1871 to 1970
fit <- arima(Nile, c(1, 0, 1))
lags <- c(5, 10, 20)
## Apply the univariate test statistic on the fitted model
MahdiMcLeod(fit, lags) ## Correct (no need to specify fitdf)
MahdiMcLeod(fit, lags, fitdf = 2) ## Correct
## Apply the test statistic on the residuals and set fitdf = 2
res <- resid(fit)
MahdiMcLeod(res, lags) ## Wrong (fitdf is needed!)
MahdiMcLeod(res, lags, fitdf = 2) ## Correct
##
##
## Quarterly, west German investment, income, and consumption from 1960 Q1 to 1982 Q4
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
MahdiMcLeod(fit,lags) ## Correct (no need to specify fitdf)
## Apply the test statistic on the residuals where fitdf = 2
res <- ts((fit$resid)[-(1:2), ])
MahdiMcLeod(res,lags) ## Wrong (fitdf is needed!)
MahdiMcLeod(res,lags,fitdf = 2) ## Correct
##
##
## Monthly log stock returns of Intel corporation data: Test for ARCH Effects
monthintel <- as.ts(monthintel)
MahdiMcLeod(monthintel) ## Usual test
MahdiMcLeod(monthintel,sqrd.res=TRUE) ## Test for ARCH effects
##
#### Write a function to fit a model: Apply portmanteau test on fitted obj with class "list"
## Example
FitModel <- function(data){
fit <- ar.ols(data, intercept = TRUE, order.max = 2)
fitdf <- 2
res <- res <- ts((fit$resid)[-(1:2), ])
list(res=res,fitdf=fitdf)
}
data(WestGerman)
DiffData <- matrix(numeric(3 * 91), ncol = 3)
for (i in 1:3)
DiffData[, i] <- diff(log(WestGerman[, i]), lag = 1)
Fit <- FitModel(DiffData)
MahdiMcLeod(Fit)
Run the code above in your browser using DataLab