Running 'tests.R' [5s]
Running the tests in 'tests/tests.R' failed.
Complete output:
  > #devtools::install_github("psolymos/detect")
  > library(detect)
  Loading required package: Formula
  Loading required package: stats4
  Loading required package: pbapply
  detect 0.4-6 	 2023-03-08
  > 
  > ## --- run examples with \dontrun sections ---
  > 
  > help_pages <- c(#"bootstrap",
  +     "cmulti", "convertEDR", "databu", "datocc",
  +     "hbootindex", "oven", "svabu", "svocc")
  > 
  > for (i in help_pages) {
  +     cat("\n\n---------- detect example:", i, "----------\n\n")
  +     eval(parse(text=paste0("example(", i,
  +         ", package = 'detect', run.dontrun = TRUE)")))
  + }
  
  
  ---------- detect example: cmulti ----------
  
  
  cmulti> simfun1 <- function(n = 10, phi = 0.1, c=1, tau=0.8, type="rem") {
  cmulti+     if (type=="dis") {
  cmulti+         Dparts <- matrix(c(0.5, 1, NA,
  cmulti+                       0.5, 1, Inf,
  cmulti+                       1, Inf, NA), 3, 3, byrow=TRUE)
  cmulti+         D <- Dparts[sample.int(3, n, replace=TRUE),]
  cmulti+         CP <- 1-exp(-(D/tau)^2)
  cmulti+     } else {
  cmulti+         Dparts <- matrix(c(5, 10, NA,
  cmulti+                       3, 5, 10,
  cmulti+                       3, 5, NA), 3, 3, byrow=TRUE)
  cmulti+         D <- Dparts[sample.int(3, n, replace=TRUE),]
  cmulti+         CP <- 1-c*exp(-D*phi)
  cmulti+     }
  cmulti+     k <- ncol(D)
  cmulti+     P <- CP - cbind(0, CP[, -k, drop=FALSE])
  cmulti+     Psum <- rowSums(P, na.rm=TRUE)
  cmulti+     PPsum <- P / Psum
  cmulti+     Pok <- !is.na(PPsum)
  cmulti+     N <- rpois(n, 10)
  cmulti+     Y <- matrix(NA, ncol(PPsum), nrow(PPsum))
  cmulti+     Ypre <- sapply(1:n, function(i) rmultinom(1, N, PPsum[i,Pok[i,]]))
  cmulti+     Y[t(Pok)] <- unlist(Ypre)
  cmulti+     Y <- t(Y)
  cmulti+     list(Y=Y, D=D)
  cmulti+ }
  
  cmulti> n <- 200
  
  cmulti> x <- rnorm(n)
  
  cmulti> X <- cbind(1, x)
  
  cmulti> ## removal, constant
  cmulti> vv <- simfun1(n=n, phi=exp(-1.5))
  
  cmulti> m1 <- cmulti(vv$Y | vv$D ~ 1, type="rem")
  
  cmulti> coef(m1)
  log.phi_(Intercept) 
             -1.53207 
  
  cmulti> ## mixture, constant (mix and fmix are identical)
  cmulti> vv <- simfun1(n=n, phi=exp(-1.5), c=plogis(0.8))
  
  cmulti> m2 <- cmulti(vv$Y | vv$D ~ 1, type="mix")
  
  cmulti> coef(m2)
              log.phi logit.c_(Intercept) 
            -1.354201            1.135932 
  
  cmulti> m2f <- cmulti(vv$Y | vv$D ~ 1, type="fmix")
  
  cmulti> coef(m2f)
  log.phi_(Intercept)             logit.c 
            -1.354201            1.135932 
  
  cmulti> ## dist, constant
  cmulti> vv <- simfun1(n=n, tau=exp(-0.2), type="dis")
  
  cmulti> m3 <- cmulti(vv$Y | vv$D ~ 1, type="dis")
  
  cmulti> coef(m3)
  log.tau_(Intercept) 
           -0.2219116 
  
  cmulti> ## removal, not constant
  cmulti> log.phi <- crossprod(t(X), c(-2,-1))
  
  cmulti> vv <- simfun1(n=n, phi=exp(cbind(log.phi, log.phi, log.phi)))
  
  cmulti> m1 <- cmulti(vv$Y | vv$D ~ x, type="rem")
  
  cmulti> coef(m1)
  log.phi_(Intercept)           log.phi_x 
            -2.065230           -1.011945 
  
  cmulti> ## mixture, fixed phi, varying c
  cmulti> logit.c <- crossprod(t(X), c(-2,1))
  
  cmulti> vv <- simfun1(n=n, phi=exp(-1.5), c=plogis(cbind(logit.c, logit.c, logit.c)))
  
  cmulti> m2 <- cmulti(vv$Y | vv$D ~ x, type="mix")
  
  cmulti> coef(m2)
              log.phi logit.c_(Intercept)           logit.c_x 
            -1.611272           -2.052116            1.133957 
  
  cmulti> ## mixture, varying phi, fixed c
  cmulti> log.phi <- crossprod(t(X), c(-2,-1))
  
  cmulti> vv <- simfun1(n=n, phi=exp(cbind(log.phi, log.phi, log.phi)), c=plogis(0.8))
  
  cmulti> m2f <- cmulti(vv$Y | vv$D ~ x, type="fmix")
  
  cmulti> coef(m2f)
  log.phi_(Intercept)           log.phi_x             logit.c 
           -1.8572433          -0.8025137           0.5914228 
  
  cmulti> ## dist, not constant
  cmulti> log.tau <- crossprod(t(X), c(-0.5,-0.2))
  
  cmulti> vv <- simfun1(n=n, tau=exp(cbind(log.tau, log.tau, log.tau)), type="dis")
  
  cmulti> m3 <- cmulti(vv$Y | vv$D ~ x, type="dis")
  
  cmulti> coef(m3)
  log.tau_(Intercept)           log.tau_x 
           -0.5196736          -0.2190564 
  
  cmulti> summary(m3)
  
  Call:
  cmulti(formula = vv$Y | vv$D ~ x, type = "dis")
  
  Distance Sampling (half-normal, circular area)
  Conditional Maximum Likelihood estimates
  
  Coefficients:
                      Estimate Std. Error z value Pr(>|z|)    
  log.tau_(Intercept) -0.51967    0.01280  -40.60   <2e-16 ***
  log.tau_x           -0.21906    0.01486  -14.74   <2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
  
  Log-likelihood: -409.8 
  BIC = 830.2 
  
  
  cmulti> coef(m3)
  log.tau_(Intercept)           log.tau_x 
           -0.5196736          -0.2190564 
  
  cmulti> vcov(m3)
                      log.tau_(Intercept)     log.tau_x
  log.tau_(Intercept)        1.638407e-04 -1.710531e-05
  log.tau_x                 -1.710531e-05  2.209340e-04
  
  cmulti> AIC(m3)
  [1] 823.6315
  
  cmulti> confint(m3)
                           2.5 %     97.5 %
  log.tau_(Intercept) -0.5447612 -0.4945860
  log.tau_x           -0.2481890 -0.1899238
  
  cmulti> logLik(m3)
  'log Lik.' -409.8158 (df=2)
  
  cmulti> ## fitted values
  cmulti> plot(exp(log.tau), fitted(m3))
  
  cmulti> ## prediction for new locations (type = 'rem')
  cmulti> ndf <- data.frame(x=seq(-1, 1, by=0.1))
  
  cmulti> summary(predict(m1, newdata=ndf, type="link"))
     Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   -3.077  -2.571  -2.065  -2.065  -1.559  -1.053 
  
  cmulti> summary(pr1 <- predict(m1, newdata=ndf, type="response"))
     Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.04609 0.07644 0.12679 0.15197 0.21029 0.34879 
  
  cmulti> ## turing singing rates into probabilities requires total duration
  cmulti> ## 5 minutes used here
  cmulti> psing <- 1-exp(-5*pr1)
  
  cmulti> plot(ndf$x, psing, type="l", ylim=c(0,1))
  
  cmulti> ## prediction for new locations (type = 'dis')
  cmulti> summary(predict(m3, newdata=ndf, type="link"))
     Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  -0.7387 -0.6292 -0.5197 -0.5197 -0.4101 -0.3006 
  
  cmulti> summary(pr3 <- predict(m3, newdata=ndf, type="response"))
     Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.4777  0.5330  0.5947  0.6000  0.6636  0.7404 
  
  cmulti> ## turing EDR into probabilities requires finite truncation distances
  cmulti> ## r=0.5 used here (50 m)
  cmulti> r <- 0.5
  
  cmulti> pdet <- pr3^2*(1-exp(-r^2/pr3^2))/r^2
  
  cmulti> plot(ndf$x, pdet, type="l", ylim=c(0,1))
  
  cmulti> ## joint removal-distance estimation
  cmulti> ## is not different from 2 orthogonal estimations
  cmulti> 
  cmulti> simfun12 <- function(n = 10, phi = 0.1, c=1, tau=0.8, type="rem") {
  cmulti+     Flat <- function(x, DIM, dur=TRUE) {
  cmulti+         x <- array(x, DIM)
  cmulti+         if (!dur) {
  cmulti+             x <- aperm(x,c(1,3,2))
  cmulti+         }
  cmulti+         dim(x) <- c(DIM[1], DIM[2]*DIM[3])
  cmulti+         x
  cmulti+     }
  cmulti+     Dparts1 <- matrix(c(5, 10, NA,
  cmulti+                         3, 5, 10,
  cmulti+                         3, 5, NA), 3, 3, byrow=TRUE)
  cmulti+     D1 <- Dparts1[sample.int(3, n, replace=TRUE),]
  cmulti+     CP1 <- 1-c*exp(-D1*phi)
  cmulti+     Dparts2 <- matrix(c(0.5, 1, NA,
  cmulti+                         0.5, 1, Inf,
  cmulti+                         1, Inf, NA), 3, 3, byrow=TRUE)
  cmulti+     D2 <- Dparts2[sample.int(3, n, replace=TRUE),]
  cmulti+     CP2 <- 1-exp(-(D2/tau)^2)
  cmulti+     k1 <- ncol(D1)
  cmulti+     k2 <- ncol(D2)
  cmulti+     DIM <- c(n, k1, k2)
  cmulti+     P1 <- CP1 - cbind(0, CP1[, -k1, drop=FALSE])
  cmulti+     P2 <- CP2 - cbind(0, CP2[, -k2, drop=FALSE])
  cmulti+     Psum1 <- rowSums(P1, na.rm=TRUE)
  cmulti+     Psum2 <- rowSums(P2, na.rm=TRUE)
  cmulti+     Pflat <- Flat(P1, DIM, dur=TRUE) * Flat(P2, DIM, dur=FALSE)
  cmulti+     PsumFlat <- Psum1 * Psum2
  cmulti+     PPsumFlat <- Pflat / PsumFlat
  cmulti+     PokFlat <- !is.na(PPsumFlat)
  cmulti+     N <- rpois(n, 10)
  cmulti+     Yflat <- matrix(NA, ncol(PPsumFlat), nrow(PPsumFlat))
  cmulti+     YpreFlat <- sapply(1:n, function(i) rmultinom(1, N, PPsumFlat[i,PokFlat[i,]]))
  cmulti+     Yflat[t(PokFlat)] <- unlist(YpreFlat)
  cmulti+     Yflat <- t(Yflat)
  cmulti+     Y <- array(Yflat, DIM)
  cmulti+     k1 <- dim(Y)[2]
  cmulti+     k2 <- dim(Y)[3]
  cmulti+     Y1 <- t(sapply(1:n, function(i) {
  cmulti+         count <- rowSums(Y[i,,], na.rm=TRUE)
  cmulti+         nas <- rowSums(is.na(Y[i,,]))
  cmulti+         count[nas == k2] <- NA
  cmulti+         count
  cmulti+     }))
  cmulti+     Y2 <- t(sapply(1:n, function(i) {
  cmulti+         count <- colSums(Y[i,,], na.rm=TRUE)
  cmulti+         nas <- colSums(is.na(Y[i,,]))
  cmulti+         count[nas == k2] <- NA
  cmulti+         count
  cmulti+     }))
  cmulti+     list(Y=Y, D1=D1, D2=D2, Y1=Y1, Y2=Y2)
  cmulti+ }
  
  cmulti> ## removal and distance, constant
  cmulti> vv <- simfun12(n=n, phi=exp(-1.5), tau=exp(-0.2))
  
  cmulti> res <- cmulti2.fit(vv$Y, vv$D1, vv$D2)
  Error in aggregate.data.frame(as.data.frame(x), ...) : 
    no rows to aggregate
  Calls: eval ... aggregate -> aggregate.default -> aggregate.data.frame
  In addition: Warning messages:
  1: In max(D, na.rm = TRUE) :
    no non-missing arguments to max; returning -Inf
  2: In log(max(D, na.rm = TRUE)) : NaNs produced
  Execution halted