[英]How to calculate the transition probability matrix of a second order Markov Chain
我有這樣的數據
經紀人職位
IP BP SP IP IP ..
我想以這種形式計算二階躍遷矩陣
BP IP SP
BPBP
SPSP
IPIP
BPSP
血壓
國際植保計划
SPIP
BPIP
IPBP
您可以使用embed
生成成對的連續過渡,使用table
進行計數, apply
以計算總數並將計數轉換為概率,進行dcast
和melt
將數組轉換為data.frame。
# Sample data
states <- sample(LETTERS[1:3], 1e5, replace=TRUE)
# Pairs of transitions
d <- embed( states, 3 )
colnames(d) <- c("today", "yesterday", "day before yesterday")
head(d)
# Count the transitions
counts <- table( as.data.frame( d ) )
# Divide by the total number of transitions, to have probabilities
probabilities <- counts
probabilities[] <- as.vector(counts) / rep( as.vector(apply( counts, 2:3, sum )), each=dim(counts)[1] )
# Check that the probabilities sum up to 1
apply( probabilities, 2:3, sum )
# Convert the 3-dimensional array to a data.frame
library(reshape2)
dcast( melt( probabilities ), yesterday + `day before yesterday` ~ today )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.