forked from ianfiske/unmarked
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from kenkellner/predict_ranef
Adds predict method for unmarkedRanef class
- Loading branch information
Showing
8 changed files
with
297 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
setGeneric("posteriorSamples", function(object, nsims, ...){ | ||
standardGeneric("posteriorSamples") | ||
}) | ||
|
||
setClass("unmarkedPostSamples", | ||
representation(numSites="numeric", | ||
numPrimary="numeric", | ||
nsims="numeric", | ||
samples="array") | ||
) | ||
|
||
setMethod("posteriorSamples", "unmarkedRanef", function(object, nsims=100, ...) | ||
{ | ||
|
||
N <- dim(object@post)[1] | ||
K <- dim(object@post)[2] | ||
T <- dim(object@post)[3] | ||
|
||
out <- array(NA, c(N, T, nsims)) | ||
|
||
for (n in 1:N){ | ||
for (t in 1:T){ | ||
out[n, t, ] <- sample(0:(K-1), nsims, replace=TRUE, | ||
prob=object@post[n,,t]) | ||
} | ||
} | ||
new("unmarkedPostSamples", numSites=N, numPrimary=T, nsims=nsims, | ||
samples=out) | ||
|
||
}) | ||
|
||
setMethod("posteriorSamples", "unmarkedFit", function(object, nsims=100, ...) | ||
{ | ||
ran <- ranef(object) | ||
posteriorSamples(ran, nsims) | ||
}) | ||
|
||
setMethod("show", "unmarkedPostSamples", function(object) | ||
{ | ||
|
||
#tdim <- character(0) | ||
#if(object@numPrimary>1){ | ||
tdim <- paste0("x ", object@numPrimary, " primary periods") | ||
#} | ||
|
||
cat("Posterior samples from unmarked model\n") | ||
cat(paste(object@numSites, "sites", tdim, "x", object@nsims, "sims\n")) | ||
cat(paste0("Showing first 5 sites and first 3 simulations\n", | ||
"To see all samples, use print()\n")) | ||
|
||
print(object@samples[1:5,,1:3]) | ||
|
||
}) | ||
|
||
print.unmarkedPostSamples <- function(x, ...){ | ||
print(x@samples) | ||
} | ||
|
||
setMethod("[", c("unmarkedPostSamples","ANY","ANY","ANY"), | ||
function(x, i, j, k) | ||
{ | ||
x@samples[i,j,k] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
\name{posteriorSamples} | ||
\alias{posteriorSamples} | ||
\alias{posteriorSamples-methods} | ||
\alias{posteriorSamples,unmarkedRanef-method} | ||
\alias{posteriorSamples,unmarkedFit-method} | ||
\alias{unmarkedPostSamples-class} | ||
\alias{show,unmarkedPostSamples-method} | ||
\alias{[,unmarkedPostSamples,ANY,ANY,ANY-method} | ||
|
||
\title{Draw samples from the posterior predictive distribution} | ||
|
||
\description{ | ||
Draw samples from the empirical Bayes posterior predictive distribution | ||
derived from unmarked models or ranef objects | ||
} | ||
|
||
\usage{ | ||
\S4method{posteriorSamples}{unmarkedRanef}(object, nsims=100, ...) | ||
\S4method{posteriorSamples}{unmarkedFit}(object, nsims=100, ...) | ||
} | ||
|
||
\arguments{ | ||
\item{object}{An object inheriting class \code{unmarkedRanef} or | ||
\code{unmarkedFit}} | ||
\item{nsims}{Number of draws to make from the posterior predictive distribution} | ||
\item{...}{Other arguments} | ||
} | ||
|
||
\value{\code{unmarkedPostSamples} object containing the draws from the | ||
posterior predictive distribution. The draws are in the \code{@samples} slot. | ||
} | ||
|
||
\author{Ken Kellner \email{contact@kenkellner.com}} | ||
|
||
\seealso{ | ||
\code{\link{ranef}}, | ||
\code{\link{predict}} | ||
} | ||
|
||
\examples{ | ||
|
||
# Simulate data under N-mixture model | ||
set.seed(4564) | ||
R <- 20 | ||
J <- 5 | ||
N <- rpois(R, 10) | ||
y <- matrix(NA, R, J) | ||
y[] <- rbinom(R*J, N, 0.5) | ||
|
||
# Fit model | ||
umf <- unmarkedFramePCount(y=y) | ||
fm <- pcount(~1 ~1, umf, K=50) | ||
|
||
# Estimates of conditional abundance distribution at each site | ||
(re <- ranef(fm)) | ||
|
||
#Draw from the posterior predictive distribution | ||
(ppd <- posteriorSamples(re, nsims=100)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters