我有这样的数据
Broker.Position
IP BP SP IP IP ..
我想像这种形式计算二阶转移矩阵
BP IP SP
BPBP
SPSP
IPIP
黑花生衣色素
SPBP
IPSP
SPIP
BPIP
IPBP
答案 0 :(得分:2)
您可以使用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 )