[R] Probability(Markov chain transition matrix)

kjetil@acelerate.com kjetil at acelerate.com
Thu Apr 29 23:32:46 CEST 2004


On 29 Apr 2004 at 20:27, Maria Gu wrote:

> Hello, My name is Maria, MBA student in Sanfransisco, USA.
> In my credit scoring class, I have hard time making "transition
> matrix", which explains probability especially in relation to "Markov
> chain model". It is regarding people's monthly credit payment
> behavior. Does R have function to caculate it? I am actually a novice
> in using 'R'. Please help me!!!
> 
> Maria Gu
> 

Hola Maria, 

you need to be more specific in what you want. Do you want to 
estimate a markov transistion matrix from data? Do you want to 
simulate from a Markoc chain model? Whatever you want, it is 
likely possible in R, but we need more information.

If you want to simulate from a model, maybe something like:

> P <- matrix(c(0.1, 0.4, 0.5, 
+               0.9, 0.05, 0.05, 
+               0.3, 0.6, 0.1),3,3, byrow=TRUE)
> simMarkov <- function(P, init, n) {
+    p <- dim(P)[1]
+    chain <- numeric(length=n)
+    chain[1] <- init
+    for (i in 2:n) {
+      chain[i] <- sample(1:p, 1, prob=P[chain[i-1],])
+    }
+    return(chain)
+ }
> simMarkov(P,1,20)
 [1] 1 2 1 3 2 2 2 1 2 1 2 1 3 1 2 1 3 2 1 1
> 

Kjetil Halvorsen

> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list