Skip to content

verifyEmpiricalToTheoretical Errors #225

@explore-starling

Description

@explore-starling

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions