[R-sig-ME] Trouble looping model using glmmTMB

Ben Bolker bbolker at gmail.com
Sun Apr 8 20:21:33 CEST 2018


Can you post a reproducible example?  This example (which I set up to
match what I thought you were describing) seems to work OK at least
for 20 bootstrap reps, still waiting on the 100-rep run  ... [might be
better to continue this conversation at
https://github.com/glmmTMB/glmmTMB/issues ...]

## simulate primary example data
set.seed(101)
nobs <- 479
ng <- 110
dd <- data.frame(x=rnorm(nobs),f=factor(sample(ng,size=nobs,replace=TRUE)))
library(emdbook)
rf <- rnorm(ng)
dd$y <- emdbook::rzinbinom(nobs,mu=exp(2+dd$x+rf[dd$f]),zprob=0.2,size=1)

## fit primary model
library(glmmTMB)
m0 <- glmmTMB(y~x+(1|f),data=dd,family="nbinom2",zi=~1)

## npar bootstrap by levels
bootsamp <- function(data=dd,grp=dd$f) {
   ss <- split(data,grp)
   bg <- levels(grp)
   bootgrps <- sample(grp,size=length(grp),replace=TRUE)
   do.call(rbind,ss[bootgrps])
}

## test
update(m0,data=bootsamp())

nboot <- 100
## run bootstrap reps
res <- matrix(ncol=4,nrow=nboot)
for (i in seq(nboot)) {
  print(i)
  set.seed(100+i)
  bootfit <- update(m0,data=bootsamp())
  res[i,] <- unlist(fixef(bootfit))
}

On Sun, Apr 8, 2018 at 10:27 AM, Viraj Torsekar
<viraj.torsekar at gmail.com> wrote:
> Hello all,
>
> I'm trying to find out if distance moved by crickets is a function of
> predation risk. My response variable is 'distance moved' and the predictor
> is probability of spatial proximity with predator, ranging from 0 to 1. The
> response variable is zero-inflated (about 77% values are zeroes) and its
> variance is far higher than its mean. Hence, I tried running zero-inflated
> negative binomial mixed models using glmmADMB, which failed (mixed because
> I have multiple values per individual). Following was the error I kept
> encountering: "function maximizer failed" (attaching a text file with
> details of this model by keeping debug=TRUE).
>
> Hence, I shifted to glmmTMB (version: 0.2.0), on Dr. Bolker's advice, and
> it worked! But the problem is, when I try bootstrapping the model using to
> obtain confidence intervals, I keep getting the following error after
> varying number of runs: 'Error in optimHess(par.fixed, obj$fn, obj$gr):
> gradient in optim evaluated to length 1 not 5'. This non-parametric
> bootstrapping routine involves for loops in which the model is run using
> bootstrapped groups (belonging to the grouping variable; individual.id in
> my case) and the model coefficients thus obtained constitute the confidence
> intervals. I've tried running 10,000 iterations, but the error pops up
> within 10 to 100 runs.
>
> Does anyone have suggestions regarding what can be changed?
>
> Details of the model run singly and not in the loop:
>
>  Family: nbinom2  ( log )
> Formula:          movement.whole ~ poc + (1 | female.id)
> Zero inflation:                  ~1
> Data: incrisk_females_comm
>
>      AIC      BIC   logLik deviance df.resid
>   1725.1   1745.9   -857.5   1715.1      474
>
> Random effects:
>
> Conditional model:
>  Groups    Name        Variance Std.Dev.
>  female.id (Intercept) 0.07924  0.2815
> Number of obs: 479, groups:  female.id, 110
>
> Overdispersion parameter for nbinom2 family (): 1.59
>
> Conditional model:
>             Estimate Std. Error z value Pr(>|z|)
> (Intercept)  4.66384    0.14161   32.93   <2e-16 ***
> poc         -0.08815    0.20978   -0.42    0.674
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> Zero-inflation model:
>             Estimate Std. Error z value Pr(>|z|)
> (Intercept)   1.2442     0.1098   11.34   <2e-16 ***
>
> Please do mention if you need further details. Thank you in advance.
>
> Viraj Torsekar,
> PhD Candidate,
> Centre for Ecological Sciences,
> Indian Institute of Science
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models



More information about the R-sig-mixed-models mailing list