I’m a math grad student who has just started learning R, and I could really use some help from any R-literate people!
I’m building a shiny app that models probabilistic evolutionary behavior (as a Markov process). It can bee seen (in showcase mode) at:
THE PROBLEM: The simulation portion of the app is throwing out 2 errors that I haven’t figured out. They are as follows:
When population size N is small (N<10) it often (but not always) throws
Error: in sample.int(length(x), size, replace, prob) :
incorrect number of probabilities
When population size N is large (N>100) it often (but not always) throws
Error: in sample.int(length(x), size, replace, prob) :
non-positive probability
You can replicate this error by setting the populations slider to max (200) or min (0), and clicking “Simulate Single Population” (in the left sidebar) repeatedly until you get an error. You may have to click it a number of times before getting the error.
It seems likely that the problem stems from this portion of code:
MPM.sim <- function(t, MPM, πNought) {
# The simulation function takes as arguments: duration t, the MPM matrix of transition probabilities, and some initial condition πNought
sim <- as.numeric(t)
if (missing(πNought)) { # If the initial state is not specified
sim[1] <- (sample(1:(N+1),1) - 1) # Then take a random initial state
}else{sim[1]<-πNought} # If the initial state is specified, start from that state.
for(i in 2:t){ # For each step of the simulation after the initial state,
newstate <- sample(1:(N+1),1,prob=MPM[sim[i-1],]) # The transition to the next state is given by the transition matrix MPM.
sim[i] <- newstate
}
sim
}
I'd so appreciate any help or suggestions on how to fix this!
The solutions were:
For the error occasionally occurring in large populations (N>100)—a normalizing term for the transition probabilities had gone missing! Replacing it resolved the issue.
For the error occasionally occurring in small populations (N<10)—there was an indexing error in the sampling function. I relabeled the rows and columns of the transition matrix that I use for sampling transition probabilities, and then (falsely) thought that I could refer to the rows by their new names. Accounting for counting from 0 resolved the issue.
Thanks again to invaluable help from @eipi10.