library(hyenaR) ## For our hyena specific functionslibrary(dplyr) ## For most data wranglinglibrary(ggplot2) ## For plottinglibrary(lubridate) ## Working with dateslibrary(tidyr) ## Extra data wrangling functionslibrary(stringr) ## Working with textlibrary(waldo) ## To compare objectslibrary(skimr) ## Inspect data
STEP 2: Load the database
load_package_database.full(# Location of our database filedb.path ="example_git/source_data/Fisidata_2022_08_10.sqlite")
Today’s goals
GOAL 1: 🧑🏫 Practical application of {hyenaR}
GOAL 1: 🧑🏫 Practical application of {hyenaR}
List of all adults of main clans with their highest lifetime rank.
GOAL 1: 🧑🏫 Practical application of {hyenaR}
List of all adults of main clans with their highest lifetime rank.
GOAL 1: 🧑🏫 Practical application of {hyenaR}
List of all adults of main clanswith their highest lifetime rank.
List of all adults of main clans
## Work within the observation period...start_date =find_pop_date.observation.first()end_date =find_pop_date.observation.last()create_id_starting.table(#Only known sexsex =c("female", "male"),#Individuals started and stopped being adults#in the study period (i.e. uncensored)lifestage ="adult",lifestage.overlap ="within",#Only use individuals born in main clans#Males that disperse from outside could have adult period unobservedclan.birth =find_clan_name.all(main.clans =TRUE),from = start_date,to = end_date)
# A tibble: 904 × 1
ID
<chr>
1 A-008
2 A-010
3 A-018
4 A-019
5 A-040
6 A-041
7 A-046
8 A-049
9 A-054
10 A-056
# … with 894 more rows
…with their highest lifetime rank.
create_id_starting.table(sex =c("female", "male"),lifestage ="adult",lifestage.overlap ="within",clan.birth =find_clan_name.all(main.clans =TRUE),from = start_date,to = end_date) %>%# Find the period during which ranks need to be calculatedmutate(start_adult =fetch_id_date.at.age(ID, age =2),end_adult =fetch_id_date.death(ID))
(expanded_df <-create_id_starting.table(sex =c("female", "male"),lifestage ="adult", lifestage.overlap ="within",clan.birth =find_clan_name.all(main.clans =TRUE),from = start_date, to = end_date) %>%mutate(start_adult =fetch_id_date.at.age(ID, age =2),end_adult =fetch_id_date.death(ID)) %>%#Expand data to one row per 6 month period of an adults lifereshape_row_date.seq(ID, from = start_adult, to = end_adult, by ="6 month"))
# A tibble: 9,798 × 2
ID date
<chr> <date>
1 A-008 1996-05-20
2 A-008 1996-11-20
3 A-008 1997-05-20
4 A-008 1997-11-20
5 A-008 1998-05-20
6 A-008 1998-11-20
7 A-008 1999-05-20
8 A-008 1999-11-20
9 A-008 2000-05-20
10 A-008 2000-11-20
# … with 9,788 more rows
…with their highest lifetime rank.
mainclans <-find_clan_name.all()system.time({expanded_df_rank <- expanded_df %>%# Extract current clan and per 6 monthsmutate(current_clan =fetch_id_clan.current(ID, at = date)) %>%# Remove individuals that were outside the clan at any pointgroup_by(ID) %>%filter(all(current_clan %in% mainclans)) %>%ungroup() %>%#Extract ranks of remaining individualsmutate(rank =fetch_id_rank.std(ID, at = date))})
user system elapsed
5.655 0.595 6.053
…with their highest lifetime rank.
expanded_df_rank
# A tibble: 9,348 × 4
ID date current_clan rank
<chr> <date> <chr> <dbl>
1 A-008 1996-05-20 A 0.0244
2 A-008 1996-11-20 A -0.116
3 A-008 1997-05-20 A -0.277
4 A-008 1997-11-20 A -0.257
5 A-008 1998-05-20 A -0.189
6 A-008 1998-11-20 S -0.111
7 A-008 1999-05-20 S 0
8 A-008 1999-11-20 S 0.167
9 A-008 2000-05-20 S 0
10 A-008 2000-11-20 S 0.333
# … with 9,338 more rows