问题
Editing this post for simplification according to @agstudy
I am trying to develop a model that simulates a polymer using a random uniform distribution.
The model has 2 states
State 1 (probability of state 1 if in state 2 is .003): growth probability, A = .01 shrink probability, B = .0025
State 2 (probability of state 2 if in state 1 is .0003): growth probability, A = .01 shrink probability, E = .05
Simulation starts in State 1
While in State 1, sample random numbers from data.frame1, if # < .0025 input -1 in data.frame2, if # < .01 input +1 in data.frame2, if neither input 0 in data.frame2. continue until reaching # < .0003 (probability to enter state 1)
If # < .0003 is sampled, input -1 into data.frame2, and switch to state 2. continue sampling random numbers, while in state 2 if # < .003 input +1 into data.frame2 and switch to State 1 and proceed as indicated above. If in state 2 the # >= .003, and if # < .05 input -1 into data.frame2, else input 0 in data.frame2
I hope this clears things up @agstudy, but there is a chance it might not… sorry if that is the case.
here is an example generated in excel of how a small set should look
the 1st column would be from data.frame1 and the input column would be data.frame2, i included state and length column for explanatory purposes.
rnumbers state input length
0 1 0 0 # initialized state
0.009413895 1 1 1
0.052959306 1 1 2
0.002453354 1 -1 1
0.000290324 2 -1 0
0.093312954 2 0 0
0.077210941 2 0 0
0.04924944 2 -1 -1
0.010590107 2 -1 -2
0.03308292 2 -1 -3
0.037239807 2 -1 -4
0.007889393 2 -1 -5
0.026476751 2 -1 -6
0.000454509 1 1 -5
0.009820683 1 1 -4
0.019836999 1 1 -3
0.009380381 1 1 -2
0.083149383 1 1 -1
0.022212547 1 1 0
0.051917035 1 1 1
0.05032031 1 1 2
0.03956718 1 1 3
0.032664445 1 1 4
0.004352291 1 1 5
0.08902418 1 1 6
0.095643962 1 1 7
回答1:
I would write something like this:
step_generator <- function(rnum)
ifelse(rnum<C,
ifelse(rnum>=D,-5,ifelse(rnum<B,-1,ifelse(rnum<A,1,0))),
ifelse(rnum<B,-1,ifelse(rnum<A,1,0)))
来源:https://stackoverflow.com/questions/20304101/program-simple-simulation-in-r