% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RcppExports.R
\name{f_projection3}
\alias{f_projection3}
\title{Project Function-based Matrix Projection Model}
\usage{
f_projection3(
  format,
  prebreeding = TRUE,
  start_age = NA_integer_,
  last_age = NA_integer_,
  fecage_min = NA_integer_,
  fecage_max = NA_integer_,
  cont = TRUE,
  stochastic = FALSE,
  standardize = FALSE,
  growthonly = TRUE,
  repvalue = FALSE,
  integeronly = FALSE,
  substoch = 0L,
  ipm_cdf = TRUE,
  nreps = 1L,
  times = 10000L,
  repmod = 1,
  exp_tol = 700,
  theta_tol = 1e+08,
  random_inda = FALSE,
  random_indb = FALSE,
  random_indc = FALSE,
  err_check = FALSE,
  quiet = FALSE,
  data = NULL,
  stageframe = NULL,
  supplement = NULL,
  repmatrix = NULL,
  overwrite = NULL,
  modelsuite = NULL,
  paramnames = NULL,
  year = NULL,
  patch = NULL,
  sp_density = NULL,
  ind_terms = NULL,
  ann_terms = NULL,
  dev_terms = NULL,
  surv_model = NULL,
  obs_model = NULL,
  size_model = NULL,
  sizeb_model = NULL,
  sizec_model = NULL,
  repst_model = NULL,
  fec_model = NULL,
  jsurv_model = NULL,
  jobs_model = NULL,
  jsize_model = NULL,
  jsizeb_model = NULL,
  jsizec_model = NULL,
  jrepst_model = NULL,
  jmatst_model = NULL,
  start_vec = NULL,
  start_frame = NULL,
  tweights = NULL,
  density = NULL,
  density_vr = NULL,
  stage_weights = NULL,
  sparse = NULL
)
}
\arguments{
\item{format}{An integer indicating the kind of function-based MPM to create.
Possible choices include: \code{1}, Ehrlen-format historical MPM; \code{2},
deVries-format historical MPM; \code{3}, ahistorical MPM; \code{4},
age-by-stage MPM; and \code{5}, Leslie (age-based) MPM.}

\item{prebreeding}{A logical value indicating whether the life history model
is a pre-breeding model. Only used in Leslie and age-by-stage MPMs. Defaults
to \code{TRUE}.}

\item{start_age}{The age from which to start the matrix. Defaults to
\code{NA}, in which case age \code{1} is used if \code{prebreeding = TRUE},
and age \code{0} is used if \code{prebreeding = FALSE}.}

\item{last_age}{The final age to use in the matrix. Defaults to \code{NA}, in
which case the highest age in the dataset is used.}

\item{fecage_min}{The minimum age at which reproduction is possible. Defaults
to \code{NA}, which is interpreted to mean that fecundity should be assessed
starting in the minimum age observed in the dataset.}

\item{fecage_max}{The maximum age at which reproduction is possible. Defaults
to \code{NA}, which is interpreted to mean that fecundity should be assessed
until the final observed age.}

\item{cont}{A logical value designating whether to allow continued survival
of individuals past the final age noted in the stageframe, using the 
demographic characteristics of the final age. Defaults to \code{TRUE}.}

\item{stochastic}{A logical value denoting whether to conduct a stochastic
projection or a deterministic / cyclical projection.}

\item{standardize}{A logical value denoting whether to re-standardize the
population size to \code{1.0} at each occasion. Used in density-independent
simulations in which it is more important to know the general trend in
population growth than the explicit growth rate. Defaults to \code{FALSE}.}

\item{growthonly}{A logical value indicating whether to produce only the
projected population size at each occasion (\code{TRUE}), or also to produce
vectors showing the stage distribution at each occasion (\code{FALSE}).
Defaults to \code{TRUE}.}

\item{repvalue}{A logical value indicating whether to calculate reproductive
value vectors at each time step. Can only be set to \code{TRUE} if 
\code{growthonly = FALSE}. Setting to \code{TRUE} may dramatically increase
the duration of calculations. Defaults to \code{FALSE}.}

\item{integeronly}{A logical value indicating whether to round the number of
individuals projected in each stage at each occasion to the nearest
integer. Defaults to \code{FALSE}.}

\item{substoch}{An integer value indicating whether to force survival-
transition matrices to be substochastic in density dependent and density
independent simulations. Defaults to \code{0}, which does not enforce
substochasticity. Alternatively, \code{1} forces all survival-transition
elements to range from 0.0 to 1.0, and forces fecundity to be non-negative;
and \code{2} forces all column rows in the survival-transition matrices to
total no more than 1.0, in addition to the actions outlined for option
\code{1}. Both settings \code{1} and \code{2} change negative fecundity
elements to \code{0.0}.}

\item{ipm_cdf}{A logical value indicating whether to estimate size
transitions using the cumulative density function in cases with continuous
distributions. Defaults to \code{TRUE}, with the midpoint method used if
\code{FALSE}.}

\item{nreps}{The number of replicate projections. Defaults to \code{1}.}

\item{times}{Number of occasions to iterate per replicate. Defaults to
\code{10000}.}

\item{repmod}{A scalar multiplier of fecundity. Defaults to \code{1}.}

\item{exp_tol}{A numeric value used to indicate a maximum value to set
exponents to in the core kernel to prevent numerical overflow. Defaults to
\code{700}.}

\item{theta_tol}{A numeric value used to indicate a maximum value to theta as
used in the negative binomial probability density kernel. Defaults to
\code{100000000}, but can be reset to other values during error checking.}

\item{random_inda}{A logical value denoting whether to treat individual
covariate \code{a} as a random, categorical variable. Otherwise is treated as
a fixed, numeric variable. Defaults to \code{FALSE}.}

\item{random_indb}{A logical value denoting whether to treat individual
covariate \code{b} as a random, categorical variable. Otherwise is treated as
a fixed, numeric variable. Defaults to \code{FALSE}.}

\item{random_indc}{A logical value denoting whether to treat individual
covariate \code{c} as a random, categorical variable. Otherwise is treated as
a fixed, numeric variable. Defaults to \code{FALSE}.}

\item{err_check}{A logical value indicating whether to append extra output
for debugging purposes. Defaults to \code{FALSE}.}

\item{quiet}{A logical value indicating whether warning messages should be
suppressed. Defaults to \code{FALSE}.}

\item{data}{The historical vertical demographic data frame used to estimate
vital rates (class \code{hfvdata}), which is required to initialize times and
patches properly. Variable names should correspond to the naming conventions
in \code{\link{verticalize3}()} and \code{\link{historicalize3}()}. If
attempting bootstrapped projection, then an object of class \code{hfvlist}
is required.}

\item{stageframe}{An object of class \code{stageframe}. These objects are
generated by function \code{\link{sf_create}()}, and include information on
the size, observation status, propagule status, reproduction status,
immaturity status, maturity status, stage group, size bin widths, and other
key characteristics of each ahistorical stage. Required for all MPM formats
except Leslie MPMs.}

\item{supplement}{An optional data frame of class \code{lefkoSD} that
provides supplemental data that should be incorporated into the MPM. Three
kinds of data may be integrated this way: transitions to be estimated via the
use of proxy transitions, transition overwrites from the literature or
supplemental studies, and transition multipliers for survival and fecundity.
This data frame should be produced using the \code{\link{supplemental}()}
function. Can be used in place of or in addition to an overwrite table (see 
\code{overwrite} below) and a reproduction matrix (see \code{repmatrix}
below).}

\item{repmatrix}{An optional reproduction matrix. This matrix is composed
mostly of \code{0}s, with non-zero entries acting as element identifiers and
multipliers for fecundity (with \code{1} equaling full fecundity). If left
blank, and no \code{supplement} is provided, then \code{flefko3()} will
assume that all stages marked as reproductive produce offspring at 1x that of
estimated fecundity, and that offspring production will yield the first stage
noted as propagule or immature. May be the dimensions of either a historical
or an ahistorical matrix. If the latter, then all stages will be used in
occasion \emph{t}-1 for each suggested ahistorical transition.}

\item{overwrite}{An optional data frame developed with the
\code{\link{overwrite}()} function describing transitions to be overwritten
either with given values or with other estimated transitions. Note that this
function supplements overwrite data provided in \code{supplement}.}

\item{modelsuite}{A \code{lefkoMod} object, at minimum with all required
best-fit vital rate models and a \code{paramnames} data frame, and following
the naming conventions used in this package. If given, then
\code{surv_model}, \code{obs_model}, \code{size_model}, \code{sizeb_model},
\code{sizec_model}, \code{repst_model}, \code{fec_model}, \code{jsurv_model},
\code{jobs_model}, \code{jsize_model}, \code{jsizeb_model},
\code{jsizec_model}, \code{jrepst_model}, \code{jmatst_model},
\code{paramnames}, \code{yearcol}, and \code{patchcol} are not required.
Alternatively, an object of class \code{vrm_input}, serving the same role.
Although this is optional input, it is recommended, and without it separate
vital rate model inputs (named \code{XX_model}) are required. If conducting
bootstrapped projection, then an object of \code{lefkoModList} is required.}

\item{paramnames}{A data frame with three columns, the first describing all
terms used in linear modeling, the second (must be called \code{mainparams})
giving the general model terms that will be used in matrix creation, and the
third showing the equivalent terms used in modeling (must be named
\code{modelparams}). Function \code{\link{create_pm}()} can be used to
create a skeleton \code{paramnames} object, which can then be edited. Only
required if \code{modelsuite} is not supplied.}

\item{year}{Either a single integer value corresponding to the year to
project, or a vector of \code{times} elements with the year to use at each
time step. Defaults to \code{NA}, in which the first year in the set of years
in the dataset is projected. If a vector shorter than \code{times} is
supplied, then this vector will be cycled.}

\item{patch}{A value of \code{NA}, a single string value corresponding to the
patch to project, or a vector of \code{times} elements with the patch to use
at each time step. If a vector shorter than \code{times} is supplied, then
this vector will be cycled. Note that this function currently does not
handle multiple projections for different patches in the same run.}

\item{sp_density}{Either a single numeric value of spatial density to use in
vital rate models in all time steps, or a vector of \code{times} elements of
such numeric values. Defaults to \code{NA}.}

\item{ind_terms}{An optional data frame with 3 columns and \code{times} rows
giving the values of individual covariates a, b, and c, respectively, for
each projected time. Unused terms must be set to \code{0} (use of \code{NA}
will produce errors.)}

\item{ann_terms}{An optional data frame with 3 columns and \code{times} rows
giving the values of annual covariates a, b, and c, respectively, for each
projected time. Unused terms must be set to \code{0} (use of \code{NA} will
produce errors.)}

\item{dev_terms}{An optional data frame with 14 columns and \code{times}
rows showing the values of the deviation terms to be added to each linear
vital rate. The column order should be: 1: survival, 2: observation, 3:
primary size, 4: secondary size, 5: tertiary size, 6: reproduction, 7:
fecundity, 8: juvenile survival, 9: juvenile observation, 10: juvenile
primary size, 11: juvenile secondary size, 12: juvenile tertiary size, 13:
juvenile reproduction, and 14: juvenile maturity transition.  Unused terms
must be set to \code{0} (use of \code{NA} will produce errors.)}

\item{surv_model}{A linear model predicting survival probability. This can 
be a model of class \code{glm} or \code{glmer}, and requires a predicted
binomial variable under a logit link. Ignored if \code{modelsuite} is
provided. This model must have been developed in a modeling exercise testing
the impacts of occasions \emph{t} and \emph{t}-1.}

\item{obs_model}{A linear model predicting sprouting or observation
probability. This can be a model of class \code{glm} or \code{glmer}, and
requires a predicted binomial variable under a logit link. Ignored if
\code{modelsuite} is provided. This model must have been developed in a
modeling exercise testing the impacts of occasions \emph{t} and \emph{t}-1.}

\item{size_model}{A linear model predicting primary size. This can be a model
of class \code{glm}, \code{glmer}, \code{glmmTMB}, \code{zeroinfl},
\code{vglm}, \code{lm}, or \code{lmer}. Ignored if \code{modelsuite} is
provided. This model must have been developed in a modeling exercise testing
the impacts of occasions \emph{t} and \emph{t}-1.}

\item{sizeb_model}{A linear model predicting secondary size. This can be a
model of class \code{glm}, \code{glmer}, \code{glmmTMB}, \code{zeroinfl},
\code{vglm}, \code{lm}, or \code{lmer}. Ignored if \code{modelsuite} is
provided. This model must have been developed in a modeling exercise testing
the impacts of occasions \emph{t} and \emph{t}-1.}

\item{sizec_model}{A linear model predicting tertiary size. This can be a
model of class \code{glm}, \code{glmer}, \code{glmmTMB}, \code{zeroinfl},
\code{vglm}, \code{lm}, or \code{lmer}. Ignored if \code{modelsuite} is
provided. This model must have been developed in a modeling exercise testing
the impacts of occasions \emph{t} and \emph{t}-1.}

\item{repst_model}{A linear model predicting reproduction probability. This 
can be a model of class \code{glm} or \code{glmer}, and requires a predicted
binomial variable under a logit link. Ignored if \code{modelsuite} is
provided. This model must have been developed in a modeling exercise testing
the impacts of occasions \emph{t} and \emph{t}-1.}

\item{fec_model}{A linear model predicting fecundity. This can be a model of
class \code{glm}, \code{glmer}, \code{glmmTMB}, \code{zeroinfl}, \code{vglm},
\code{lm}, or \code{lmer}. Ignored if \code{modelsuite} is provided. This
model must have been developed in a modeling exercise testing the impacts of
occasions \emph{t} and \emph{t}-1.}

\item{jsurv_model}{A linear model predicting juvenile survival probability.
This can be a model of class \code{glm} or \code{glmer}, and requires a
predicted binomial variable under a logit link. Ignored if \code{modelsuite}
is provided. This model must have been developed in a modeling exercise
testing the impacts of occasions \emph{t} and \emph{t}-1.}

\item{jobs_model}{A linear model predicting juvenile sprouting or observation
probability. This can be a model of class \code{glm} or \code{glmer}, and
requires a predicted binomial variable under a logit link. Ignored if
\code{modelsuite} is provided. This model must have been developed in a
modeling exercise testing the impacts of occasions \emph{t} and \emph{t}-1.}

\item{jsize_model}{A linear model predicting juvenile primary size. This
can be a model of class \code{glm}, \code{glmer}, \code{glmmTMB},
\code{zeroinfl}, \code{vglm}, \code{lm}, or \code{lmer}. Ignored if
\code{modelsuite} is provided. This model must have been developed in a
modeling exercise testing the impacts of occasions \emph{t} and \emph{t}-1.}

\item{jsizeb_model}{A linear model predicting juvenile secondary size. This
can be a model of class \code{glm}, \code{glmer}, \code{glmmTMB},
\code{zeroinfl}, \code{vglm}, \code{lm}, or \code{lmer}. Ignored if
\code{modelsuite} is provided. This model must have been developed in a
modeling exercise testing the impacts of occasions \emph{t} and \emph{t}-1.}

\item{jsizec_model}{A linear model predicting juvenile tertiary size. This
can be a model of class \code{glm}, \code{glmer}, \code{glmmTMB},
\code{zeroinfl}, \code{vglm}, \code{lm}, or \code{lmer}. Ignored if
\code{modelsuite} is provided. This model must have been developed in a
modeling exercise testing the impacts of occasions \emph{t} and \emph{t}-1.}

\item{jrepst_model}{A linear model predicting reproduction probability of a 
mature individual that was immature in time \emph{t}. This can be a model
of class \code{glm} or \code{glmer}, and requires a predicted binomial
variable under a logit link. Ignored if \code{modelsuite} is provided. This
model must have been developed in a modeling exercise testing the impacts of
occasions \emph{t} and \emph{t}-1.}

\item{jmatst_model}{A linear model predicting maturity probability of an 
individual that was immature in time \emph{t}. This can be a model of class
\code{glm} or \code{glmer}, and requires a predicted binomial variable under
a logit link. Ignored if \code{modelsuite} is provided. This model must have
been developed in a modeling exercise testing the impacts of occasions
\emph{t} and \emph{t}-1.}

\item{start_vec}{An optional numeric vector denoting the starting stage
distribution for the projection. Defaults to a single individual of each
stage.}

\item{start_frame}{An optional data frame characterizing stages, age-stages,
or stage-pairs that should be set to non-zero values in the starting vector,
and what those values should be. Can only be used with \code{lefkoMat}
objects.}

\item{tweights}{An optional numeric vector or matrix denoting the
probabilities of choosing each matrix in a stochastic projection. If a
matrix is input, then a first-order Markovian environment is assumed, in
which the probability of choosing a specific annual matrix depends on which
annual matrix is currently chosen. If a vector is input, then the choice of
annual matrix is assumed to be independent of the current matrix. Defaults
to equal weighting among matrices.}

\item{density}{An optional data frame describing the matrix elements that
will be subject to density dependence, and the exact kind of density
dependence that they will be subject to, or a list of such objects. The data
frame used should be an object of class \code{lefkoDens}, which is the
output from function \code{\link{density_input}()}.}

\item{density_vr}{An optional data frame describing density dependence
relationships in vital rates, if such relationships are to be assumed. The
data frame must be of class \code{lefkoDensVR}, which is the output from the
function \code{\link{density_vr}()}.}

\item{stage_weights}{An optional object of class \code{lefkoEq} giving the
degree to which individuals in each stage are equivalent to one another, or
a list of such objects. May also be a numeric vector (or a list thereof), in
which case the vector must have the same number of elements as the number of
rows in the associated MPM, with each element giving the effect of an
individual of that age, stage, age-stage, or stage-pair, depending on
whether the MPM is age-based, ahistorical stage-based, age-by-stage, or
historical stage-based, respectively.}

\item{sparse}{A text string indicating whether to use sparse matrix encoding
(\code{"yes"}) or dense matrix encoding (\code{"no"}). Defaults to
\code{"auto"}, in which case sparse matrix encoding is used with square
matrices with at least 50 rows and no more than 50\% of elements with values
greater than zero. Can also be entered as a logical value if forced sparse
(\code{TRUE}) or forced dense (\code{FALSE}) projection is desired.}
}
\value{
If running a single (including replicated) projection, then the
output is a list of class \code{lefkoProj}, which always includes the first
three elements of the following, and also includes the remaining elements
below when a \code{lefkoMat} object is used as input:
\item{projection}{A list of lists of matrices showing the total number of
individuals per stage per occasion. The first list corresponds to each
pop-patch followed by each population (this top-level list is a single
element in \code{f_projection3()}). The inner list corresponds to
replicates within each pop-patch or population.}
\item{stage_dist}{A list of lists of the actual stage distribution in each
occasion in each replicate in each pop-patch or population. The list
structure is the same as in \code{\link{projection3}()}.}
\item{rep_value}{A list of lists of the actual reproductive value in each
occasion in each replicate in each pop-patch or population. The list
structure is the same as in \code{\link{projection3}()}.}
\item{pop_size}{A list of matrices showing the total population size in each
occasion per replicate (row within matrix) per pop-patch or population
(list element). Only a single pop-patch or population is allowed in
\code{f_projection3()}.}
\item{labels}{A data frame showing the order of populations and patches in
item \code{projection}.}
\item{ahstages}{The original stageframe used in the study.}
\item{hstages}{A data frame showing the order of historical stage pairs.}
\item{agestages}{A data frame showing the order of age-stage pairs.}
\item{labels}{A short data frame indicating the population (always \code{1}),
and patch (either the numeric index of the single chosen patch, or \code{1}
in all other cases).}
\item{control}{A short vector indicating the number of replicates and the
number of occasions projected per replicate.}
\item{density}{The data frame input under the density option. Only provided
if input by the user.}
\item{density_vr}{The data frame input under the density_vr option. Only
provided if input by the user.}

If running bootstrapped projections (i.e. using \code{hfvlist} and
\code{lefkoModList} input), then will output an object of class
\code{lefkoProjList}, which is just a simple list of \code{lefkoProj}
objects.
}
\description{
Function \code{f_projection3()} develops and projects function-based matrix
models. Unlike \code{\link{projection3}()}, which uses matrices provided as
input via already created \code{lefkoMat} objects, function
\code{f_projection3()} creates matrices at each time step from vital rate
models and parameter inputs provided. Projections may be stochastic or not,
and may be density dependent in either case. Also handles replication.
}
\section{Notes}{

Population projection can be a very time-consuming activity, and it is most
time-consuming when matrices need to be created at each time step. We have
created this function to work as quickly as possible, but some options will
slow analysis. First, the \code{err_check} option should always be set to
\code{FALSE}, as the added created output will not only slow the analysis
down but also potentially crash the memory if matrices are large enough.
Second, the \code{repvalue} option should be set to \code{FALSE} unless
reproductive values are genuinely needed, since this step requires
concurrent backward projection and so in some cases may double total run
time. Next, if the only needed data is the total population size and
age/stage structure at each time step, then setting \code{growthonly = TRUE}
will yield the quickest possible run time. Finally, the default behavior of
the function is to round down fractional values of individuals, and to stop
running projections (replicates) when the population drops to 0. Setting
\code{integeronly = FALSE} will have the impact of increasing runtime,
potentially dramatically, since the population can reach a point in which
the population size is extremely small but not equal to 0.

Projections with large matrices may take a long time to run. To assess the
likely running time, try using a low number of iterations on a single
replicate first. For example, set \code{nreps = 1} and \code{times = 10} for
a trial run. If a full run is set and takes too long, press the STOP button
in RStudio to cancel the projection run, or click \code{esc}.

This function currently allows three forms of density dependence. The first
modifies matrix elements on the basis of the input provided in option
\code{density}, and so alters matrix elements once the matrix has already
been created. The second form alters the vital rates estimated, and so
estimates matrix elements using vital rate values already modified by
density. This second form uses the input provided in option
\code{density_vr}. These two forms of density dependence utilize the
projected population size at some time to make these alterations. The third
form of density dependence also alters the vital rates, but using spatial
density supplied via option \code{sp_density} and only in vital rates in
which spatial density is included as a fixed factor in the associated
vital rate model.

When running density dependent simulations involving user-set exponents,
such as the beta term in the Ricker function and both the alpha and beta
terms in the Usher function, values above or below the computer limits may
cause unpredictable behavior. Noted odd behavior includes sudden shifts in
population size to negative values. This function produces warnings when
such values are used, and the values used for warnings may be reset with the
\code{exp_tol} term. In addition, this function resets beta values for the
Ricker function automatically to positive or negative \code{exp_tol}, giving
a warning when doing so.

Consistently positive population growth can quickly lead to population size
numbers larger than can be handled computationally. In that circumstance, a
continuously rising population size will suddenly become \code{NaN} for the
remainder of the projection.

This function does not reduce the dimensionality of matrices developed for
projection.

Speed can sometimes be increased by shifting from automatic sparse matrix
determination to forced dense or sparse matrix projection. This will most
likely occur when matrices have between 30 and 300 rows and columns.
Defaults work best when matrices are very small and dense, or very large and
sparse.

Some issues may arise in first-order Markovian stochastic projections if
the \code{year} argument is used. Use the matrix input in the
\code{tweights} argument to eliminate any years from consideration that are
not needed.
}

\examples{
\donttest{
data(lathyrus)

sizevector <- c(0, 4.6, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8,
  9)
stagevector <- c("Sd", "Sdl", "Dorm", "Sz1nr", "Sz2nr", "Sz3nr", "Sz4nr",
  "Sz5nr", "Sz6nr", "Sz7nr", "Sz8nr", "Sz9nr", "Sz1r", "Sz2r", "Sz3r", 
  "Sz4r", "Sz5r", "Sz6r", "Sz7r", "Sz8r", "Sz9r")
repvector <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1)
obsvector <- c(0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
matvector <- c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
immvector <- c(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
propvector <- c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0)
indataset <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
binvec <- c(0, 4.6, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
  0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5)

lathframeln <- sf_create(sizes = sizevector, stagenames = stagevector, 
  repstatus = repvector, obsstatus = obsvector, matstatus = matvector, 
  immstatus = immvector, indataset = indataset, binhalfwidth = binvec, 
  propstatus = propvector)

lathvertln <- verticalize3(lathyrus, noyears = 4, firstyear = 1988,
  patchidcol = "SUBPLOT", individcol = "GENET", blocksize = 9, 
  juvcol = "Seedling1988", sizeacol = "lnVol88", repstracol = "Intactseed88",
  fecacol = "Intactseed88", deadacol = "Dead1988", 
  nonobsacol = "Dormant1988", stageassign = lathframeln, stagesize = "sizea",
  censorcol = "Missing1988", censorkeep = NA, NAas0 = TRUE, censor = TRUE)

lathvertln$feca2 <- round(lathvertln$feca2)
lathvertln$feca1 <- round(lathvertln$feca1)
lathvertln$feca3 <- round(lathvertln$feca3)

lathvertln_adults <- subset(lathvertln, stage2index > 2)
surv_model <- glm(alive3 ~ sizea2 + sizea1 + as.factor(patchid) +
  as.factor(year2), data = lathvertln_adults, family = "binomial")

obs_data <- subset(lathvertln_adults, alive3 == 1)
obs_model <- glm(obsstatus3 ~ as.factor(patchid), data = obs_data,
  family = "binomial")

size_data <- subset(obs_data, obsstatus3 == 1)
siz_model <- lm(sizea3 ~ sizea2 + sizea1 + repstatus1 + as.factor(patchid) +
  as.factor(year2), data = size_data)

reps_model <- glm(repstatus3 ~ sizea2 + sizea1 + as.factor(patchid) +
  as.factor(year2), data = size_data, family = "binomial")

fec_data <- subset(lathvertln_adults, repstatus2 == 1)
fec_model <- glm(feca2 ~ sizea2 + sizea1 + repstatus1 + as.factor(patchid),
  data = fec_data, family = "poisson")

lathvertln_juvs <- subset(lathvertln, stage2index < 3)
jsurv_model <- glm(alive3 ~ as.factor(patchid), data = lathvertln_juvs,
  family = "binomial")

jobs_data <- subset(lathvertln_juvs, alive3 == 1)
jobs_model <- glm(obsstatus3 ~ 1, family = "binomial", data = jobs_data)

jsize_data <- subset(jobs_data, obsstatus3 == 1)
jsiz_model <- lm(sizea3 ~ as.factor(year2), data = jsize_data)

jrepst_model <- 0
jmatst_model <- 1

mod_params <- create_pm(name_terms = TRUE)
mod_params$modelparams[3] <- "patchid"
mod_params$modelparams[4] <- "alive3"
mod_params$modelparams[5] <- "obsstatus3"
mod_params$modelparams[6] <- "sizea3"
mod_params$modelparams[9] <- "repstatus3"
mod_params$modelparams[11] <- "feca2"
mod_params$modelparams[12] <- "sizea2"
mod_params$modelparams[13] <- "sizea1"
mod_params$modelparams[18] <- "repstatus2"
mod_params$modelparams[19] <- "repstatus1"

lathsupp3 <- supplemental(stage3 = c("Sd", "Sd", "Sdl", "Sdl", "mat", "Sd", "Sdl"), 
  stage2 = c("Sd", "Sd", "Sd", "Sd", "Sdl", "rep", "rep"),
  stage1 = c("Sd", "rep", "Sd", "rep", "Sd", "mat", "mat"),
  eststage3 = c(NA, NA, NA, NA, "mat", NA, NA),
  eststage2 = c(NA, NA, NA, NA, "Sdl", NA, NA),
  eststage1 = c(NA, NA, NA, NA, "Sdl", NA, NA),
  givenrate = c(0.345, 0.345, 0.054, 0.054, NA, NA, NA),
  multiplier = c(NA, NA, NA, NA, NA, 0.345, 0.054),
  type = c(1, 1, 1, 1, 1, 3, 3), type_t12 = c(1, 2, 1, 2, 1, 1, 1),
  stageframe = lathframeln, historical = TRUE)

# While we do not use MPMs to initialize f_projections3(), we do use MPMs to
# initialize functions start_input() and density_input().
lathmat3ln <- flefko3(year = "all", patch = "all", data = lathvertln,
  stageframe = lathframeln, supplement = lathsupp3, paramnames = mod_params,
  surv_model = surv_model, obs_model = obs_model, size_model = siz_model,
  repst_model = reps_model, fec_model = fec_model, jsurv_model = jsurv_model,
  jobs_model = jobs_model, jsize_model = jsiz_model,
  jrepst_model = jrepst_model, jmatst_model = jmatst_model, reduce = FALSE)

e3m_sv <- start_input(lathmat3ln, stage2 = "Sd", stage1 = "Sd", value = 1000)

dyn7 <- c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
  FALSE, FALSE, FALSE, FALSE, FALSE)
dst7 <- c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
dal7 <- c(0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
dbe7 <- c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

e3d_vr <- density_vr(density_yn = dyn7, style = dst7, alpha = dal7,
  beta = dbe7)

trial7_dvr_1 <- f_projection3(format = 1, data = lathvertln, supplement = lathsupp3,
  paramnames = mod_params, stageframe = lathframeln, nreps = 2,
  surv_model = surv_model, obs_model = obs_model, size_model = siz_model,
  repst_model = reps_model, fec_model = fec_model, jsurv_model = jsurv_model,
  jobs_model = jobs_model, jsize_model = jsiz_model,
  jrepst_model = jrepst_model, jmatst_model = jmatst_model,
  times = 100, stochastic = TRUE, standardize = FALSE, growthonly = TRUE,
  substoch = 0, sp_density = 0, start_frame = e3m_sv, density_vr = e3d_vr)
}

}
\seealso{
\code{\link{start_input}()}

\code{\link{density_input}()}

\code{\link{density_vr}()}

\code{\link{projection3}()}

\code{\link{flefko3}()}

\code{\link{flefko2}()}

\code{\link{aflefko2}()}

\code{\link{fleslie}()}

\code{\link{append_lP}()}

\code{\link{summary.lefkoProj}()}

\code{\link{plot.lefkoProj}()}
}
