MY following function denotes n as days and initial value x1. I'm trying to find the number of days it takes from any value or rmicro<30
to reach rmicro>=40
.
Asked
Active
Viewed 84 times
-3

Something Asian
- 1
- 1
1 Answers
1
Try while
instead of for
:
i = 2
while(si[i - 1] < 40){
si[i]=si[i-1]+rpois(1,0.978+0.2*si[i-1]*exp(-0.03*si[i-1]))-rbinom(n,si[i- 1],0.1)
i = i + 1
}
print(paste0('Iterations until 40: ', i - 1))
print(paste0('First value >= 40 ', si[i - 1]))

Keith Mertan
- 136
- 4
-
Hi sorry i tried your code but it doesn't work... what is i++? – Something Asian Apr 18 '18 at 22:27
-
Try it out now. Didn't realize R doesn't accept `i++` as a substitute for `i = i + 1` – Keith Mertan Apr 18 '18 at 22:30
-
nope... still doesn't seem to work haha... – Something Asian Apr 18 '18 at 22:33
-
When saying something doesn't work, you need to explain what error you're getting or else no one on here can help you. If you've initialized `si[1]` and `n`, which I assumed you did based on your original post, this should work. Also, what is `n`? Do you really want to stop your loop if you reach `n` iterations and haven't received `40` as a result, like your original post entails? – Keith Mertan Apr 18 '18 at 22:58
-
Sorry... I get the response `Error in si[i - 1] : object of type 'closure' is not subsettable` Also what do you mean by initialising? – Something Asian Apr 18 '18 at 23:08