Calculate the empirical mortality rate over a given interval.
Usage
compute_mortality_rate(
lifelihoodData,
interval_width,
max_time = NULL,
min_sample_size = 1,
groupby = NULL
)
Arguments
- lifelihoodData
lifelihoodData
object created withlifelihoodData()
.- interval_width
The interval width used to calculate the mortality rate. For instance, if the time unit for deaths in the original dataset is days and
interval_width
is set to 10, the mortality rate will be calculated every 10 days for each group.- max_time
The maximum time for calculating the mortality rate. If set to NULL, the time of the last observed death is used.
- min_sample_size
The minimum number of individuals alive at the beggining of a time interval for computing the observed mortality rate
- groupby
covariate(s) over which mortality rate should be computed (default is
NULL
).If NULL, calculates a single overall mortality rate.
If
"all"
, calculates mortality rate over each combination of covariates listed in thelifelihoodData
object provided.Otherwise must be a character (
"covariate1"
) or a character vector (c("covariate1", "covariate2")
).
Value
A dataframe with 3 columns: Interval (time interval, based
on interval_width
value), Group (identifier of a given subgroup,
or "Overall" if groupby = NULL), and MortalityRate (mortality rate
at this time).
Examples
library(lifelihood)
library(tidyverse)
df <- fakesample |>
mutate(
geno = as.factor(geno),
type = as.factor(type)
)
clutchs <- c(
"clutch_start1", "clutch_end1", "clutch_size1",
"clutch_start2", "clutch_end2", "clutch_size2"
)
dataLFH <- lifelihoodData(
df = df,
sex = "sex",
sex_start = "sex_start",
sex_end = "sex_end",
maturity_start = "mat_start",
maturity_end = "mat_end",
clutchs = clutchs,
death_start = "death_start",
death_end = "death_end",
covariates = c("geno", "type"),
model_specs = c("gam", "lgn", "wei")
)
mort_df <- compute_mortality_rate(dataLFH, interval_width = 2)
head(mort_df)
#> Interval_start Interval_end Mean_Interval MortalityRate
#> 1 0 2 1 0.2307692
#> 2 2 4 3 0.0000000
#> 3 4 6 5 0.1000000
#> 4 6 8 7 0.0000000
#> 5 8 10 9 0.0000000
#> 6 10 12 11 0.0000000
mort_df <- compute_mortality_rate(
dataLFH,
interval_width = 2,
groupby = NULL,
max_time = 170
)
head(mort_df)
#> Interval_start Interval_end Mean_Interval MortalityRate
#> 1 0 2 1 0.2307692
#> 2 2 4 3 0.0000000
#> 3 4 6 5 0.1000000
#> 4 6 8 7 0.0000000
#> 5 8 10 9 0.0000000
#> 6 10 12 11 0.0000000