Examples of using data and functions

The SRKW orca data is included as a dataframe. Each row is an individual, with birth and death information.

data(orca)
head(orca)
#>     X animal birth death  pod matriline  mom sexF1M2 includeFec includeSurv
#> 1 485   J001  1951  2010 J001      J002 <NA>       2          1           1
#> 2 486   J002  1911  2017 J001      J002 <NA>       1          1           1
#> 3 487   J003  1953  1995 J001      J016 <NA>       2          1           1
#> 4 488   J004  1957  1995 J001      J008 <NA>       1          1           1
#> 5 489   J005  1938  1997 J001      J009 <NA>       1          0           1
#> 6 490   J006  1956  1998 J001      J008 <NA>       2          1           1
#>   population
#> 1       SRKW
#> 2       SRKW
#> 3       SRKW
#> 4       SRKW
#> 5       SRKW
#> 6       SRKW

But for modeling, it’s more useful to work with an expanded version, where every animal-year combination gets its own row. The expand() function does this step,

data(ages2stages)
whaleData = kwdemog::expand(orca, ages2stages =ages2stages)

Then we can do all kinds of filtering for various analyses – e.g. 

whaleData = whaleData[whaleData$birth > 1970,]

# A handful of unknown sexes need to be randomly filled in
whaleData$sexF1M2[which(whaleData$sexF1M2==0)] = sample(c(1,2), size=length(which(whaleData$sexF1M2==0)), replace=T)

whales_since76 = as.character(orca$animal[orca$birth > 1970])

sub = whaleData[which(whaleData$animal%in%whales_since76 & whaleData$population=="SRKW" & !is.na(whaleData$gave_birth) & whaleData$sexF1M2=="1" & whaleData$age>=10 & whaleData$age< 43), ]
sub$animal = as.factor(sub$animal)


# filter out females alive in current year
currently_alive = dplyr::filter(sub, year == year.end, is.na(death))
knitr::kable(currently_alive)
year animal X birth death pod matriline mom sexF1M2 includeFec includeSurv population age alive gave_birth
2024 J022 506 1985 NA J001 J009 J010 1 1 1 SRKW 39 1 0
2024 J031 515 1995 NA J001 J008 J011 1 1 1 SRKW 29 1 0
2024 J035 519 1998 NA J001 J009 J017 1 1 0 SRKW 26 1 0
2024 J036 521 1999 NA J001 J016 J016 1 1 1 SRKW 25 1 0
2024 J037 522 2001 NA J001 J002 J014 1 1 1 SRKW 23 1 0
2024 J040 525 2004 NA J001 J002 J014 1 1 1 SRKW 20 1 0
2024 J041 526 2005 NA J001 J008 J019 1 1 1 SRKW 19 1 0
2024 J042 527 2007 NA J001 J016 J016 1 1 1 SRKW 17 1 0
2024 J046 531 2009 NA J001 J009 J028 1 1 1 SRKW 15 1 0
2024 J047 532 2010 NA J001 J009 J035 1 1 1 SRKW 14 1 0
2024 K016 558 1985 NA K001 K003 K003 1 1 1 SRKW 39 1 0
2024 K020 562 1986 NA K001 K007 K013 1 1 1 SRKW 38 1 0
2024 K022 564 1987 NA K001 K004 K012 1 1 1 SRKW 37 1 0
2024 K027 569 1994 NA K001 K007 K013 1 1 1 SRKW 30 1 0
2024 K042 584 2008 NA K001 K003 K014 1 1 1 SRKW 16 1 0
2024 K043 585 2010 NA K001 K004 K012 1 1 1 SRKW 14 1 0
2024 L072 648 1986 NA L001 L026 L043 1 1 1 SRKW 38 1 0
2024 L077 653 1987 NA L001 L012 L011 1 1 1 SRKW 37 1 0
2024 L082 658 1990 NA L001 L004 L055 1 1 1 SRKW 34 1 0
2024 L083 659 1990 NA L001 L021 L047 1 1 1 SRKW 34 1 0
2024 L086 662 1991 NA L001 L004 L004 1 1 1 SRKW 33 1 0
2024 L090 666 1993 NA L001 L026 L026 1 1 1 SRKW 31 1 1
2024 L091 667 1995 NA L001 L021 L047 1 1 1 SRKW 29 1 0
2024 L103 679 2003 NA L001 L004 L055 1 1 1 SRKW 21 1 0
2024 L113 689 2009 NA L001 L012 L094 1 1 1 SRKW 15 1 0
2024 L118 694 2011 NA L001 L004 L055 1 1 1 SRKW 13 1 0
# find last birth for each of these
last_birth = group_by(dplyr::filter(sub, gave_birth==1), animal) %>% 
  dplyr::summarise(last_birth = max(year[which(gave_birth==1)]))
last_birth$last_birth[!is.finite(last_birth$last_birth)] = NA

currently_alive = dplyr::left_join(currently_alive, last_birth) %>% 
  dplyr::mutate(unlikely_future_mom="") %>%
  dplyr::select(animal,age,last_birth,unlikely_future_mom) %>% data.frame()

Notes on demographic modeling

As a cautionary note, there may be interest in using these data to test correlations with prey (salmon) indices or other environmental drivers. There are several very nuanced and important things to note about different subsets of these data.

First, ages on animals in the beginning of the surveys were often ‘guessed’, and in the cases of females, assigned an age of 40 based on the last observed birth. This procedure will generate bias (many of the animals being < 40 during their last birth), so these animals have been excluded from fecundity-at-age analyses, or only used in stage-based survival estimates. The includeSurv and includeFec columns are used to designate most of these cases (1 = include, 0 = exclude).

Second, a handful of deaths are either tied to human disturbance (direct or indirectly associated with ship strikes) or are not independent. Calves that died at the same time as their moms are not independent (J24, J54, K39, L97, L104). Other animals are associated with vessel strikes (L98), satellite tag infections (L95) or unknown trauma (L112). For the latter three animals, it is possible to include the years they survive as data, and replace the death data with an NA. For example:

whaleData[which(whaleData$animal %in% c("L095","L098","L112") & whaleData$alive==0),] = NA