diff options
author | Ken Kellner <ken@kenkellner.com> | 2022-07-15 16:54:39 -0400 |
---|---|---|
committer | Ken Kellner <ken@kenkellner.com> | 2022-07-15 16:54:39 -0400 |
commit | 9048b742319965297c7b95b9ea7bb6ecd3399f2d (patch) | |
tree | 95437978d601af28349883812647058efe4e8cae | |
parent | a1f211529eb5e26f743a3bb08177ad8df231a4f4 (diff) |
Fix simulation with random effects for distsamp and multinomPois, fixes #235
-rw-r--r-- | R/simulate.R | 4 | ||||
-rw-r--r-- | R/unmarkedFit.R | 16 |
2 files changed, 10 insertions, 10 deletions
diff --git a/R/simulate.R b/R/simulate.R index 7868948..7793820 100644 --- a/R/simulate.R +++ b/R/simulate.R @@ -66,7 +66,7 @@ setMethod("simulate", "character", coefs <- check_coefs(coefs, fit, formulas) coefs <- generate_random_effects(coefs, fit, formulas) fit <- replace_estimates(fit, coefs) - ysims <- simulate(fit, nsim) + ysims <- suppressWarnings(simulate(fit, nsim)) umf <- fit@data # fix this umfs <- lapply(ysims, function(x){ @@ -533,5 +533,5 @@ setMethod("simulate_fit", "unmarkedFitGDR", gdistremoval(lambdaformula=formulas$lambda, phiformula=formulas$phi, removalformula=formulas$rem, distanceformula=formulas$dist, data=umf, keyfun=keyfun, output=output, unitsOut=unitsOut, - mixture=mixture, K=K, se=FALSE, control=list(maxit=1)) + mixture=mixture, K=K, se=FALSE, control=list(maxit=1), method='L-BFGS-B') }) diff --git a/R/unmarkedFit.R b/R/unmarkedFit.R index 0a8d107..6557b9d 100644 --- a/R/unmarkedFit.R +++ b/R/unmarkedFit.R @@ -1631,13 +1631,13 @@ setMethod("getP", "unmarkedFitDS", umf <- object@data designMats <- getDesign(umf, formula, na.rm = na.rm) y <- designMats$y - V <- designMats$V + V <- cbind(designMats$V, designMats$Z_det) V.offset <- designMats$V.offset if (is.null(V.offset)) V.offset <- rep(0, nrow(V)) M <- nrow(y) J <- ncol(y) - ppars <- coef(object, type = "det") + ppars <- coef(object, type = "det", fixedOnly=FALSE) db <- umf@dist.breaks w <- diff(db) survey <- umf@survey @@ -1902,13 +1902,13 @@ setMethod("getP", "unmarkedFitMPois", function(object, na.rm = TRUE) umf <- object@data designMats <- getDesign(umf, formula, na.rm = na.rm) y <- designMats$y - V <- designMats$V + V <- as.matrix(cbind(designMats$V, designMats$Z_det)) V.offset <- designMats$V.offset if (is.null(V.offset)) V.offset <- rep(0, nrow(V)) M <- nrow(y) J <- obsNum(umf) #ncol(y) - ppars <- coef(object, type = "det") + ppars <- coef(object, type = "det", fixedOnly=FALSE) p <- plogis(V %*% ppars + V.offset) p <- matrix(p, M, J, byrow = TRUE) pi <- do.call(piFun, list(p = p)) @@ -2063,13 +2063,13 @@ setMethod("simulate", "unmarkedFitDS", w <- diff(db) designMats <- getDesign(umf, formula, na.rm = na.rm) y <- designMats$y - X <- designMats$X + X <- as.matrix(cbind(designMats$X, designMats$Z_state)) X.offset <- designMats$X.offset if (is.null(X.offset)) X.offset <- rep(0, nrow(X)) M <- nrow(y) J <- ncol(y) - lamParms <- coef(object, type = "state") + lamParms <- coef(object, type = "state", fixedOnly=FALSE) lambda <- drop(exp(X %*% lamParms + X.offset)) if(identical(object@output, "density")) { switch(umf@survey, @@ -2342,14 +2342,14 @@ setMethod("simulate", "unmarkedFitMPois", umf <- object@data designMats <- getDesign(umf, formula, na.rm = na.rm) y <- designMats$y - X <- designMats$X + X <- as.matrix(cbind(designMats$X, designMats$Z_state)) X.offset <- designMats$X.offset if (is.null(X.offset)) { X.offset <- rep(0, nrow(X)) } M <- nrow(y) J <- ncol(y) - lamParms <- coef(object, type = "state") + lamParms <- coef(object, type = "state", fixedOnly=FALSE) lam <- as.numeric(exp(X %*% lamParms + X.offset)) lamvec <- rep(lam, each = J) pivec <- as.vector(t(getP(object, na.rm = na.rm))) |