rbchan / unmarked

R package for hierarchical models in ecological research
https://rbchan.github.io/unmarked/
36 stars 25 forks source link

Parboot now works with passed formula object #113

Closed adamdsmith closed 6 years ago

adamdsmith commented 6 years ago

Includes test, as well as fix for way seed was specified in parboot...

library(unmarked)
R <- 20
J <- 5
z <- rbinom(R, 1, 0.6)
y <- matrix(NA, R, J)
y[] <- rbinom(R*J, 1, z*0.7)
x1 <- rnorm(R)
x2 <- y
x2[] <- rnorm(R*J)
x2[1,] <- NA
x2[3,1] <- NA
umf <- unmarkedFrameOccu(y=y, siteCovs=data.frame(x1=x1),
                         obsCovs=list(x2=x2))

fitstats <- function(fm) {
  observed <- getY(fm@data)
  expected <- fitted(fm)
  resids <- residuals(fm)
  sse <- sum(resids^2,na.rm=TRUE)
  chisq <- sum((observed - expected)^2 / expected,na.rm=TRUE)
  freeTuke <- sum((sqrt(observed) - sqrt(expected))^2,na.rm=TRUE)
  out <- c(SSE=sse, Chisq=chisq, freemanTukey=freeTuke)
  return(out)
}
x.1 <- "x1"; x.2 <- "x2"
frm.obj <- as.formula(paste("~", x.2, "~", x.1))
fm1 <- occu(~x2 ~x1, umf)
fm2 <- occu(frm.obj, umf)
gof <- parboot(fm1, fitstats, nsim = 100, seed = 6546)
gof2 <- parboot(fm2, fitstats, nsim = 100, seed = 6546)
identical(gof@t.star, gof2@t.star)