Understanding Markov Chain source code in R

前端 未结 1 897
广开言路
广开言路 2021-01-28 01:32

The following source code is from a book. Comments are written by me to understand the code better.

#============================================================         


        
相关标签:
1条回答
  • 2021-01-28 02:13

    Names of the states don't really need to have any statistical meaning as long as they are different. So, while simulating transitions between states, it's perfectly fine to choose states <- 1:length(init) or any other names for them. Ultimately, though, for practical purposes we often have in mind some labels in mind, such as -1, 0, ..., n, as in your example. You can provide those names as the labels parameter and then labels[simlist] will rename 1:length(init) to labels, element by element. I.e., if initially we had c(1, 2, 3) and you provided labels as c(5, 10, 12), then the output will be in terms of the latter vector. For instance,

    (states <- sample(1:3, 10, replace = TRUE))
    # [1] 1 3 3 2 2 1 2 1 3 3
    labels <- c(5, 10, 12)
    labels[states]
    # [1]  5 12 12 10 10  5 10  5 12 12
    
    0 讨论(0)
提交回复
热议问题