test.ts.2nd.order.sig <- function(x, graph=F)
{
## Purpose: Test Time Series 2nd Order Significance.
## Arguments:
## x: a numeric vector (time series)
## Return: P-value for testing the 2nd order significance.
## Author: Feiming Chen, Date: 10 Apr 2017, 14:57
## ________________________________________________
d <- data.frame(Response = x, Time = seq(x))
m1 <- lm(Response ~ Time, d)
m2 <- lm(Response ~ Time + I(Time^2), d)
a <- anova(m1, m2)
model.compare.P.value <- a[["Pr(>F)"]][2]
if (graph) {
plot(d$Time, d$Response, xlab="Time", ylab="Response", type="b")
lines(d$Time, fitted(m1), col="red")
lines(d$Time, fitted(m2), col="blue")
title(main=paste("Model Comparison P-value =", round(model.compare.P.value, 3)))
}
model.compare.P.value
}
if (F) { # Unit Test
x <- rnorm(100)
test.ts.2nd.order.sig(x, graph = T)
r <- replicate(10000, test.ts.2nd.order.sig(rnorm(20))) # should follow a uniform distribution.
## summary(r)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00011 0.25800 0.50600 0.50500 0.75500 1.00000
}
No comments:
Post a Comment