Skip to content

verifyEmpiricalToTheoretical Errors #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
explore-starling opened this issue Apr 11, 2025 · 1 comment
Open

verifyEmpiricalToTheoretical Errors #225

explore-starling opened this issue Apr 11, 2025 · 1 comment

Comments

@explore-starling
Copy link

I've been running into issues using the verifyEmpiricalToTheoretical() function.

library(markovchain)

states <- c("LRP", "LF", "LFP", "Bi", "RFP", "RF", "RRP", "BS", "ES") # list of possible states
transitions <- matrix(c(0.1639, 0.0459, 0.0852, 0.0569, 0.0852, 0.0459, 0.1639, 0.1399, 0.2132,
              0.1639, 0.0459, 0.0852, 0.0569, 0.0852, 0.0459, 0.1639, 0.1399, 0.2132,
              0.1639, 0.0459, 0.0852, 0.0569, 0.0852, 0.0459, 0.1639, 0.1399, 0.2132,
              0.1639, 0.0459, 0.0852, 0.0569, 0.0852, 0.0459, 0.1639, 0.1399, 0.2132,
              0.1639, 0.0459, 0.0852, 0.0569, 0.0852, 0.0459, 0.1639, 0.1399, 0.2132,
              0.1639, 0.0459, 0.0852, 0.0569, 0.0852, 0.0459, 0.1639, 0.1399, 0.2132,
              0.1639, 0.0459, 0.0852, 0.0569, 0.0852, 0.0459, 0.1639, 0.1399, 0.2132,
              0.1639, 0.0459, 0.0852, 0.0569, 0.0852, 0.0459, 0.1639, 0.1399, 0.2132,
              0.2083, 0.0583, 0.1084, 0.0722, 0.1084, 0.0583, 0.2083, 0.1778, 0), byrow=TRUE,
            nrow = 9, ncol=9, dimnames = list(c("LRP", "LF", "LFP", "Bi", "RFP", "RF", "RRP", "BS", "ES"),c("LRP", "LF", "LFP", "Bi", "RFP", "RF", "RRP", "BS", "ES")))
# creates theoretical transition matrix


chainE <- new("markovchain", states = states, transitionMatrix = transitions, 
              name = "ChainE") # creates markov chain object for theoretical transitions

# to create sample empirical sequence
generate_sequence <- function(states, n) {
  result <- character(n)
  prev <- ""

   for (i in 1:n) {
   # If previous state was "ES", exclude it from sampling
   options <- if (prev == "ES") states[states != "ES"] else states

   result[i] <- sample(options, 1)
   prev <- result[i]
 }
 return(result)
}

seq <- generate_sequence(states, 46000) # creates a sample like my dataset

verifyEmpiricalToTheoretical(data = seq, object = chainE, verbose = TRUE) # should compare the sequence and the theoretical markov chain?

I got some help over on StackOverflow (https://stackoverflow.com/questions/79560561/verifyempiricaltotheoretical-returns-error-even-when-using-character-sequence) to fix the first issue, which is that the function was not working despite my seq vector being characters.

verifyEmpiricalToTheoretical2 <- verifyEmpiricalToTheoretical
body(verifyEmpiricalToTheoretical2)[[4]][[2]] <- quote(!(is.numeric(data) || is.character(data) || is.matrix(data)))
environment(verifyEmpiricalToTheoretical2) <- environment(verifyEmpiricalToTheoretical)
verifyEmpiricalToTheoretical2(data = seq, object = chainE, verbose = TRUE)

With that code, I'm able to get an output, but it seems to be incorrect.

"At least one transition is >0 in the data that is 0 in the object. Therefore the null hypothesis is rejected."

However, the only transition in my sequence matrix that is 0 is also 0 in the transition matrix (this is because by definition, ES cannot follow ES in my data).

@spedygiorgio
Copy link
Owner

dear @explore-starling thanks for the point raisen. I know the issue but unfortunately I lack resources to fix it. Should you perform a pull request to fix it, I would be happy to overview it and eventually merge in the main branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants