Mean imputation for multidimensional covariate using JAGS

125 views
Skip to first unread message

Martha Wohlfeil

unread,
Mar 7, 2019, 4:12:05 PM3/7/19
to hmecology: Hierarchical Modeling in Ecology
Hello,

I am attempting to include a random effect which includes NAs in my N-mixture model. I would like to use mean imputation to deal with the missing data. My problem is that my random variable is multidimensional, and I am struggling with how to tell JAGS to subset my data.

For context, I am modeling avian point counts, where we visited each site three times in a given year. Each visit, a different observer visited the site. I would like to model the random effect of observer, but I would like a different output for each of the 3 visits. 

Here is my model. The text in blue represents the code to define the random effect of observer. Attached is an image of the head of the data used in this model. 

model { 

  for(i in 1:n_obs){
    alpha3[i] ~ dnorm(0, sigma.o)
  }
  for( i in 1:M) 
  {
    N[i] ~ dpois(lambda[i])  
    log(lambda[i]) <- alpha0 + beta1*elev[i]  #State model

    for (j in 1:J) {
      C[i,j] ~ dbin(p[i,j], N[i])
      logit(p[i,j]) <- beta0 + alpha3[obs_num[i,j]]  #Observation model
    }
    
      for (i in 1:n_obs){
    obs_num[i,1] ~ dnorm(0, 0.001)    #Attempting to subset obs_num 
    }
       for (i in 1:n_obs){
    obs_num[i,2] ~ dnorm(0, 0.001)
    }
       for (i in 1:n_obs){
    obs_num[i,3] ~ dnorm(0, 0.001)
    }
    
  }
  
  beta0 ~ dnorm(0,1)
  alpha0 ~ dnorm(0,1)
  beta1 ~ dnorm(0,1)
  sigma.o ~ dunif(0,1)
}




Additionally, I have tried:

model { 

  for(i in 1:n_obs){
    alpha3[i] ~ dnorm(0, sigma.o)
  }
  for( i in 1:M) 
  {
    N[i] ~ dpois(lambda[i])  
    log(lambda[i]) <- alpha0 + beta1*elev[i]  #State model

    for (j in 1:J) {
      C[i,j] ~ dbin(p[i,j], N[i])
      logit(p[i,j]) <- beta0 + alpha3[obs_num[i,j]]  #Observation model
    }
    
      for (i in 1:n_obs){
      for (j in 1:3){
    obs_num[i,j] ~ dnorm(0, 0.001)  #a different attempt to subset
    }
   }
    
  }
  
  beta0 ~ dnorm(0,1)
  alpha0 ~ dnorm(0,1)
  beta1 ~ dnorm(0,1)
  sigma.o ~ dunif(0,1)
}

When I run either of these models in R using jagsUI, I get the error "Compilation error; Attempt to redefine node obs_num." 

The model has run fine without the addition of this random effect. Any advice about sub-setting the data within the model structure would be greatly appreciated, and I would be happy to provide more information/code! Apologies if I have not included pertinent information, this is my first time posting. 

Thanks,
Martha  






head_of_model_data.JPG

Daniel J Hocking

unread,
Mar 7, 2019, 6:20:53 PM3/7/19
to Martha Wohlfeil, hmecology: Hierarchical Modeling in Ecology
Martha,

At a quick glance it looks like you need to close out the for(i in 1:M) loop before restarting the for(i in 1:n_obs) loop. That might solve the problem in both cases (move one “}” up to earlier in the code. That’s what’s giving you the redefining n_obs error since it’s trying to redefine it in each loop up to M.

Hope that helps,
Dan

Daniel J. Hocking, Ph.D.
Assistant Professor
Frostburg State University
Compton Science Center, room 309
101 Braddock Road
Frostburg, MD 21532-2303







--
*** Three hierarchical modeling email lists ***
(1) unmarked: for questions specific to the R package unmarked
(2) SCR: for design and Bayesian or non-bayesian analysis of spatial capture-recapture
(3) HMecology (this list): for everything else, especially material covered in the books by Royle & Dorazio (2008), Kéry & Schaub (2012), Kéry & Royle (2015)
---
You received this message because you are subscribed to the Google Groups "hmecology: Hierarchical Modeling in Ecology" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hmecology+...@googlegroups.com.
To post to this group, send email to hmec...@googlegroups.com.
Visit this group at https://groups.google.com/group/hmecology.
To view this discussion on the web visit https://groups.google.com/d/msgid/hmecology/a3748a77-4c9c-4a42-9003-060c8c623736%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<head_of_model_data.JPG>

Message has been deleted

paul...@noaa.gov

unread,
Mar 7, 2019, 10:07:03 PM3/7/19
to hmecology: Hierarchical Modeling in Ecology
Hi Martha,
Just took a quick glance at your code and what popped out to me was that some of your parameter indices are random variables. I think when I've tried this in the past I've run into errors.  see e.g.

https://stats.stackexchange.com/questions/154530/jags-choosing-a-random-subset-of-a-vector

So in alpha3[obs_num[i,j]]  , obs_num is a random variable which I think will create problems.  Then again I might be wrong...I don't use JAGS much.

Cheers, Paul
Reply all
Reply to author
Forward
0 new messages