diagnostics: Diagnostics tests

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/diagnostics.R

Description

Auxiliary function (i.e. not intended for the average user) called by the arx, getsm, getsv and isat functions. The diagnostics function undertakes tests for autocorrelation, ARCH and non-normality in a residual series x, and - optionally - perform additional user-defined diagnostics provided via the user.fun argument (see details). The autocorrelation and ARCH tests are conducted as Ljung and Box (1979) tests of autocorrelation in the residuals and squared residuals, respectively, whereas the test for non-normality is that of Jarque and Bera (1980).

Usage

1
2
3
diagnostics(x, s2=1, y=NULL, xreg=NULL, ar.LjungB=c(1,0.025),
  arch.LjungB=c(1,0.025), normality.JarqueB=NULL, verbose=TRUE,
  user.fun=NULL)

Arguments

x

numeric vector, typically the residuals from a regression

s2

the standard deviation of x

y

NULL or the regressand of the model to be diagnosticised. In the latter case, the vector is passed on to the user-defined diagnostics function provided via user.fun, see details

xreg

NULL or the matrix of regressors of the model to be diagnosticised. In the latter case, the matrix is passed on to the user-defined diagnostics function provided via user.fun, see details

ar.LjungB

a two element vector or NULL. In the former case, the first element contains the AR-order, the second element the significance level. If NULL, then a test for autocorrelation is not conducted

arch.LjungB

a two element vector or NULL. In the former case, the first element contains the ARCH-order, the second element the significance level. If NULL, then a test for ARCH is not conducted

normality.JarqueB

NULL or a value between 0 and 1. In the latter case, a test for non-normality is conducted using a significance level equal to normality.JarqueB. If NULL, then no test for non-normality is conducted

verbose

logical. If TRUE, then a data.frame with the results of the diagnostics is returned. If FALSE, then the return-value is a logical that indicates whether the model passes the diagnostics (TRUE if it does, otherwise FALSE)

user.fun

NULL or a list with two entries, name and pval. The first item (name) should contain the name of the user-defined function, and must be of class character. The the second item should contain the chosen significance level or levels, i.e. either a scalar or a vector of length equal to the number of p-values returned by the user-defined diagnostics function, see details

Details

The argument user.fun enables the user to specify additional diagnostics. The function must be defined in the global environment (i.e. .GlobalEnv), and the user-defined function should accept at least the first of the following four arguments: the regressand, the matrix of mean-regressors, the vector of residuals, the vector of standardised residuals. Also, the arguments must enter in the order indicated (see example below). However, the user-defined function need of course not use all of these arguments within itself when doing the diagnostics. The value returned by the user-defined function should be a matrix of dimension m x 3, where m is the number of diagnostic tests performed inside the user-defined function. (If only a single test is performed, then the returned value can be a vector of length 3.) The three columns of the m x 3 matrix should contain, in the following order: 1) the value(s) of the test-statistic(s), 2) the degrees of freedom(s) (or NA if there are none) of the tests, and 3) the p-value(s) of the test(s). When checking whether the model passes the diagnostics or not, the p-value(s) is(are) checked against the value in the second entry of user.fun

Value

If verbose=TRUE:

a data.frame that contains the diagnostics results

If verbose=FALSE:

a logical indicating whether the residuals and/or model passes the diagnostics (TRUE if it does, FALSE otherwise)

Author(s)

Genaro Sucarrat, http://www.sucarrat.net/

References

C. Jarque and A. Bera (1980): 'Efficient Tests for Normality, Homoscedasticity and Serial Independence'. Economics Letters 6, pp. 255-259

G. Ljung and G. Box (1979): 'On a Measure of Lack of Fit in Time Series Models'. Biometrika 66, pp. 265-270

See Also

arx, getsm, getsv, isat

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
##check x for autocorrelation and ARCH, and return a
##data-frame with the results:
set.seed(123)
x <- rnorm(40)
diagnostics(x)

##add the Jarque-Bera normality test to the diagnostics:
diagnostics(x, normality.JarqueB=TRUE)

##check x for autocorrelation and ARCH, and indicate
##whether it passes the check:
diagnostics(x, verbose=FALSE)

##user-defined Shapiro-Wilks test for normality in the residuals:
SWtest <- function(y, xreg, x, zhat, ...){
  tmp <- shapiro.test(x) #do test on the residuals
  return( c(tmp$statistic, NA, tmp$p.value) )
}
diagnostics(x, user.fun=list(name="SWtest", pval=0.025))
diagnostics(x, user.fun=list(name="SWtest", pval=0.025), verbose=FALSE)

gets documentation built on May 30, 2017, 4:09 a.m.

Search within the gets package
Search all R packages, documentation and source code