是否有用于从频率表计算一阶转换矩阵的 R 包? [英] Is there an R package to calculate 1st order transition matrix from a frequency table?

查看:23
本文介绍了是否有用于从频率表计算一阶转换矩阵的 R 包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 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:

<头>
state1state2n
311
122
213
324

使用包 pollster,我可以计算出比例表:

Using the package pollster, I can compute the proportion table:

library(pollster)
crosstab(model.data, state1, state2, Freq)

<头>
state112n
101002
210003
320805

然而,我正在寻找的对称转移矩阵是:

However, the symmetric transition matrix I am looking for is:

<头>
state1123n
1010002
2100003
3208005

也就是说,即使没有人转换到状态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 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆