if (FALSE) {
## data generating
set.seed(1977)
Q <- 3
true.beta1 <- c(1, 1, 1) ; true.beta2 <- c(1, -1, -1)
true.sigma2 <- c(1, 3); true.D1 <- diag(.5, Q); true.D2 <- diag(2.5, Q)
N=20; T=100;
NT <- N*T
x1 <- rnorm(NT)
x2 <- runif(NT, 5, 10)
X <- cbind(1, x1, x2); W <- X; y <- rep(NA, NT)
## true break numbers are one and at the center
break.point = rep(T/2, N); break.sigma=c(rep(1, N));
break.list <- rep(1, N)
id <- rep(1:N, each=NT/N)
K <- ncol(X);
ruler <- c(1:T)
## compute the weight for the break
W.mat <- matrix(NA, T, N)
for (i in 1:N){
W.mat[, i] <- pnorm((ruler-break.point[i])/break.sigma[i])
}
Weight <- as.vector(W.mat)
## data generating by weighting two means and variances
j = 1
for (i in 1:N){
Xi <- X[j:(j+T-1), ]
Wi <- W[j:(j+T-1), ]
true.V1 <- true.sigma2[1]*diag(T) + Wi%*%true.D1%*%t(Wi)
true.V2 <- true.sigma2[2]*diag(T) + Wi%*%true.D2%*%t(Wi)
true.mean1 <- Xi%*%true.beta1
true.mean2 <- Xi%*%true.beta2
weight <- Weight[j:(j+T-1)]
y[j:(j+T-1)] <- (1-weight)*true.mean1 + (1-weight)*chol(true.V1)%*%rnorm(T) +
weight*true.mean2 + weight*chol(true.V2)%*%rnorm(T)
j <- j + T
}
## model fitting
subject.id <- c(rep(1:N, each=T))
time.id <- c(rep(1:T, N))
resid <- rstandard(lm(y ~X-1 + as.factor(subject.id)))
G <- 100
out0 <- testpanelGroupBreak(subject.id, time.id, resid, m=0,
mcmc=G, burnin=G, thin=1, verbose=G,
b0=0, B0=1/100, c0=2, d0=2, marginal.likelihood = "Chib95")
out1 <- testpanelGroupBreak(subject.id, time.id, resid, m=1,
mcmc=G, burnin=G, thin=1, verbose=G,
b0=0, B0=1/100, c0=2, d0=2, marginal.likelihood = "Chib95")
out2 <- testpanelGroupBreak(subject.id, time.id, resid, m=2,
mcmc=G, burnin=G, thin=1, verbose=G,
b0=0, B0=1/100, c0=2, d0=2, marginal.likelihood = "Chib95")
out3 <- testpanelGroupBreak(subject.id, time.id, resid, m=3,
mcmc=G, burnin=G, thin=1, verbose=G,
b0=0, B0=1/100, c0=2, d0=2, marginal.likelihood = "Chib95")
## Note that the code is for a hypothesis test of no break in panel residuals.
## When breaks exist, the estimated number of break in the mean and variance of panel residuals
## tends to be larger than the number of break in the data generating process.
## This is due to the difference in parameter space, not an error of the code.
BayesFactor(out0, out1, out2, out3)
## In order to identify the number of breaks in panel parameters,
## use HMMpanelRE() instead.
}
Run the code above in your browser using DataLab