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}
Extract monthly female reproduction and clan size for Airstrip. Build a model to study the relationship.
GOAL 1: 🧑🏫 Practical application of {hyenaR}
Extract monthly female reproduction and clan size for Airstrip. Build a model to study the relationship.
GOAL 1: 🧑🏫 Practical application of {hyenaR}
Extract monthly female reproductionand clan size for Airstrip. Build a model to study the relationship.
GOAL 1: 🧑🏫 Practical application of {hyenaR}
Extract monthly female reproductionand clan size for Airstrip. Build a model to study the relationship.
Extract monthly female reproduction…
start_date <-find_pop_date.observation.first()end_date <-find_pop_date.observation.last()## Extract all adult females in Airstrip during observation periodcreate_id_starting.table(sex ="female",clan ="A",lifestage ="adult",from = start_date,to = end_date)
# A tibble: 139 × 1
ID
<chr>
1 A-001
2 A-002
3 A-003
4 A-004
5 A-006
6 A-007
7 A-008
8 A-009
9 A-010
10 A-013
# … with 129 more rows
Extract monthly female reproduction…
start_date <-find_pop_date.observation.first()end_date <-find_pop_date.observation.last()create_id_starting.table(sex ="female", clan ="A", lifestage ="adult", from = start_date, to = end_date) %>%## Extract dates over which individual could reproduce...mutate(adult_date =fetch_id_date.at.age(ID = ID, age =2),death_date =fetch_id_date.death(ID = ID))
start_date <-find_pop_date.observation.first()end_date <-find_pop_date.observation.last()create_id_starting.table(sex ="female", clan ="A", lifestage ="adult", from = start_date, to = end_date) %>%mutate(adult_date =fetch_id_date.at.age(ID = ID, age =2),death_date =fetch_id_date.death(ID = ID)) %>%## Use case when to deal with left and right censored individuals...mutate(start_date =case_when(adult_date < start_date ~ start_date,TRUE~ start_date),end_date =case_when(is.na(death_date) ~ end_date,TRUE~ death_date))
tibble(clan ="A") %>%#Expand out for Airstrip over the same dates...reshape_row_date.seq(clan,from =ceiling_date(start_date, unit ="month"),to =floor_date(end_date, unit ="month"),by ="month") %>%mutate(end_date =lead(date) -1) %>%filter(!is.na(end_date))
# A tibble: 314 × 3
clan date end_date
<chr> <date> <date>
1 A 1996-05-01 1996-05-31
2 A 1996-06-01 1996-06-30
3 A 1996-07-01 1996-07-31
4 A 1996-08-01 1996-08-31
5 A 1996-09-01 1996-09-30
6 A 1996-10-01 1996-10-31
7 A 1996-11-01 1996-11-30
8 A 1996-12-01 1996-12-31
9 A 1997-01-01 1997-01-31
10 A 1997-02-01 1997-02-28
# … with 304 more rows
…and clan size for Airstrip
clan_data <-tibble(clan ="A") %>%reshape_row_date.seq(clan, from =ceiling_date(start_date, unit ="month"), to =floor_date(end_date, unit ="month"), by ="month") %>%mutate(end_date =lead(date) -1) %>%filter(!is.na(end_date)) %>%mutate(clan_size =fetch_clan_number(clan = clan,from = date, to = end_date))
…and clan size for Airstrip
clan_data
# A tibble: 314 × 4
clan date end_date clan_size
<chr> <date> <date> <int>
1 A 1996-05-01 1996-05-31 42
2 A 1996-06-01 1996-06-30 44
3 A 1996-07-01 1996-07-31 44
4 A 1996-08-01 1996-08-31 44
5 A 1996-09-01 1996-09-30 41
6 A 1996-10-01 1996-10-31 43
7 A 1996-11-01 1996-11-30 44
8 A 1996-12-01 1996-12-31 47
9 A 1997-01-01 1997-01-31 49
10 A 1997-02-01 1997-02-28 49
# … with 304 more rows
…and clan size for Airstrip
final_data <- repro_data %>%left_join(clan_data, by =c("date", "end_date"))
Build a model to study the relationship.
mod <-glm(repro ~ clan_size, data = final_data,family ="binomial")summary(mod)
Call:
glm(formula = repro ~ clan_size, family = "binomial", data = final_data)
Deviance Residuals:
Min 1Q Median 3Q Max
-0.2270 -0.1487 -0.1235 -0.1091 3.2734
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -6.01069 0.18759 -32.042 < 2e-16 ***
clan_size 0.01778 0.00219 8.119 4.69e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 3280.9 on 31173 degrees of freedom
Residual deviance: 3218.5 on 31172 degrees of freedom
AIC: 3222.5
Number of Fisher Scoring iterations: 7