过渡矩阵 [英] Transition matrix

查看:288
本文介绍了过渡矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下数据框:

 df = data.frame(cusip = paste("A", 1:10, sep = ""), xt = c(1,2,3,2,3,5,2,4,5,1), xt1 = c(1,4,2,1,1,4,2,2,2,5))

数据分为五个状态,实际上是分位数: 1,2,3,4,5 . 数据框的第一列表示时间 t 的状态,第二列表示时间 t + 1 的状态.

The data is divided in five states, which are quantiles in reality: 1,2,3,4,5. The first column of the dataframe represents the state at time t, and the second column is the state at time t+1.

我想为这五个状态计算一种转换矩阵.矩阵的含义如下:

I would like to compute a sort of a transition matrix for the five states. The meaning of the matrix would be as follows:

  • (Row,Col)=(1,1):在时间 t 处在分位数1中的cusips的百分比, 并在时间 t + 1
  • 停留在1
  • (Row,Col)=(1,2):在 t 处处于分位数1的的百分比 在 t + 1
  • 成为第2分位数
  • 等...
  • (Row, Col) = (1,1) : % of cusips that were in quantile 1 at time t, and stayed at 1 in time t+1
  • (Row, Col) = (1,2) : % of cusips that were in quantile 1 at t, and became quantile 2 at t+1
  • etc...

我真的不确定如何有效地做到这一点.我觉得答案很琐碎,但我始终无法解决.

I am really not sure how to do this in an efficient way. I have the feeling the answer is trivial, but I just can't get my head around it.

有人可以帮忙吗?

推荐答案

res <- with(df, table(xt, xt1)) ## table() to form transition matrix
res/rowSums(res)                ## /rowSums() to normalize by row
#    xt1
# xt          1         2         4         5
#   1 0.5000000 0.0000000 0.0000000 0.5000000
#   2 0.3333333 0.3333333 0.3333333 0.0000000
#   3 0.5000000 0.5000000 0.0000000 0.0000000
#   4 0.0000000 1.0000000 0.0000000 0.0000000
#   5 0.0000000 0.5000000 0.5000000 0.0000000

## As an alternative to  2nd line above, use sweep(), which won't rely on 
## implicit recycling of vector returned by rowSums(res)
sweep(res, MARGIN = 1, STATS = rowSums(res), FUN = `/`)

这篇关于过渡矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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