###############################################################
## Example 1
###############################################################
# Simple example using Monte-Carlotest in portes.
# 100 replications takes about 6 seconds on single cpu 2.67 GHz.
##
ans <- arima(Nile, order=c(1,0,1))
portest(ans, NREP=100)
##
###############################################################
## Example 2
###############################################################
# Simulate a bivariate VARIMA(1,c(0,0),1) process with length 300.
# Apply gvtest based on the two methods implemented in portes.
# 100 replications takes about 23.63 seconds on single cpu 2.67 GHz.
##about 6 seconds
k <- 2
n <- 300
Trunc.Series <- 50
phi <- array(c(0.5,0.4,0.1,0.5),dim=c(k,k,1))
theta <- array(c(0,0.25,0,0),dim=c(k,k,1))
d <- NA
sigma <- matrix(c(1,0.71,0.71,2),k,k)
z <- varima.sim(phi,theta,d,sigma,n)
ans <- ar(z)
portest(ans, MonteCarlo=FALSE) ## asymptotic distribution method
portest(ans, NREP=100) ## Monte-Carlo method
##
###############################################################
## Example 3
###############################################################
# Simulate a bivariate VARIMA(2,d,0) process with length 300.
## d is a vector (1,2)
## Fit VAR(2) using the function VAR in the package "vars"
# Apply gvtest based on the two methods implemented in portes.
# 100 replications takes about 30.17 seconds on single cpu 2.67 GHz.
##
library(vars)
k <- 2
n <- 300
Trunc.Series <- 50
phi <- array(c(0.5,0.4,0.1,0.5,0,0.25,0,0),dim=c(k,k,2))
theta <- NULL
d <- c(1,2)
sigma <- matrix(c(1,0.71,0.71,2),k,k)
z <- varima.sim(phi,theta,d,sigma,n)
ans <- VAR(z, p=2) ## inadequate fitted model
portest(ans, MonteCarlo=FALSE) ## asymptotic distribution method
portest(ans, NREP=100) ## Monte-Carlo method
##
###############################################################
## Example 4
###############################################################
## Checks the residuals for randomness using LjungBox test.
portest(rnorm(100),test="LjungBox")
##
###############################################################
## Example 5 - Using "snow" package
###############################################################
## Apply Hosking test on fitted VAR(2) model to WestGerman data.
##
library("snow")
data("WestGerman")
DiffData <- matrix(numeric(3 * 91), ncol = 3)
for (i in 1:3) DiffData[, i] <- diff(log(WestGerman[, i]), lag = 1)
Fit2 <- ar.ols(DiffData, aic=FALSE, order.max = 2, intercept = FALSE)
portest(Fit2,test="Hosking",nslaves=8) ## Monte-Carlo
portest(Fit2,test="Hosking",MonteCarlo=FALSE) ## asymptotic distribution
##
###############################################################
## Example 6 - Using "snow" package
###############################################################
## Test monthly log stock returns of Intel data for ARCH effects.
## gvtest statistic on PC with 8 CPU's using "snow".
## It takes 16.75 seconds based on the Monte-Carlo test.
##
data(monthintel)
returns <- as.ts(monthintel)
lags <- c(10, 20, 30, 40)
portest(returns,lags=lags,MonteCarlo=TRUE,nslaves=8,SquaredQ=TRUE)
##
###############################################################
## Example 7 - Using "snow" package
###############################################################
## Fit Fractional Gaussian Noise, FGN, to NileMin data in FGN package.
## Monte-Carlo of gvtest on 8 CPU's using "snow".
##
library(FGN)
data(NileMin)
NILE.FGN <- FitFGN(NileMin)
lags <- c(5, 10, 20)
##
## gvtest statistic on fitted model (55 seconds)
portest(NILE.FGN, lags=lags, nslaves=8)
##
## gvtest statistic on residuals (6 seconds)
res <- NILE.FGN$res
portest(res, lags=lags, nslaves=8)
##
Run the code above in your browser using DataLab