XtP(t)ttXtXt−7Xt−1
XtXt−2Xt−1nXtXt−2{Xt−1=xj}nxjXt−ℓℓ>1Xt−2
apply
sweep
p(Xt|Xt−1=xj,Xt−2=xi)ij als spaltenindex im spalier soll unter MP zu ähnlichen verteilungen innerhalb einer spalte führen.
Der Junge. 5 des Buches Die statistische Analyse stochastischer Prozesse in der Zeit von JK Lindsey enthält andere Ideen zur Überprüfung von Annahmen.
[## simulates a MC with transition matrix in 'trans', starting from 'ini'
simMC <- function(trans, ini = 1, N) {
X <- rep(NA, N)
Pcum <- t(apply(trans, 1, cumsum))
X[1] <- ini
for (t in 2:N) {
U <- runif(1)
X[t] <- findInterval(U, Pcum[X[t-1], ]) + 1
}
X
}
set.seed(1234)
## transition matrix
P <- matrix(c(0.1, 0.1, 0.1, 0.7,
0.1, 0.1, 0.6, 0.2,
0.1, 0.3, 0.2, 0.4,
0.2, 0.2, 0.3, 0.3),
nrow = 4, ncol = 4, byrow = TRUE)
N <- 2000
X <- simMC(trans = P, ini = 1, N = N)
## it is better to work with factors
X <- as.factor(X)
levels(X) <- LETTERS[1:4]
## table transitions and normalize each row
Phat <- table(X[1:(N-1)], X[2:N])
Phat <- sweep(x = Phat, MARGIN = 1, STATS = apply(Phat, 1, sum), FUN = "/")
## explicit dimnames
dimnames(Phat) <- lapply(list("X(t-1)=" ,"X(t)="),
paste, sep = "", levels(as.factor(X)))
## transition 3-fold contingency array
P3 <- table(X[1:(N-2)], X[2:(N-1)], X[3:N])
dimnames(P3) <- lapply(list("X(t-2)=", "X(t-1)=" ,"X(t)="),
paste, sep = "", levels(as.factor(X)))
## apply ONE indendence test
fisher.test(P3[ , 1, ], simulate.p.value = TRUE)
## plot conditional distr.
library(lattice)
X3 <- data.frame(X = X[3:N], lag1X = X[2:(N-1)], lag2X = X[1:(N-2)])
histogram( ~ X | lag1X + lag2X, data = X3, col = "SteelBlue3")
]