For anyone that can help me this would be greatly appreciated.
I have to determine the probability of a Markov chain hittng state 1 before hitting state 5, with z varying from 0 to 1 and produce a plot.
The code I have so far is:
trials = 1000;
hitone=0;
for (k in 1:20){
z= 0 + k/20
for(j in 1:trials){
X=3 #initial state of Markov chain
P= matrix(c(1, 0, 0, 0, 0,
0, 0.4, 0.5, 0.1, 0,
0.1, 0.6, 0.2, 0.1, 0,
(0.1*z), (0.6*z), (0.3*z), (0*z), (1-z),
0, 0, 0, 0, 1),nrow=5, ncol=5, byrow=TRUE)#Matrix
i=1 #Number of steps
}
while(X>1 && X<5){
Y<-runif(1) # uniform sample
p<-P[X,] # Calculate the p values
p<-cumsum(p)# update the chain
if(Y<=p[1]){
X[i+1]= 1
}else if(Y<=p[2]){
X[i+1]=2
}else if (Y<=p[3]){
X[i+1] = 3
}else if (Y<=p[4]){
X[i+1] = 4
}else if (Y<=p[5]){
X[i+1] =5
}
i<-i+1
}
if(X==1){
hitone<-hitone+1
}else{
hitone<- hitone+0
}
}
probest[z]<-hitone/trials
probest[z]
plot(z, probest[z], type='line')
it keeps telling me that the plot type line will be truncated to the first character and then produces a graph with nothing on it.
Could somebody please explain what it means by this and if its not too much to ask any pointers on how to fix it,
I am a relative novice when it comes to using R so please dont spare any details in an explanation,
Thank you very much
I have to determine the probability of a Markov chain hittng state 1 before hitting state 5, with z varying from 0 to 1 and produce a plot.
The code I have so far is:
trials = 1000;
hitone=0;
for (k in 1:20){
z= 0 + k/20
for(j in 1:trials){
X=3 #initial state of Markov chain
P= matrix(c(1, 0, 0, 0, 0,
0, 0.4, 0.5, 0.1, 0,
0.1, 0.6, 0.2, 0.1, 0,
(0.1*z), (0.6*z), (0.3*z), (0*z), (1-z),
0, 0, 0, 0, 1),nrow=5, ncol=5, byrow=TRUE)#Matrix
i=1 #Number of steps
}
while(X>1 && X<5){
Y<-runif(1) # uniform sample
p<-P[X,] # Calculate the p values
p<-cumsum(p)# update the chain
if(Y<=p[1]){
X[i+1]= 1
}else if(Y<=p[2]){
X[i+1]=2
}else if (Y<=p[3]){
X[i+1] = 3
}else if (Y<=p[4]){
X[i+1] = 4
}else if (Y<=p[5]){
X[i+1] =5
}
i<-i+1
}
if(X==1){
hitone<-hitone+1
}else{
hitone<- hitone+0
}
}
probest[z]<-hitone/trials
probest[z]
plot(z, probest[z], type='line')
it keeps telling me that the plot type line will be truncated to the first character and then produces a graph with nothing on it.
Could somebody please explain what it means by this and if its not too much to ask any pointers on how to fix it,
I am a relative novice when it comes to using R so please dont spare any details in an explanation,
Thank you very much
Last edited: