using derived parameter from latent state as a covariate

45 views
Skip to first unread message

Dilsad Dagtekin

unread,
Jul 31, 2023, 5:23:50 AM7/31/23
to nimble-users
Hi everyone!

I am trying to do a capture-recapture model in nimble. Model works fine and all, however, I have population size covariate in the model and I thought it could also be calculated with the same model and used in the same model, could be useful to account for uncertainty in that variable.
 But when I tried it out I got an error saying:
"The graph has cycles; topological sorting is only possible in acyclic graphs, Invalid value"

So, I was wondering if it is possible to use a derived parameter (population size) from latent state (z, true survival) in a model as a covariate in the same model in nimble? 

The relevant code snippet is below (the model is rather big, if need be I can send it):

for (t in 1:(n.occasions-1)){    
logit(phi_jpost[t]) <- alpha_phi + beta1*cov1[t] + beta2*cov2[t] + beta3t*popsize_out[t]
 }

 ### LIKELIHOOD ##
  for (i in 1:M){
    ## Define latent state at first occasion ##
    z[i,1] <- 1   # Make sure that all M individuals are in state 1 at t=1
    for (t in 2:n.occasions){
     
      ## State process: draw S(t) given S(t-1) ##
      z[i,t] ~ dcat(ps[z[i,t-1], i, t-1, 1:6])}
  
      # total jpost
      z_jpost[i, t-1] <- equals(z[i,t], 2)
     
      # total jpre
      z_jpre[i, t-1] <- equals(z[i,t], 3)
     
      # total apost
      z_apost[i, t-1] <- equals(z[i,t], 4)
     
      # total apre
      z_apre[i, t-1] <- equals(z[i,t], 5)
} # i
} # t

for (t in 1:n.occasions){
    N_jpost[t] <- sum(z_jpost[1:M,t])   
    N_jpre[t] <- sum(z_jpre[1:M,t])
    N_apost[t] <- sum(z_apost[1:M,t])
    N_apre[t] <- sum(z_apre[1:M,t])

    popsize_out[t] <-  N_jpost[t] + N_jpre[t] + N_apost[t] + N_apre[t]
  }

Any help is appreciated.
Cheers,
Dilsad

Quresh Latif

unread,
Jul 31, 2023, 5:30:26 PM7/31/23
to nimble-users
I have tried using latent states or parameters derived from latent states as covariates in a couple different contexts. Sometimes it has worked and sometimes it hasn't. The main thing to remember is that it needs to make sense for information to flow both from the latent state to the dependent parameter and back the other way. If it doesn't make sense for information to flow in both directions, the model will choke or you will get wonky estimates. In this case, it looks to me like 'z' is a latent state representing the realization of survival probability, so you can't then use 'z' to inform survival probability. If you are trying to do this, you have a cycle in your model graph, which is why you are getting that error.

Dilsad Dagtekin

unread,
Aug 1, 2023, 2:16:50 AM8/1/23
to Quresh Latif, nimble-users
Hi Quresh,

Thanks for your reply, that's what I thought so as well. 

Best,
Dilsad

--
You received this message because you are subscribed to a topic in the Google Groups "nimble-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nimble-users/cER5DnhdrhE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/8e7ad929-ffe4-47b3-921a-e3d83ee7fc93n%40googlegroups.com.


--
A. Dilsad Dagtekin
PhD Student at Population Ecology Research Group
University of Zurich, Department of Evolutionary Biology and Environmental Studies 
Ecology and Evol. Biology Society of Turkey: www.ekoevo.org
ITU Alumni '16 & '18 | AFS Alumni '12

Reply all
Reply to author
Forward
0 new messages