简体   繁体   English

R:使用过渡矩阵进行蒙特卡洛模拟

[英]R:Montecarlo simulation using transition matrix

I have a transition matrix : 我有一个转换矩阵:

a
           A          B         C     D         E          F          G          H         I
A 0.00000000 0.66666667 0.0000000 0.000 0.0000000 0.00000000 0.00000000 0.33333333 0.0000000
B 0.08823529 0.02941176 0.2941176 0.000 0.2352941 0.05882353 0.02941176 0.11764706 0.1470588
C 0.00000000 0.37500000 0.0000000 0.000 0.4166667 0.00000000 0.00000000 0.08333333 0.1250000
D 0.00000000 0.00000000 0.3333333 0.000 0.0000000 0.00000000 0.33333333 0.33333333 0.0000000
E 0.00000000 0.50000000 0.2307692 0.000 0.0000000 0.00000000 0.00000000 0.07692308 0.1923077
F 0.00000000 0.00000000 0.0000000 0.000 0.5000000 0.00000000 0.00000000 0.00000000 0.5000000
G 0.00000000 0.50000000 0.0000000 0.500 0.0000000 0.00000000 0.00000000 0.00000000 0.0000000
H 0.00000000 0.27272727 0.3636364 0.000 0.1818182 0.00000000 0.00000000 0.00000000 0.1818182
I 0.00000000 0.31250000 0.1875000 0.125 0.3125000 0.00000000 0.00000000 0.06250000 0.0000000

and i have in my dataset a categorical variable : 我的数据集中有一个分类变量:

state=c("G" ,"I" ,"G", "C", "D", "I","A" ,"G", "G" ,"H", "C", "D" ,"C", "H" "F", "B", "F" ,"G" ,"D", "E" ,"B" ,"H" ,"E" ,"C" ,"F" ,"H", "C", "H" ,"F" ,"H") 

and now i want to use my transition matrix to simulate a variable just like my variable state using the Monte carlo approach. 现在我想使用转移矩阵来模拟变量,就像使用蒙特卡洛方法模拟变量state一样。 Can you advise me which R package or function can help me to do my simulation please ! 您能告诉我哪个R软件包或函数可以帮助我进行仿真!

You could use markovchainSequence from package markovchain . 您可以使用markovchainSequence软件包中的markovchain Example: 例:

mat <- as.matrix(read.table(text="
0.00000000 0.66666667 0.0000000 0.000 0.0000000 0.00000000 0.00000000 0.33333333 0.0000000
0.08823529 0.02941176 0.2941176 0.000 0.2352941 0.05882353 0.02941176 0.11764706 0.1470588
0.00000000 0.37500000 0.0000000 0.000 0.4166667 0.00000000 0.00000000 0.08333333 0.1250000
0.00000000 0.00000000 0.3333333 0.000 0.0000000 0.00000000 0.33333333 0.33333333 0.0000000
0.00000000 0.50000000 0.2307692 0.000 0.0000000 0.00000000 0.00000000 0.07692308 0.1923077
0.00000000 0.00000000 0.0000000 0.000 0.5000000 0.00000000 0.00000000 0.00000000 0.5000000
0.00000000 0.50000000 0.0000000 0.500 0.0000000 0.00000000 0.00000000 0.00000000 0.0000000
0.00000000 0.27272727 0.3636364 0.000 0.1818182 0.00000000 0.00000000 0.00000000 0.1818182
0.00000000 0.31250000 0.1875000 0.125 0.3125000 0.00000000 0.00000000 0.06250000 0.000000",
           header=FALSE,stringsAsFactors = FALSE))
rownames(mat) <- colnames(mat) <- LETTERS[1:9]
mat[,9] <- 1-rowSums(mat[,1:8]) #To make sure your rows sum to 1

statesNames <- LETTERS[1:9]
markovchain_object <- new("markovchain", states = statesNames, transitionMatrix = mat)

markovchainSequence(n=10, markovchain = markovchain_object)

 [1] "C" "H" "C" "E" "C" "B" "C" "E" "B" "G"

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

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