I asked my original question incorrectly, so here is the better version.
I would like to generate a model using R. The gist of the model --> A polymer can grow or shrink at different rates. Every now and then the rate of shrinking increases by 20 fold while the growth rate stays constant, this is considered the "catastrophic state". The state switches to and from the "catastrophic state" with certain rates. The question then becomes how does the length of the polymer change with respect to time??? Here is what I am thinking:
Initialization:
5 polymers of length 0 (indicated by column index)
rnumbers <- data.frame(replicate(5,runif(20000, 0, 1)))
rstate <- rnumbers # copy the structure
rstate[] <- NA # preserve structure with NA's
# Init:
rstate[1, ] <- c(0)
> head(rstate)
X1 X2 X3 X4 X5
1 0 0 0 0 0
2 NA NA NA NA NA
3 NA NA NA NA NA
4 NA NA NA NA NA
5 NA NA NA NA NA
6 NA NA NA NA NA
I want to run the simulation for 200s
Setting the rates :
dt <- c(.01) # how time is being divided
A <- dt*1 # probability growth will occur under normal conditions
B <- dt*.25 # probability shrinking will occur under normal conditions
C <- dt*.03 # probability "catastrophic state" will occur
D <- dt*.3 # probability normal state will occur once in "catastrophic state"
E <- dt*5 # probability shrinking will occur "catastrophic state"
You notice that under normal conditions the probability of growth exceeds the probability of shrinkage, however when in "catastrophic state" shrinkage dominates. Also dt = .01 for 20000 rows in the data frame adds up to 200s
Not taking into account the switching to catastrophic state, this is what the code looks like :
library("Rcell")
rnumbers <- data.frame(replicate(5,runif(20000, 0, 1)))
dt <- c(.01)
A <- dt*1
B <- dt*.25
C <- dt*.03
D <- dt*.3
E <- dt*5
rstate <- rnumbers # copy the structure
rstate[] <- NA # preserve structure with NA's
# Init:
rstate[1, ] <- c(0)
step_generator <- function(col, rnum){
for (i in 2:length(col) ){
if( rnum[i] < B) {
col[i] <- -1
}
else {
if (rnum[i] < A) {
col[i] <- 1
}
else {
col[i] <- 0
}
}
}
return(col)
}
# Run for each column index:
for(cl in 1:5){ rstate[ , cl] <-
step_generator(rstate[,cl], rnumbers[,cl]) }
rstate1 <- transform(rstate, time = rep(dt))
rstate2 <- transform(rstate1, cumtime = cumsum(time))
cum_sum <- apply(rstate2, 2, cumsum)
cum_sum <- as.data.frame(cum_sum)
dev.new(width=5, height=5)
cplot(cum_sum, X2 ~ time, geom = "line")
If you run this code, a jagged line with a positive slope is plotted over a 200 units of time. The package "Rcell" is required to use the plot I used.
My difficulty arises when I try to incorporate the catastrophic state. How can I make this code incorporate the catastrophic state? I imagine something like this, but I am unsure how to translate the syntax:
step_generator <- function(col, rnum)
for (i in 2:length(col)
start in normal growth conditions (growth prob = A ; shrinkage prob = B)
if rnum[i] < C switch to catastrophic state (
growth prob = A ;
shrinkage prob = E),
else stay in normal growth conditions (i.e. if rnum[i] >= C)
stay in catastrophic state until rnum[i] < D, when this happens switch back to normal growth conditions.
repeat through the entire 20000 rows
thanks for any and all help!