0
deadcheck<-function(a,t){                       #function to check if dead for specific age at a time age sending to function 
 roe<-which( birthmort$age[i]==fertmortc$min & fertmortc$max) #checks row in fertmortc(hart) to pick an age that meets min and max age requirements I think this could be wrong...
prob<-1-(((1-fertmortc$mortality[roe])^(1/365))^t) #finds the prob for the row that meets the above requirements
 if(runif(1,0,1)<=prob) {d<-TRUE} else {d<-FALSE} #I have a row that has the probability of death every 7 days. 
return(d) #outputs if dead

Background: I am creating an agent based model that is a population in a dataframe that is simulating how Tuberculosis spreads in a population. ( I know that there are probably 10000 better ways of having done this). I have thus far created a loop that populates my dataframe with people ages etc. I am now trying to create a function that will go to a chart that lists the probability of death per year, based on a age bracket. 0-5,5-10,10-15 etc. (I have math in there b/c I want it to check who lives, dies, makes babies every 7 days). I have a function similar to this that check who is pregnant and it works. However I for the life of me can't figure out why this function is not working. I keep getting the following error. Error in if (runif(1, 0, 1) <= prob) { : argument is of length zero I am unsure how to fix this.

I apologize in advanced it this is a dumb question, I have been trying to teach myself to code over the last 4-5 months. If I asked this question in the wrong format or incorrectly then please let me know how to do so correctly.

Ebony
  • 1
  • 2
  • Please give a [mcve]. In particular -- give a reproducible input which leads to that error. The problem seems to be that your vector `prob` is of length 0. Since I have no idea what `fertmortc$mortality` is, it is impossible to say why this might be the case. – John Coleman Mar 17 '18 at 18:35
  • Thank you! I will read those instructions and edit the post. I appreciate the help. – Ebony Mar 18 '18 at 16:55
  • Thank you! I will read those instructions and edit the post. I appreciate the help. – Ebony Mar 18 '18 at 16:55
  • `if(runif(1,0,1)<=prob) {d<-TRUE} else {d<-FALSE}` could be replaced by `d <- (runif(1) <= prob)` The result of a comparison is already Boolean -- you don't need to use `if ... else` to convert it to Boolean. Also -- `d` is just a local variable. Why compute it in 1 line just to return it the next? The last two lines of your function can be replaced by the single line `runif(1) <= prob` (no need for an explicit return). – John Coleman Mar 18 '18 at 17:17
  • Thanks John, I got it to work Kinda, not sure how or why. I appreciate the help, but I am not sure I am understanding why it works with one function but when I write the same function just pulling data from a different column in the same dataframe it doesn't work. – Ebony Mar 18 '18 at 23:34
  • Are you sure that it works in other functions? If "it" refers to code like the above then maybe it isn't working. The mere fact that a function is producing output doesn't mean the output is correct. Even though with your data the above code produces an error, in other cases the exact same code won't give you an error but will produce nonsensical output (since even if for a given input `prob` is nonempty, it probably isn't what you think it is). Bugs only trigger runtime errors when you are lucky. You might want to run test cases and carefully check the output. – John Coleman Mar 19 '18 at 10:29
  • It does work in the for loop that I have it in. It is making other functions, if statements not work( why I don't know). But that being said it still gives me the error code even though it is doing what I want it to do. I have tested it several times. I will try to post more of code when I get a chance. – Ebony Mar 21 '18 at 20:38
  • I suspect that you might need the `ifelse()` function rather than the `if ... else` construct. If you post the other code I'll look at it. – John Coleman Mar 21 '18 at 21:22
  • John I got it to work!!!!!! 100% work!!! I am tickled right now!!! I had it running through the whole data frame, which has 0values in in! Fixed it ! – Ebony Mar 22 '18 at 14:45
  • Glad that it worked. If my answer below was part of the solution, you could always mark it as "accepted". Good luck with your studies. – John Coleman Mar 22 '18 at 15:05

2 Answers2

0

Value of prob is of length zero. It means

prob = NULL

in this case. Try to print alter your code and add

print(prob)

so you can check partial result.

  • I did the print (prob) and it says numeric (0) isn't this what I want? – Ebony Mar 18 '18 at 16:54
  • @Ebony `numeric(0)` *means* that it is a numeric vector of length `0`. The only thing that would really make sense in your code is is `prob` is a numeric vector of length 1 – John Coleman Mar 18 '18 at 17:11
  • @ebony numeric(0) is a NULL value for numeric variable. It means that this `prob<-1-(((1-fertmortc$mortality[roe])^(1/365))^t) ` doesn't work – Patrik Plecho Mar 21 '18 at 17:00
  • That makes sense. I figured out that I needed the data for the probabilities in separate data files. It wouldn't let me pull data from the same excel spreadsheet. I now how it where the function works, and does what I want in the loop, but it still says the same error even though its making the correct changes. – Ebony Mar 21 '18 at 20:36
0

As you suspected in your comments, the expression

birthmort$age[i]==fertmortc$min & fertmortc$max

is problematic. What this does is evaluate the comparison birthmort$age[i]==fertmortc$min, and then takes the result of that comparison and combines it with fertmortc$max using the and operator. This involves forming the and of a Boolean value and an integer, which is unlikely to make much sense.

Just guessing, you perhaps want:

birthmort$age[i] >= fertmortc$min & birthmort$age[i] <= fertmortc$max

I don't know if this will fix your problem -- you haven't given enough to test it. For optimal help, you should give a reproducible example. See this for how to do so in R

John Coleman
  • 51,337
  • 7
  • 54
  • 119