-1

我有这样的数据

Broker.Position

IP BP SP IP IP ..

我想以这种形式计算二阶转换矩阵

             BP IP SP

血压

SPSP

IPIP

BPSP

SPBP

IPSP

SPIP

BPIP

IPBP

4

1 回答 1

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 )
于 2013-04-10T17:22:37.050 回答