我有这样的数据
Broker.Position
IP BP SP IP IP ..
我想以这种形式计算二阶转换矩阵
BP IP SP
血压
SPSP
IPIP
BPSP
SPBP
IPSP
SPIP
BPIP
IPBP
我有这样的数据
Broker.Position
IP BP SP IP IP ..
我想以这种形式计算二阶转换矩阵
BP IP SP
血压
SPSP
IPIP
BPSP
SPBP
IPSP
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 )