View source: R/lifetable_main.R
| lifeTable_analyze | R Documentation |
Non-interactive, fully parameter-driven entry point for the
age-stage, two-sex life table analysis. It (1) builds a
life_table object from column vectors of an already loaded
data frame (e.g. after data <- read.csv("XXX.csv"), or accepts
a ready life_table object), (2) computes the life table
parameters and (3) optionally draws the age-stage survival curves
with customisable title, axis titles and legend labels, optionally
written to disk as png when plot_file is supplied. Tabular
export is handled separately by lifeTable_export.
lifeTable_analyze(
lt = NULL,
stages = NULL,
adult_days = NULL,
sex = NULL,
oviposition = NULL,
stage_names = NULL,
file_name = "life_table",
check = TRUE,
fecundity = TRUE,
bootstrap = FALSE,
B = 1e+05,
seed = NULL,
plot = FALSE,
title = NULL,
x_title = "Age(days)",
y_title = "Age-Stage Survival Rate(Sxj)",
legend_labels = NULL,
dpi = 300,
plot_file = NULL,
plot_width = 12,
plot_height = 8,
plot_units = "cm",
plot_res = 300
)
lt |
Optional; an existing |
stages, adult_days, sex, oviposition, stage_names, file_name, check |
Passed to |
fecundity |
Logical; whether to compute the reproduction-related
parameters (F, F_xj, m_x, R0, r, lambda, T). |
bootstrap |
Logical; whether to estimate the standard errors and
percentile confidence intervals of all scalar parameters with the
bootstrap technique of TWOSEX-MSChart via
|
B |
Integer; number of bootstrap replicates (only used when
|
seed |
Integer; seed of the bootstrap random number generator
(only used when |
plot |
Logical; whether to draw the age-stage survival curves
(default |
title |
Character; plot title. |
x_title, y_title |
Character; axis titles. Defaults
|
legend_labels |
Character vector; legend labels, one per stage
(immature stages + Female + Male), e.g.
|
dpi |
Numeric; resolution used for scaling the text of the plot (default 300). |
plot_file |
Optional png path: when supplied together with
|
plot_width, plot_height, plot_units, plot_res |
Physical size and
resolution of the exported png (only used when |
A list with components lt (the life_table
object), results (the list returned by
lifeTable_calculate_all; additionally containing
boot, the lifeTable_bootstrap result, when
bootstrap = TRUE), plot (the ggplot object when
plot = TRUE, otherwise NULL) and plot_file
(the png path when plot_file was supplied, otherwise
NULL).
lifeTable_build,
lifeTable_calculate_all, lifeTable_bootstrap,
lifeTable_plot, lifeTable_export
## The example raw data shipped with the package (the same layout as
## the csv template: ID + immature stage columns + Adult + gender +
## one column per oviposition day of the females)
f <- system.file("extdata", "lifetable_example.csv", package = "insectecol")
## ^^ change "lifeTable" to the actual package name
d <- read.csv(f)
names(d) # with check.names = TRUE (default) the names become
# ID, Egg, X1st.instar, X2nd.instar, ..., Prepupa, Pupa,
# Adult, gender, ...
## --- way 1: pass a column-range subset of the data frame
## (positional indexing: works regardless of how the names were mangled)
out1 <- lifeTable_analyze(stages = d[2:8], adult_days = d$Adult,
sex = d$gender, oviposition = d[, 11:17],
file_name = "Example - way 1")
out1$results$N # number of individuals
out1$results$R0 # net reproductive rate
## --- with bootstrap standard errors (small B for a fast example;
## use the default B = 100000 for publications)
out1b <- lifeTable_analyze(stages = d[2:8], adult_days = d$Adult,
sex = d$gender, oviposition = d[, 11:17],
file_name = "Example - way 1",
bootstrap = TRUE, B = 2000, seed = 1)
out1b$results$boot$summary
## --- way 2: pass a named list of single columns
## (the list names become the stage names in plots and results)
out2 <- lifeTable_analyze(stages = list(Egg = d[[2]], "1st instar" = d[[3]],
"2nd instar" = d[[4]], "3rd instar" = d[[5]],
"4th instar" = d[[6]], Prepupa = d[[7]],
Pupa = d[[8]]),
adult_days = d$Adult, sex = d$gender,
fecundity = FALSE) # survival analysis only,
# oviposition not supplied
out2$results$N
## --- way 3: select the stage columns by their original names
## (re-read with check.names = FALSE to keep "1st instar", "2nd instar", ...)
d3 <- read.csv(f, check.names = FALSE)
out3 <- lifeTable_analyze(stages = d3[, c("Egg", "1st instar", "2nd instar",
"3rd instar", "4th instar",
"Prepupa", "Pupa")],
adult_days = d3$Adult, sex = d3$gender,
oviposition = d3[, 11:17],
stage_names = c("Egg", "L1", "L2", "L3", "L4",
"Prepupa", "Pupa"),
plot = TRUE,
legend_labels = c("Egg", "L1", "L2", "L3", "L4",
"Prepupa", "Pupa",
"Female", "Male"))
out3$plot # print or further customise the ggplot object
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.