spedygiorgio / markovchain

Easy Handling Discrete Time Markov Chains
https://spedygiorgio.github.io/markovchain/
Other
105 stars 40 forks source link

verifyEmpiricalToTheoretical fails if the input sequence does not contain all possible states from the markovchain object #191

Closed ebbertd closed 4 years ago

ebbertd commented 4 years ago

Given the following example:

library(markovchain)
mc <- matrix(c(
  1 / 10, 7 / 10, 1 / 10, 1 / 10,
  1 / 10, 1 / 10, 4 / 10, 4 / 10,
  1 / 10, 5 / 10, 1 / 10, 3 / 10,
  1 / 10, 5 / 10, 3 / 10, 1 / 10
),
byrow = TRUE,
nrow = 4
)
rownames(mc) <- c(1:4)
colnames(mc) <- c(1:4)
theoreticalMc <- as(mc, "markovchain")
sequence <- c(1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1)
verifyEmpiricalToTheoretical(data = sequence, object = theoreticalMc)

The following error is produced:

Fehler in data[match(rownames(data), names(object)), ] : 
  Indizierung außerhalb der Grenzen

This is due to the missing state in the sequence that is present in the markovchain object. The created matrix for the input should thus be adjusted to include all possible states from the markovchain object.

spedygiorgio commented 4 years ago

thanks for fixing it @ebbertd , may you also write some unit tests for it?

ebbertd commented 4 years ago

See pull request #198