简体   繁体   English

马尔可夫链的R模拟

[英]R Simulation Of Markov Chain

data1=data.frame("group"=c(1,2,3,4,5),
"t11"=c(0.01,0.32,0.25,0.37,0.11),
"t12"=c(0.48,0.45,0.61,0.29,0.23),
    "t13"=c(0.51,0.23,0.14,0.3,0.67),
    "t22"=c(0.13,0.91,0.41,0.69,0.42),
    "t23"=c(0.87,0.09,0.59,0.31,0.58))
set.seed(1)
    data2=data.frame("student"=c(1:20),
    "group"=c(sample(1:5,rep=T,20)))

I seek to estimate how Students make transitions through grades.我试图估计学生如何通过成绩进行过渡。 This is a sample data where t11 = stay in grade, t12 = move forward a grade, and t13 equals to graduating.这是一个示例数据,其中 t11 = 保持成绩,t12 = 前进一个等级,而 t13 等于毕业。 And so on.等等。 This is data1.这是数据1。

I saw some very complex packages to simulate the outcomes for this type of probabilities matrix--I am wondering is there a simpler way to simulate this 10-time steps using data2 as the student body and transition them using data1?我看到了一些非常复杂的包来模拟这种类型的概率矩阵的结果——我想知道是否有一种更简单的方法来使用 data2 作为学生主体来模拟这 10 次步骤并使用 data1 转换它们?

Here is a base R solution.这是一个基本的 R 解决方案。

  • First, you can define your function markov for transition matrix, ie,首先,您可以为转换矩阵定义函数markov ,即,
markov <- function(x, n) {
  m <- matrix(0,nrow = 3,ncol = 3)
  m[lower.tri(m,diag = TRUE)] <- c(unlist(x),1)
  r<-(u<-Reduce(`%*%`,replicate(n,m,simplify = FALSE)))[lower.tri(u,diag = TRUE)][-5]
}
  • Then, you can append the resulting probabilities to data1 , yielding data1_10step , and merge data1_10step with data2然后,您可以将结果概率附加到data1 ,产生data1_10step ,并将data1_10stepdata2合并
data1_10step <- data1
data1_10step[-1]<-t(apply(data1[-1], 1, markov,10))
data2out <- merge(data2,data1_10step)

such that以至于

> data2out
   group student          t11          t12       t13          t22 t23
1      1       1 1.000000e-20 5.514340e-09 1.0000000 1.378585e-09   1
2      1      10 1.000000e-20 5.514340e-09 1.0000000 1.378585e-09   1
3      1       3 1.000000e-20 5.514340e-09 1.0000000 1.378585e-09   1
4      1      18 1.000000e-20 5.514340e-09 1.0000000 1.378585e-09   1
5      1      15 1.000000e-20 5.514340e-09 1.0000000 1.378585e-09   1
6      1      19 1.000000e-20 5.514340e-09 1.0000000 1.378585e-09   1
7      2       4 1.125900e-05 2.970037e-01 0.7029850 3.894161e-01   1
8      2      13 1.125900e-05 2.970037e-01 0.7029850 3.894161e-01   1
9      2      14 1.125900e-05 2.970037e-01 0.7029850 3.894161e-01   1
10     2       7 1.125900e-05 2.970037e-01 0.7029850 3.894161e-01   1
11     3       9 9.536743e-07 5.081030e-04 0.9994909 1.342266e-04   1
12     3       6 9.536743e-07 5.081030e-04 0.9994909 1.342266e-04   1
13     3       8 9.536743e-07 5.081030e-04 0.9994909 1.342266e-04   1
14     4       2 4.808584e-05 2.212506e-02 0.9778269 2.446194e-02   1
15     5      12 1.000000e-10 1.227639e-04 0.9998772 1.708020e-04   1
16     5       5 1.000000e-10 1.227639e-04 0.9998772 1.708020e-04   1
17     5      16 1.000000e-10 1.227639e-04 0.9998772 1.708020e-04   1
18     5      17 1.000000e-10 1.227639e-04 0.9998772 1.708020e-04   1
19     5      20 1.000000e-10 1.227639e-04 0.9998772 1.708020e-04   1
20     5      11 1.000000e-10 1.227639e-04 0.9998772 1.708020e-04   1

EDIT编辑

If you trace the process of markov , you can vectorize markov , ie,如果你追踪markov的过程,你可以向量化markov ,即,

markov <- Vectorize(function(x, n) {
  m <- matrix(0,nrow = 3,ncol = 3)
  m[lower.tri(m,diag = TRUE)] <- c(unlist(x),1)
  r<-(u<-Reduce(`%*%`,replicate(n,m,simplify = FALSE)))[lower.tri(u,diag = TRUE)][-5]
})

and then you are able to trace n from 1 to 10 by using然后您可以使用以下方法跟踪n110

markov(x,seq(10))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM