Could you explain how the answer came about?

P=matrix(c(0.6,0.2,0.2,0.4,0.3,0.3,.2,.2,.6),nrow=3,byrow=T)
n=100000
x=numeric(n)
pi0=c(0.5,0.2,0.3)
state=sample(1:3,1,prob=pi0)
x[1]=state
x[1]
for (i in 2:n) {
state=sample(1:3,1,prob=P[state,])
x[i]=state
}
table(x)

Can you explain how to find table(x)??

I am not sure what the question is. The code runs as it should:

P=matrix(c(0.6,0.2,0.2,0.4,0.3,0.3,.2,.2,.6),nrow=3,byrow=T)
n=100000
x=numeric(n)
pi0=c(0.5,0.2,0.3)
state=sample(1:3,1,prob=pi0)
x[1]=state
x[1]
#> [1] 3
for (i in 2:n) {
  state=sample(1:3,1,prob=P[state,])
  x[i]=state
}
table(x)
#> x
#>     1     2     3 
#> 40486 22262 37252

Created on 2020-03-17 by the reprex package (v0.2.1)

Is this a home work assignment?

Your first stop should be your professor or fellow students. Make sure you read the home work policy:

As far as I can tell, you cannot know exactly what table(x) will give you since the sample command is random. If you want the same numbers each time you need to use set.seed. The best way to work through the code is to use the help function for each command that you do not know (or Google it, most help files are online). If you need access to R and cannot install it on your own computer, you can use RStudio Cloud.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Yeah it 's a practice problem my prof gave me. Could you explain how to interpret this code? If we don't have R to actually run it, how will we determine the table x. The explanation I have is
100000*c(11/27,2/9,10/27)
40740.74, 22222.22, 37037.04
Thank.