如何计算二阶马尔可夫链的转移概率矩阵

时间:2013-04-10 16:26:29

标签: r markov-chains markov markov-models

我有这样的数据

Broker.Position

IP BP SP IP IP ..

我想像这种形式计算二阶转移矩阵

             BP IP SP

BPBP

SPSP

IPIP

黑花生衣色素

SPBP

IPSP

SPIP

BPIP

IPBP

1 个答案:

答案 0 :(得分:2)

您可以使用embed生成连续转换对, table来统计他们, apply计算总数并将计数转换为概率, dcastmelt将数组转换为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 )