简体   繁体   English

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

[英]How to calculate the transition probability matrix of a second order Markov Chain

I have data like in form of this 我有这样的数据

Broker.Position 经纪人职位

IP BP SP IP IP .. IP BP SP IP IP ..

I would like to calculate the second order transition matrix like in this form 我想以这种形式计算二阶跃迁矩阵

             BP IP SP

BPBP BPBP

SPSP SPSP

IPIP IPIP

BPSP BPSP

SPBP 血压

IPSP 国际植保计划

SPIP SPIP

BPIP BPIP

IPBP IPBP

You can use embed to generate the pairs of consecutive transitions, table to count them, apply to compute the totals and convert the counts to probabilities, dcast and melt to convert the array to a data.frame. 您可以使用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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM