XYXYtXt−1,i→Xt,jXt−1tijX Y
Pr(Xt−1,i→Xt,j)=Pr(Xt,j|Xt−1,i,Yt−1,k)Pr(Yt−1,h→Yt,k)=Pr(Yt,h|Yt−1,k,Xt−1,i)
通过计算过渡历史并随后对概率进行归一化,可以轻松计算出过渡概率:
states <- c("absent", "present", "attack", "threat")
# data is stored in 3-dimensional array, initialized with
# a very small "default" non-zero count to avoid zeros.
female_counts <- male_counts <- array(1e-16, c(4,4,4), list(states, states, states))
n <- length(male_seq)
for (i in 1:n) {
male_counts[female_seq[i-1], male_seq[i-1], male_seq[i]] <- male_counts[female_seq[i-1], male_seq[i-1], male_seq[i]] + 1
female_counts[male_seq[i-1], female_seq[i-1], female_seq[i]] <- female_counts[male_seq[i-1], female_seq[i-1], female_seq[i]] + 1
}
male_counts/sum(male_counts)
female_counts/sum(female_counts)
也可以使用边际概率轻松地模拟它:
male_sim <- female_sim <- "absent"
for (i in 2:nsim) {
male_sim[i] <- sample(states, 1, prob = male_counts[female_sim[i-1], male_sim[i-1], ])
female_sim[i] <- sample(states, 1, prob = female_counts[male_sim[i-1], female_sim[i-1], ])
}
这种模拟的结果绘制在下面。
而且,它可以用于进行一步一步的预测:
male_pred <- female_pred <- NULL
for (i in 2:n) {
curr_m <- male_counts[female_seq[i-1], male_seq[i-1], ]
curr_f <- female_counts[male_seq[i-1], female_seq[i-1], ]
male_pred[i] <- sample(names(curr_m)[curr_m == max(curr_m)], 1)
female_pred[i] <- sample(names(curr_f)[curr_f == max(curr_f)], 1)
}
您提供的数据具有69-86%的准确性:
> mean(male_seq == male_pred, na.rm = TRUE)
[1] 0.8611111
> mean(female_seq == female_pred, na.rm = TRUE)
[1] 0.6944444
如果过渡随机发生,则过渡概率将遵循离散的均匀分布。这不是证明,而是可以用作使用简单模型思考数据的一种方式。