-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathget_index_sims.Rd
105 lines (94 loc) · 3.92 KB
/
get_index_sims.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get-index-sims.R
\name{get_index_sims}
\alias{get_index_sims}
\title{Calculate a population index via simulation from the joint precision matrix}
\usage{
get_index_sims(
obj,
level = 0.95,
return_sims = FALSE,
area = rep(1, nrow(obj)),
est_function = stats::median,
area_function = function(x, area) x + log(area),
agg_function = function(x) sum(exp(x))
)
}
\arguments{
\item{obj}{\code{\link[=predict.sdmTMB]{predict.sdmTMB()}} output with \code{nsim > 0}.}
\item{level}{The confidence level.}
\item{return_sims}{Logical. Return simulation draws? The default (\code{FALSE}) is
a quantile summary of those simulation draws.}
\item{area}{A vector of grid cell/polyon areas for each year-grid cell (row
of data) in \code{obj}. Adjust this if cells are not of unit area or not all
the same area (e.g., some cells are partially over land/water). Note that
the area vector is added as \code{log(area)} to the raw values in \code{obj}. In
other words, the function assumes a log link, which typically makes sense.}
\item{est_function}{Function to summarize the estimate (the expected value).
\code{mean()} would be an alternative to \code{median()}.}
\item{area_function}{Function to apply area weighting.
Assuming a log link, the \code{function(x, area) x + log(area)} default makes sense.
If in natural space, \code{function(x, area) x * area} makes sense.}
\item{agg_function}{Function to aggregate samples within each time slice.
Assuming a log link, the \code{function(x) sum(exp(x))} default makes sense.
If in natural space, \code{function(x) sum(x)} makes sense.}
}
\value{
A data frame. If \code{return_sims = FALSE}:
\itemize{
\item name of column (e.g. \code{year}) that was supplied to \code{\link[=sdmTMB]{sdmTMB()}} time argument
\item \code{est}: estimate
\item \code{lwr}: lower confidence interval value
\item \code{upr}: upper confidence interval value
\item \code{log_est}: log estimate
\item \code{se}: standard error on the log estimate
}
If \code{return_sims = TRUE}, samples from the index values in a long-format data frame:
\itemize{
\item name of column (e.g. \code{year}) that was supplied to \code{\link[=sdmTMB]{sdmTMB()}} time argument
\item \code{.value}: sample value
\item \code{.iteration}: sample number
}
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}}
Calculate a population index via simulation from the joint precision matrix.
Compared to \code{\link[=get_index]{get_index()}}, this version can be faster if bias correction was
turned on in \code{\link[=get_index]{get_index()}} while being approximately equivalent. \strong{This is an
experimental function.} This function usually works reasonably well, but we
make no guarantees. It is recommended to use \code{\link[=get_index]{get_index()}} with \code{bias_correct = TRUE} for final inference.
}
\details{
Can also be used to produce an index from a model fit with
\pkg{tmbstan}.
This function does nothing more than summarize and reshape the
matrix of simulation draws into a data frame.
}
\examples{
\donttest{
m <- sdmTMB(density ~ 0 + as.factor(year),
data = pcod_2011, mesh = pcod_mesh_2011, family = tweedie(link = "log"),
time = "year"
)
qcs_grid_2011 <- replicate_df(qcs_grid, "year", unique(pcod_2011$year))
p <- predict(m, newdata = qcs_grid_2011, nsim = 100)
x <- get_index_sims(p)
x_sims <- get_index_sims(p, return_sims = TRUE)
if (require("ggplot2", quietly = TRUE)) {
ggplot(x, aes(year, est, ymin = lwr, ymax = upr)) +
geom_line() +
geom_ribbon(alpha = 0.4)
ggplot(x_sims, aes(as.factor(year), .value)) +
geom_violin()
}
# Demo custom functions if working in natural space:
ind <- get_index_sims(
exp(p),
agg_function = function(x) sum(x),
area_function = function(x, area) x * area
)
}
}
\seealso{
\code{\link[=get_index]{get_index()}}
}