#The repository contains all variables required to reconstruct the datasets used in the statistical analyses presented in the manuscript. The accompanying R script documents the processing steps required to generate the model-specific datasets used in each analysis. 
#Original GPS coordinates, timestamps, and animal identifiers were removed for confidentiality and animal welfare reasons. The code below describes the few processing steps required to reconstruct the specific datasets used in each model.

#For Model 1a all variables required for reproducing the analysis are already included in the deposited dataset.
#For Model 1b, one record per individual-year combination is required because home-range size is constant within each individual-year.
library(dplyr)

dataset_small_repository <- read.csv(
"Data_for_Bye_bye_baby_Effects_of_bear_predation_on_the_movement_and_behaviour_of_female_moose.csv",
na.strings = "NA"
)
dataset_small_repository <- dataset_small_repository %>%
  mutate(
    individual_year_id = paste(individual_id, year, sep = "_")
  )

dataset_small_repositorysingle <- dataset_small_repository %>%
  group_by(individual_year_id) %>%
  slice(1) %>%   # take first row of each individual_year_id
  ungroup()

#For Model 2a it is necessary to subset the dataset, focusing only on the females who lost their offspring. Then, we further subset the dataset, focusing only on the time after predation.

dataset_small_repository_lost <- subset(dataset_small_repository, calf_status == "Lost")

dataset_small_repository_lost2 <- subset(dataset_small_repository_lost, time_until_or_from_predation > 0)

#Then, we create 3 groups based on Age Classes.

dataset_small_repository_lost2 <- dataset_small_repository_lost2 %>%
  mutate(AgeGroup = case_when(
    calf_age_at_death >= 1 & calf_age_at_death <= 6  ~ "Group1",
    calf_age_at_death >= 7 & calf_age_at_death <= 14 ~ "Group2",
    calf_age_at_death >= 15                       ~ "Group3",
    TRUE                                       ~ NA_character_
  ))
dataset_small_repository_lost2 <- dataset_small_repository_lost2 %>%
  mutate(AgeGroup = factor(AgeGroup, levels = c("Group1", "Group2", "Group3"),
                           labels = c("Age 1-6 days", "Age 7-14 days", "Age ≥15 days")))

##For Model 2b, we calculate the daily movement rate by summing movement rates within each individual and Julian day.


dataset_small_repository_lost2 <- dataset_small_repository_lost2 %>%
  group_by(individual_year_id, julian.day) %>% 
  mutate(daily_movement_rate = sum(movement_rate, na.rm = TRUE)) %>% 
  ungroup()

