Skip to contents
If you haven’t already check it, have a look at:

Load libraries


library(lifelihood)
library(tidyverse)
#> ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
#> ✔ dplyr     1.1.4     ✔ readr     2.1.5
#> ✔ forcats   1.0.0     ✔ stringr   1.5.1
#> ✔ ggplot2   3.5.2     ✔ tibble    3.3.0
#> ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
#> ✔ purrr     1.0.4     
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag()    masks stats::lag()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Data preparation


Load the dataset from .csv file:

# input data
df <- fakesample |>
  mutate(
    type = as.factor(type),
    geno = as.factor(geno)
  )

Prepare input parameters for the lifelihood() function:

# name of the columns of the clutchs into a single vector
clutchs <- c(
  "clutch_start1",
  "clutch_end1",
  "clutch_size1",
  "clutch_start2",
  "clutch_end2",
  "clutch_size2"
)

Note: If you have a large number of clutches, it is easier to generate this vector programmatically, particularly if your dataset contains a large number of clutches. See the Generate clutch names vignette.

Create the lifelihoodData object

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")
)

Get the results


Let’s run the analysis with default parameters.

results <- lifelihood(
  lifelihoodData = dataLFH,
  path_config = get_config_path("config")
)

Warning


What is it?

When runnning lifelihood() function, you might encounter the following warning:

## Warning in check_valid_estimation(results_lifelihood = results): Estimation of
## 'increase_death_hazard' is close to the maximum bound:
## increase_death_hazard≃9.9864992332278. Consider increasing maximum bound.

This warning indicates that the estimation of the increase_death_hazard parameter is close to the maximum bound.

This is not an error, but a signal that the estimation of the increase_death_hazard parameter is approaching its upper limit. During the optimization process, the algorithm may have been constrained by the current parameter boundaries, potentially affecting the accuracy of the estimation.

To address this warning, you can consider increasing the maximum bound of the increase_death_hazard parameter. This will give the optimization algorithm more flexibility to find the best estimate.

Customize parameter boundaries

You can get the parameter boundaries with the default_bounds_df() function and by passing the lifelihoodData object:

bounds_df <- default_bounds_df(dataLFH)

Since 10 seems to not be high enough, let’s try with 80:

bounds_df[bounds_df$name == "increase_death_hazard", "max"] <- 80

Once it’s changed, you just have to call lifelihood() again with the param_bounds_df argument:

results <- lifelihood(
  lifelihoodData = dataLFH,
  path_config = get_config_path("config"),
  param_bounds_df = bounds_df
)
#> Warning in check_valid_estimation(results): Estimation of 'expt_death' is close
#> to the minimum bound: expt_death~=0.00100001799607917. Consider decreasing
#> minimum bound.
#> Warning in check_valid_estimation(results): Estimation of 'survival_shape' is
#> close to the maximum bound: survival_shape~=499.999953196802. Consider
#> increasing maximum bound.

Now we don’t get any warning!