问题描述
我有一个由 8 亿条记录聚合而成的频率表,我想知道是否可以使用包从频率表中计算一阶转移矩阵,这是不对称的,因为某些状态再也没有发生过.频率表的一个样本是:
I have a frequency table aggregated from 800 millions of records and am wondering if I can use a package to calculate 1st order transition matrix from the frequency table, which is not symmetric because some state just never happened again. A sample of the frequency table is:
library(data.table)
model.data <- data.table(state1 = c(3, 1, 2, 3), state2 = c(1, 2, 1, 2), Freq = c(1,2,3,4))
model.data 看起来像这样:
model.data looks like this:
state1 | state2 | n |
---|---|---|
3 | 1 | 1 |
1 | 2 | 2 |
2 | 1 | 3 |
3 | 2 | 4 |
使用包 pollster,我可以计算出比例表:
Using the package pollster, I can compute the proportion table:
library(pollster)
crosstab(model.data, state1, state2, Freq)
state1 | 1 | 2 | n |
---|---|---|---|
1 | 0 | 100 | 2 |
2 | 100 | 0 | 3 |
3 | 20 | 80 | 5 |
然而,我正在寻找的对称转移矩阵是:
However, the symmetric transition matrix I am looking for is:
state1 | 1 | 2 | 3 | n |
---|---|---|---|---|
1 | 0 | 100 | 0 | 2 |
2 | 100 | 0 | 0 | 3 |
3 | 20 | 80 | 0 | 5 |
也就是说,即使没有人转换到状态3,我仍然想包含状态3,并且代码应该能够自动找出3需要附加一列0.
That is, I still want to include the state 3 even though no one transitioned to it, and the code should be able to automatically find out 3 needs to be appended with a column of 0s.
由于内存限制和缓慢的计算速度,我不确定带有 markovchainFit 函数的 markovchain 包是否能够处理我需要转换为数百万序列列表的 8 亿行数据.
I am not sure if the markovchain package with the markovchainFit function is going to handle my 800 million rows of data that I need to transform into a list of millions of sequences, due to memory constraints and slow computing speed.
有人知道吗?
推荐答案
An option with igraph
An option with igraph
model.data %>%
setorder(state1) %>%
graph_from_data_frame() %>%
as_adjacency_matrix(attr = "Freq", sparse = FALSE) %>%
proportions(1) # 1 sets rows as the margin, similar to `prop.table`
给予
1 2 3
1 0.0 1.0 0
2 1.0 0.0 0
3 0.2 0.8 0
或使用基数 R
Or with base R
> proportions(xtabs(Freq ~ ., model.data), 1)
state2
state1 1 2
1 0.0 1.0
2 1.0 0.0
3 0.2 0.8
这篇关于是否有用于从频率表计算一阶转换矩阵的 R 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!