Zero-truncated poisson lognormal

73 views
Skip to first unread message

Bert van der Veen

unread,
Oct 18, 2019, 5:38:16 AM10/18/19
to TMB Users
Hello all,

I'm attempting to fit the model from Engen et al. 2011 ("Estimating similarity of communities: a parametric approach to spatio-temporal analysis of species diversity"), who employ a zero-truncated poisson lognormal distribution to estimate the similarity between two communities, when the sampling intensity is unknown.

Unfortunately, the optimizer is unable to evaluate the function returned by TMB at the (true) starting values, so I suspect there is an issue with either my code, or the combination of the model and the laplace approximation.

Any feedback, suggestions or improvements are greatly appreciated.

TMB-code
#include <TMB.hpp>
#include<math.h>

template<class Type>
Type objective_function<Type>::operator() ()
{
  DATA_MATRIX
(y);
 
  PARAMETER_VECTOR
(u);
  PARAMETER_VECTOR
(rho);
  PARAMETER_VECTOR
(lg_sigma);
  PARAMETER_VECTOR
(mu);
 
  vector
<Type> sigma = exp(lg_sigma);
 
 
int n = y.cols();
 
int p = y.rows();
 
 
using namespace density;
 
Type nll = 0.0;

 
for (int j=0; j<p;j++){
       
Type q = 1.0;
       
Type q0 = 1.0;
       
for (int i=0; i<n;i++){  
         q
*= (exp(u(i)*sigma(i)*y(j,i)+mu(i)*y(j,i)-exp(-(u(i)*sigma(i)+mu(i))))/exp(lfactorial(y(j,i))));
         q0
*= exp(-exp(-(u(i)*sigma(i)+mu(i))));  
       
}
        q
*= exp(-1*UNSTRUCTURED_CORR(rho)(u));
        q0
*= exp(-1*UNSTRUCTURED_CORR(rho)(u));
       
    nll
-= log(q/(1-q0));
 
}

  REPORT
(UNSTRUCTURED_CORR(rho).cov());
 
 
return nll;
 
}

R-code
library(poilog)

y
<- rbipoilog(S=100, mu1=1, mu2=2, sig1=1, sig2=2, rho=0.7, nu1=0.5, nu2=0.7)
sigma
<- c(1, 2)
mu
<-c(1, 2)
rho
<- 0.7
u
<- c(0.5, 0.7)

library
(TMB)
compile
(file="TMB.cpp")

dyn
.load(dynlib("TMB"))
objr
<- MakeADFun(
  data
= list(y = y),
  parameters
= list(u=u, rho=rho, lg_sigma=log(sigma), mu=mu),
  DLL
= "TMB",
  random
="u")

opt
<-optim(objr$par,objr$fn,objr$gr,method="BFGS")

dyn
.unload(dynlib("TMB"))

Bert

Ben Bolker

unread,
Oct 18, 2019, 9:52:15 AM10/18/19
to tmb-...@googlegroups.com

I haven't looked at this carefully at all, but (1) I would strongly
recommend doing the calculations in the log-space as far as possible,
*then* exponentiating (use logspace_add and logspace_sub where possible,
see e.g.
https://kaskr.github.io/adcomp/group__special__functions.html#ga2d5a07f1202dc628ba18ee9a2af8b9cd
and (2) I'm not sure of the detailed form of this, but it might be
possible to implement in glmmTMB using an observation-level random effect?






On 2019-10-18 5:38 a.m., Bert van der Veen wrote:
> Hello all,
>
> I'm attempting to fit the model from Engen et al. 2011 ("Estimating
> similarity of communities: a parametric approach to spatio-temporal
> analysis of species diversity"), who employ a zero-truncated poisson
> lognormal distribution to estimate the similarity between two
> communities, when the sampling intensity is unknown.
>
> Unfortunately, the optimizer is unable to evaluate the function returned
> by TMB at the (true) starting values, so I suspect there is an issue
> with either my code, or the combination of the model and the laplace
> approximation.
>
> Any feedback, suggestions or improvements are greatly appreciated.
>
> *TMB-code*
> |
> #inclu
> |
>
> Bert
>
> --
> To post to this group, send email to us...@tmb-project.org. Before
> posting, please check the wiki and issuetracker at
> https://github.com/kaskr/adcomp/. Please try to create a simple
> repeatable example to go with your question (e.g issues 154, 134, 51).
> Use the issuetracker to report bugs.
> ---
> You received this message because you are subscribed to the Google
> Groups "TMB Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to tmb-users+...@googlegroups.com
> <mailto:tmb-users+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/tmb-users/6748dd5b-8501-4e50-a369-60f3b9212ff9%40googlegroups.com
> <https://groups.google.com/d/msgid/tmb-users/6748dd5b-8501-4e50-a369-60f3b9212ff9%40googlegroups.com?utm_medium=email&utm_source=footer>.

Andrea Havron

unread,
Oct 18, 2019, 3:30:34 PM10/18/19
to Bert van der Veen, TMB Users
 I agree with Ben that you should look into using a Poisson distribution with observation-level random effect. I've been able to generate data using the poilog package in R (from Engen et al. 2002) which results in comparable likelihoods calculated from dpoilog and TMB using a Poisson with an observation-level random effect. The TMB model can easily be extended to treat the random effects as arriving from a spatial-temporal process.  

--
To post to this group, send email to us...@tmb-project.org. Before posting, please check the wiki and issuetracker at https://github.com/kaskr/adcomp/. Please try to create a simple repeatable example to go with your question (e.g issues 154, 134, 51). Use the issuetracker to report bugs.
---
You received this message because you are subscribed to the Google Groups "TMB Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmb-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tmb-users/6748dd5b-8501-4e50-a369-60f3b9212ff9%40googlegroups.com.


--
PhD Candidate, Statistics
University of Auckland
MS, Marine Resource Management 




Bert van der Veen

unread,
Oct 21, 2019, 2:55:48 AM10/21/19
to TMB Users
Thanks for the feedback, both. Although it isn't really my intention to fit this model with glmmTMB, perhaps your comments will help me understand what's going on!
To unsubscribe from this group and stop receiving emails from it, send an email to tmb-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages