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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
Fstats <- function(formula, from = 0.15, to = NULL, data = list(), vcov. = NULL)
{
if(!inherits(formula, "formula")) {
X <- if(is.matrix(formula$x))
formula$x
else model.matrix(terms(formula), model.frame(formula))
y <- if(is.vector(formula$y))
formula$y
else model.response(model.frame(formula))
} else {
mf <- model.frame(formula, data = data)
y <- model.response(mf)
X <- model.matrix(formula, data = data)
}
k <- ncol(X)
n <- length(y)
e <- lm.fit(X,y)$residuals
## check if tsp are available and may be used
## (potentially not if NAs were removed)
ytsp <- NULL
orig.y <- NULL
tsp_ok <- FALSE
if(is.ts(data)){
if(NROW(data) == n) {
ytime <- time(data)
ytsp <- tsp(data)
tsp_ok <- TRUE
}
} else {
env <- environment(formula)
if(missing(data)) data <- env
orig.y <- eval(attr(terms(formula, data = data), "variables")[[2]], data, env)
if(is.ts(orig.y) & (NROW(orig.y) == n)){
ytime <- time(orig.y)
ytsp <- tsp(orig.y)
tsp_ok <- TRUE
}
}
ts.eps <- getOption("ts.eps")
if(length(from) > 1) {
if(!is.null(ytsp) && from[2] <= ytsp[3]) {
from <- which(abs(ytime-(from[1]+(from[2]-1)/ytsp[3])) < ts.eps)
if(!is.null(to)) to <- which(abs(ytime-(to[1]+(to[2]-1)/ytsp[3])) < ts.eps)
} else {
stop(paste(sQuote("from"), "does not specify a valid time point"))
}
}
else if(from < 1)
{
from <- floor(from*n)
if(!is.null(to)) to <- floor(to*n)
}
if(is.null(to)) to <- n - from
if(from < (k+1))
{
from <- k+1
warning("'from' changed (was too small)")
}
if(to > (n-k-1))
{
to <- n-k-1
warning("'to' changed (was too large)")
}
if(from <= to)
point <- (from:to)
else
stop("inadmissable change points: 'from' is larger than 'to'")
sume2 <- sum(e^2)
lambda <- ((n-from)*to)/(from*(n-to))
np <- length(point)
stats <- rep(0,np)
for(i in 1:np)
{
X1 <- as.matrix(X[(1:point[i]),])
X2 <- as.matrix(X[((point[i]+1):n),])
if(is.null(vcov.)) {
fm1 <- lm.fit(X1,y[1:point[i]])
fm2 <- lm.fit(X2,y[((point[i]+1):n)])
u <- c(fm1$residuals, fm2$residuals)
sigma2 <- (sum(u^2))/(n-2*k)
stats[i] <- (sume2-sum(u^2))/sigma2
}
else {
allX <- cbind(X1, matrix(rep(0, point[i]*k), ncol=k))
allX <- rbind(allX, cbind(X2, X2))
fm2 <- lm(y ~ 0 + allX)
beta2 <- coef(fm2)[-(1:k)]
V <- vcov.(fm2)
stats[i] <- as.vector(t(beta2) %*% chol2inv(chol(V[-(1:k),-(1:k)])) %*% beta2)
}
}
sup.point <- which.max(stats) + from - 1
if(is.null(vcov.))
min.RSS <- sume2/(1 + max(stats)/(n - 2*k))
else
min.RSS <- NA
if(is.ts(data) & tsp_ok){
stats <- ts(stats, start = time(data)[from], frequency = frequency(data))
datatsp <- tsp(data)
}
else if(!is.null(orig.y) & tsp_ok) {
stats <- ts(stats, start = time(orig.y)[from], frequency = frequency(orig.y))
datatsp <- tsp(orig.y)
}
else{
stats <- ts(stats, start = from/n, frequency = n)
datatsp <- c(0, 1, n)
}
retval <- list(Fstats = stats,
nreg = k,
nobs = n,
par = lambda,
call = match.call(),
formula = formula,
breakpoint = sup.point,
RSS = min.RSS,
datatsp = datatsp)
class(retval) <- "Fstats"
return(retval)
}
print.Fstats <- function(x, ...)
{
cat("\nF statistics \n\n")
cat("Call: ")
print(x$call)
cat("\n")
}
sctest.Fstats <- function(x, type = c("supF", "aveF", "expF"), asymptotic = FALSE, ...)
{
dname <- paste(deparse(substitute(x)))
type <- match.arg(type)
switch(type,
supF = {
STATISTIC <- max(x$Fstats)
names(STATISTIC) <- "sup.F"
METHOD <- "supF test"
},
aveF = {
STATISTIC <- mean(x$Fstats)
names(STATISTIC) <- "ave.F"
METHOD <- "aveF test"
},
expF = {
STATISTIC <- log(mean(exp(0.5*x$Fstats)))
names(STATISTIC) <- "exp.F"
METHOD <- "expF test"
})
if((x$par == 1) & !(type == "expF") & !asymptotic)
{
METHOD <- "Chow test"
PVAL <- 1 - pf(STATISTIC, x$nreg, (x$nobs-2*x$nreg))
}
else
PVAL <- pvalue.Fstats(STATISTIC, type = type,
k=x$nreg, lambda=x$par)
RVAL <- list(statistic = STATISTIC, p.value = PVAL,
method = METHOD, data.name = dname)
class(RVAL) <- "htest"
return(RVAL)
}
plot.Fstats <- function(x, pval = FALSE, asymptotic = FALSE,
alpha = 0.05, boundary = TRUE, aveF = FALSE,
xlab = "Time", ylab = NULL,
ylim = NULL, ...)
{
k <- x$nreg
n <- x$nobs
bound <- boundary(x, alpha = alpha, pval = pval, aveF = aveF, asymptotic =
asymptotic)
x <- x$Fstats
if(pval)
{
if(asymptotic)
x <- 1 - pchisq(x, k)
else
x <- 1 - pf(x, k, (n-2*k))
if(is.null(ylab)) ylab <- "p values"
}
else
if(is.null(ylab)) ylab <- "F statistics"
if(is.null(ylim)) ylim <- c(0, max(c(x,bound)))
plot(x, xlab = xlab, ylab = ylab, ylim = ylim, ...)
abline(0,0)
if(boundary)
{
lines(bound, col=2)
if(aveF) lines(ts(rep(mean(x),length(x)),start=start(x),
frequency = frequency(x)),lty=2)
}
}
boundary.Fstats <- function(x, alpha = 0.05, pval = FALSE, aveF =
FALSE, asymptotic = FALSE, ...)
{
if(aveF)
{
myfun <- function(y) {pvalue.Fstats(y, type="ave", x$nreg, x$par) - alpha}
upper <- 40
}
else
{
myfun <- function(y) {pvalue.Fstats(y, type="sup", x$nreg, x$par) - alpha}
upper <- 80
}
bound <- uniroot(myfun, c(0,upper))$root
if(pval)
{
if(asymptotic)
bound <- 1 - pchisq(bound, x$nreg)
else
bound <- 1 - pf(bound, x$nreg, (x$nobs-2*x$nreg))
}
bound <- ts(bound,
start = start(x$Fstats),
end = end(x$Fstats),
frequency = frequency(x$Fstats))
return(bound)
}
lines.Fstats <- function(x, ...)
{
lines(x$Fstats, ...)
}
|