Alternatives for 'if-else-statement', writing own function

10 views
Skip to first unread message

Magali Frauendorf

unread,
May 9, 2021, 6:29:17 AMMay 9
to nimble-users

Dear nimble team,

I have this part of code in my multi-state model estimating individual survival for each state and time occasion, with first[i] being the first capture/sighting event and T the number of time occasions.

for(k in 1:9){ # for each state

    for(i in 1:n){ # for each individual

      for(j in (first[i]+1):T){ # for each sampling after first capture

        logit(phi[k,i,j]) <- mu.phi[k,i,j]  + epst.phi[k,j-1]  # epst.phi being a random time effect

        mu.phi[k,i,j] <- int.phi[k,age[i,j-1]] + beta1 * eta1[i]  #int.phi=intercept for each age/state       #occasion, eta being the individual condition

      } #j

    } #i=n

  } #k

 

Since I want to estimate the effect of condition on survival only for one time occasion after the capture event (seeing the condition as a dynamic rather than static individual trait), I was thinking of an if-else-statement as for instance like this:

for(k in 1:9){ # for each state

    for(i in 1:n){ # for each individual

      for(j in (first[i]+1):T){ # for each sampling after first capture

       

        if (T=tcatch)    #where tcatch is a vector with the time occasion of capture for each individual

        logit(phi[k,i,j]) <- mu.phi[k,i,j]  + epst.phi[k,j-1] 

        mu.phi[k,i,j] <- int.phi[k,age[i,j-1]] + beta1 * eta1[i]  #eta being the condition

 

       else

        logit(phi[k,i,j]) <- mu.phi[k,i,j]  + epst.phi[k,j-1] 

        mu.phi[k,i,j] <- int.phi[k,age[i,j-1]]                            # here without eta

 

      } #j

    } #i=n

  } #k

 

So that the survival is estimated with the term“beta1+eta1[i]” only for the year after capture and otherwise without the “beta1+eta1[i]”-term.

I read that if-then-else statements like this will not work in this case. So I guess I would need to write a custom function which includes the if-else statement and plug it in the code. I was wondering if you think that a function could work in my case? Since I have never written an own function in nimble, can you give me some suggestions as a starting help? Or do you have other ideas how I could model this if-else-statement? 

Thank you very much!

Magali

Ben Goldstein

unread,
May 9, 2021, 4:01:33 PMMay 9
to Magali Frauendorf, nimble-users
Hi Magali,

I think there's a chance the if-statement will work in this case. If everything inside the if-statement is a constant--if the if-statement can be evaluated when the model is built--then it can be used. This includes for-loop indices. I.e. something like

if (j == tcatch[i])

should be ok.

Ben

--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/35ba3bb3-8ac8-4031-9743-6052bfd0ba6bn%40googlegroups.com.

Chris Paciorek

unread,
May 10, 2021, 2:18:33 PMMay 10
to Magali Frauendorf, nimble-users, Ben Goldstein
Actually, NIMBLE is not able to handle this kind of conditional statement during model building.

The following should work:

for(j in (first[i]+1):T){ # for each sampling after first capture
        logit(phi[k,i,j]) <- mu.phi[k,i,j]  + epst.phi[k,j-1]  # epst.phi being a random time effect
        mu.phi[k,i,j] <- int.phi[k,age[i,j-1]] + (j == tcatch[i]) * beta1 * eta1[i] 
      } 

The use of the equality check "j == tcatch[i]" should 'zero out' the second term in the sum when it shouldn't be included.

-chris

Ben Goldstein

unread,
May 10, 2021, 2:26:36 PMMay 10
to Chris Paciorek, Magali Frauendorf, nimble-users
Whoops. Thanks, Chris!

Magali Frauendorf

unread,
May 11, 2021, 7:31:24 AMMay 11
to nimble-users
Hi Chris,

Thank you! Indeed including the equality check in the code solved it!

Best,
Magali

Reply all
Reply to author
Forward
0 new messages