-
Notifications
You must be signed in to change notification settings - Fork 10
/
TestDurbinWatson.R
61 lines (48 loc) · 1.67 KB
/
TestDurbinWatson.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#' @title Durbin-Watson test for serial correlation.
#' @description Performs the Durbin-Watson test for serial correlation.
#'
#' @param rets numeric :: time series returns. May be a zoo or numeric vector.
#' @param a numeric :: alpha. This controls the significance level of the results.
#'
test_durbinwatson <- function(rets, a = 0.99, r = "lm", w = 126) {
# Check and convert the data.
.check_data(data = rets)
rets <- as.numeric(rets)
# Now construct the data frame.
k <- length(rets)
y.var <- tail(rets, k - 1)
x.var <- head(rets, k - 1)
data <- data.frame(y.var, x.var)
# Use lmtest to compute the p-values.
colnames(data) <- c("y", "x")
# Fit the linear model.
lmfit <- lm(formula = y ~ x,
data = data)
# Now compute the durbin-watson statistic.
dw <- lmtest::dwtest(formula = lmfit,
alternative = "two.sided")
# Get the test statistic (D) for the test.
stat <- dw$statistic
# Get the p-value for the D statistic.
p.value <- dw$p.value
# Determins the Z-score.
z.score <- qnorm(p.value)
# Compute the required threshold.
thresh <- abs(qnorm((1 - a) / 2))
# Return the results object.
return(RandomnessResult("Durbin Watson", "Serial Correlation",
stat, p.value, z.score, abs(z.score) > thresh))
}
.test_durbinwatson_nop <- function(residuals) {
if (is.zoo(residuals)) {
residuals <- as.numeric(residuals)
} else if (!is.numeric(residuals)) {
stop("test.durbinwatson only works with a vector or zoo")
}
k <- length(residuals)
sumr2 <- sum(residuals ^ 2)
diffs <- cAsDifferences(residuals, 1)
sumd2 <- sum(diffs ^ 2)
d <- sumd2 / sumr2
return(c(d, d < 1))
}