Now that the model is build, we can make prediction of the following states when we are confronted with new patterns. Note that the First-Order Markov Chain (mc)1 expects you to give 1 pattern sequence and mc2 expects 2 pattern sequences.

pattern1 <- new("Pattern", sequence = c("P8"))
resultPattern1 <- predict(mc1, startPattern = pattern1, dist = 1)
resultPattern1
Sequence: P5
Probability: 0.5
Absorbing Probabilities: 
  None
1  NaN

pattern2 <- new("Pattern", sequence = c("P9", "P2")) 
resultPattern2 <- predict(mc2, startPattern = pattern2, dist = 1)
resultPattern2
Sequence: P1
Probability: 0.6666667
Absorbing Probabilities: 
  None
1  NaN

These results can also be calculated manually. Let’s calculate the latter step by step.

STEP 1: Transition probabilities per lag

These probabilities are exactly the same probabilities that were shown in the previous exercise.

Q2 <- mc2@transitions[[2]]
Q1 <- mc2@transitions[[1]]

STEP 2: Weights per lag (lambda)

We extract the lambda for each lag and multiply it with the transition probabilities per lag.

L2 <- mc2@lambda[[2]]
L1 <- mc2@lambda[[1]]

lag1 <- L1 * Q1 
lag2 <- L2 * Q2

STEP 3: Calculating the next value

Finally, we will try to calculate the next value for the sequence P9 and P2. To do so, add transition probability of P2 in lag1 and the transition probability of P9 in lag2 and select the state with the highest expected prob.

X <-  lag1[,'P2'] + lag2[,'P9']
names(X) = names(mc2@transitions[[1]])    #Add names of our states to this object
X
      Buy     Defer        P1       P10       P11       P12       P13        P2        P3        P4        P5        P6        P7 
0.0000000 0.0000000 0.6666667 0.0000000 0.3333333 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 
       P8        P9 
0.0000000 0.0000000 

max(X)                                    #The max probability is the next state
[1] 0.6666667

From this results we learn that the next state will be web page 1.

Exercise

Calculate the next value for the sequence P5 and P11 by using the model and store it as next_state. Manually calculate the probability that this state will be the next state and store it as prob_next_state.