R package for animal behavior classification from accelerometer data—rabc

Abstract Increasingly, animal behavior studies are enhanced through the use of accelerometry. To allow translation of raw accelerometer data to animal behaviors requires the development of classifiers. Here, we present the “rabc” (r for animal behavior classification) package to assist researchers with the interactive development of such animal behavior classifiers in a supervised classification approach. The package uses datasets consisting of accelerometer data with their corresponding animal behaviors (e.g., for triaxial accelerometer data along the x, y and z axes arranged as “x, y, z, x, y, z,…, behavior”). Using an example dataset collected on white stork (Ciconia ciconia), we illustrate the workflow of this package, including accelerometer data visualization, feature calculation, feature selection, feature visualization, extreme gradient boost model training, validation, and, finally, a demonstration of the behavior classification results.


| INTRODUC TI ON
Our understandings of animal movement patterns and behaviors continue to rapidly advance with the use of ever smarter and smaller tracking technologies (Ropert-Coudert & Wilson, 2005;Williams et al., 2019). Increasingly, the tracking of animals is also combined with accelerometer (ACC) data collection to study the free-roaming behaviors of animals across a wide range of taxa (Brown et al., 2013;Shepard et al., 2008). Compared with direct human observation, using ACC to study animal behaviors has the obvious advantage that it reduces the influence of human presence and also allows the recording of behaviors that would otherwise be hard to observe, away from the human eye (Brown et al., 2013). However, these obvious merits of ACC technology can only be achieved when a reliable behavior classification model is available that can convert ACC data into meaningful behavior types.
Many studies have already conducted behavior classification from ACC data (e.g., Nathan et al., 2012). In most cases, ACC data with corresponding behavioral field observations are used to train behavior classification models (e.g., Kölzsch et al., 2016;Kröschel et al., 2017). However, in some instances the developed classifiers that translate ACC data into behavior types yield only low classification accuracy (Fehlmann et al., 2017). As a general remedy, using fewer behavior classes and aggregating behaviors usually yields better classification performance (Ladds et al., 2017). Such grouping of behaviors is typically based solely on biological or ecological considerations without the use of computational pattern recognition.
Nevertheless, some behaviors whose discrimination may have little biological value might have very similar ACC recording patterns and grouping of these behaviors based on the observed patterns might potentially yield better classification models. It is this often iterative process of grouping and splitting behaviors within the behavior set that the here presented rabc (r for animal behavior classification) package also endeavors to assist with. In this way, the rabc package allows the user to derive optimal and validated behavior classifiers suited to their specific research system and questions.
To help biologists translate ACC data into behaviors, this package uses XGBoost, which is currently one of the most promising supervised machine learning methods for this specific purpose . Unlike the web-based tool "AcceleRater" (Resheff et al., 2014), our rabc package does not focus on providing a "onestop service" turning ACC data into behaviors. Rather, this package focuses on (a) providing interactive visualization tools to its user to assist in handling and interpreting the ACC input data, (b) deciding on appropriate behavior categories for classification as highlighted in the previous paragraph, and (c) reducing ACC data volume efficiently and effectively (through the calculation and selection of a range of features) without compromising behavior classification performance. In brief, this package endeavors to open the lid of the machine learning "black-box", allowing the integration of the user's expert knowledge on their own research system in developing advanced behavior classification models.

| r ab c WORK FLOW
The general workflow of the rabc package to transform ACC data using supervised machine learning methods into behaviors is outlined in Figure 1, The rabc package can be installed in Rstudio by "devtools::install_ github("YuHuiDeakin/rabc", build_vignette=TRUE)".

| ACC dataset preparation and behavior labels
Segments of continuous ACC data will need to be translated into meaningful behaviors. For ACC data segmentation, there are two choices: even-length segmentation and variable-length segmentation (Bom et al., 2014). Variable-length segmentation requires an F I G U R E 1 The general workflow of the rabc package to develop classifiers for adequately transforming ACC data into behaviors. The various elements in the diagram are numbered according to the paragraphs where these are being described in detail algorithm to detect behavior change points and may thus be prone to error. Even-length segmentation does not require these additional calculations and is therefore much easier to implement. However, even-length ACC segments will inevitably contain behavior change points (and thus multiple behaviors) affecting down the line processing and behavior classification. An ACC segment should be sufficiently long to contain enough data to be representative of a behavior (and, thus, interpretable as a specific behavior type), whereas its length should be limited to avoid inclusion of multiple behaviors as much as possible. Regarding the inevitable segments where behavior transitions take place, we recommend retaining these segments in the model training. Although these data might decrease the accuracy of the classification model, they will make the model more robust and avoid overestimating model performance.
The rabc package only supports even-length segmentation data with corresponding behavioral data, that is, the key behavior scored for the duration of the segment. These behavioral data are essential for supervised machine learning methods. ACC data collection with associated behavioral observations can be made both in the wild (e.g., Kröschel et al., 2017) and in captivity (e.g., Kölzsch et al., 2016).
Obviously great care should be taken that observations (video recording) are accurately synchronized with ACC data collection (e.g., Kröschel et al., 2017). Although not provided by the rabc package, to reduce signal noise raw ACC data can potentially be preprocessed (Brown et al., 2013) before entering the data into the rabc package.
Such "filtered" data would however require that any behavioral classification model generated by the rabc package is used to predict behaviors on filtered ACC data exclusively.
The input data should be a data.frame or tibble containing data including the behavior associated with the ACC data. For triaxial ACC data, each row of equal length should be arranged as "x, y, z, x, y, z, …, behavior", where "behavior" is the (primary) behavior observed during that segment. For dual-axial ACC data, it should be arranged as "x, y, x, y, …,behavior" and for single-axial ACC data as "x, x, …, behavior". A range of ACC data formats exist that are different to the format required by the rabc package. For instance, ACC data from triaxial trackers developed by e-obs GmbH (Munich, Germany) are arranged as "x y z x y z … ". At the end of this section, we provide an example for reading data recorded by e-obs trackers (Pokrovsky et al., 2021) and transforming these into a format suitable for the rabc package. Data provided by Ornitela (Vilnius, Lithuania) and Druid Technology (Chengdu, China) ACC trackers are arranged in a four column table format, where each row contains "timestamp, x, y, z". Thus, 10 rows of data make one second of ACC recordings when the sampling frequency is 10 Hz. In the vignette of the rabc package, which can be accessed by using the function "browseVignettes('rabc')", we provide an example on converting this specific format to the format required by the rabc package.
The here used triaxial ACC demo dataset from white stork (Ciconia ciconia) (data accessible from the AcceleRater website: http:// TA B L E 1 Summary of rabc functions. In the "Wrapper" field, functions from other R packages that are being used in the rabc package are being listed columns. The first 120 columns are ACC measurements from three orthogonal axes, arranged as x, y, z, x, y, z, …,x, y, z. The final column is of type character containing the corresponding behavior. The dataset contains 5 different behaviors including "A_FLIGHT" -active flight (77 cases), "P_FLIGHT" -passive flight (96), "WALK" -walking (437), "STND" -standing (863), "SITTING" -sitting (273).

| ACC visualization
The rabc package offers two types of graphs, that is, dynamic graphs and static graphs. Dynamic graphs produced by the "dygraphs" package (Vanderkam et al., 2018) allow users to zoom in and out and scroll through the depicted ACC data to facilitate data examination.
Static graphs produced by the "ggplot2" package (Wickham, 2016) help users to examine feature distributions and to check behavior classification results.
Prior to visualizing the ACC data, the dataset needs to be sorted by behavior using the order_acc function. The purpose of this function is to ease comparison of ACC patterns among segments sharing the same behavior labels. For ACC data visualization, the rabc package uses the function dygraph from the "dygraphs" package to plot all ACC segments grouped by behavior. This dynamic mode of presentation provides the user with a visual impression of how the ACC signal generally relates to the different behaviors and can also be used for data quality control (i.e., identifying potentially incorrect segments where ACC and behavioral data do not conform to the general pattern otherwise observed due to, for instance, incorrect behavioral observation). The x-axis of this dygraph indicates the row sequence number (i.e., the segment number) of the sorted data.
Plotting the complete white stork ACC dataset using function plot_acc ( Figure 2a) and next zooming in on the area around segments 55-80 ( Figure 2b), it can be seen that the ACC data between segments 60 and 70 is very different from neighboring segments.
Albeit all being labeled as "A_FLIGHT", the ACC data in this range resemble more static behaviors, warranting their scrutiny and, potentially, their relabeling or removal from the dataset.
In the following, the relevant R code plotting ACC data:

| Feature calculation
The next step is to calculate features from the ACC data. A feature is a specific mathematical description (such as the mean and the standard deviation) of the ACC signal within a segment, which will form the input to the machine learning models (Brown et al., 2013 This value has been proven to be correlated with the animal's energy expenditure . These features are calculated for each ACC axis separately (denoted with prefix x, y, z in the output data frame), except for ODBA, which is calculated using all available axes.
The frequency-domain feature set includes main frequency, main amplitude, and frequency entropy. Also, these features are calculated for each ACC axis separately (denoted with prefix x, y, z users may consider calculation of custom features. All functions in the rabc package are also able to process custom features after the user has included these in the feature data frame using functions cbind or bind_cols from the "dplyr" package (Wickham et al., 2021).
In the following, we present the relevant R code calculating features from ACC data:  The purpose of the wrapper is to select most relevant features. The wrapper part applies stepwise forward selection (SFS) (Rückstieß et al., 2011) using the extreme gradient boosting (XGBoost) model, which is not only used for feature selection but also for the final classification model (see below). XGBoost is a scalable tree boosting method that proved to be faster and have a better performance than other currently available tree boosting methods (Chen & Guestrin, 2016). In a comparison with three other supervised machine learning methods (support vector machine, artificial neural network, and random forest models), XGBoost classified behavior from ACC data similarly well to the alternative methods.
However, XGBoost had the fastest runtime and the second smallest memory usage . In the rabc package, we use UMAP (Konopka, 2020) (Kuhn, 2020) and "xgboost" packages (Chen et al., 2021) to automatically conduct the three above steps for model construction and evaluation.
Four arguments can be set in the function train_model to control the training and validation processes. Which features to use for model building is set by "df", which in the following example is set to "selection$features[1:6]" (i.e., the first six selected features from the feature selection procedure). The "vec_label" argument is used to pass on a vector of behavior types. How to select the hyperparameter set is set by "hyper_choice", which has two options.
The first is "defaults" which will let XGBoost use its default hyperparameters with a fixed setting of "nrounds = 10". The alternative "hyper_choice" option is "tune", which will run repeated crossvalidations (main parameters: method = "repeatedcv", number = 5, repeats = 3) to find a best set. Note that for four hyperparameters, html>. The third part of the output, statistics by class, presents a range of performance statistics for the individual behavioral categories, which are explained in detail in <https://topepo.github.io/ caret/ measu ring-perfo rmance.html>. Finally, the importance of the Image Missing F I G U R E 6 Demonstrations of the three tabs generated by the plot_UMAP function. Tab a-UMAP calculation and tuning-evaluates whether ACC features represent behaviors. The "Features to input" section allows users to choose which feature groups to use as input to UMAP. The "UMAP hyperparameter tuning" section allows users to interactively adjust three hyperparameters within the UMAP function to control the two-dimensional clustering. Tab (Figure 8).
The R code to visualize incorrect classifications

| D ISCUSS I ON AND CON CLUS I ON S
As demonstrated, the rabc package can assist researchers in developing good animal behavior classification models in an interactive fashion. ACC data visualization assists in the detection of aberrant associated behavior scores. Feature visualization helps researchers to understand how different features distribute across behaviors and whether the current behavior set potentially needs adjustments, either by grouping or by splitting behaviors into new behavior types.
Finally, classification-result visualization assists the understanding of misclassification patterns. Other than the visualization functionalities, this package provides complete functions to perform behavior classification through XGboost, including feature calculation, feature selection, model hyperparameter tuning, model training and validation, and an output classifier for future ACC data classification.
Given its unique aim and functionality, the rabc package will be a valuable addition to the growing array of R packages already available for behavior and movement analyses (Joo et al., 2020). There is one other R package, "m2b", that shows some resemblance to the rabc package in that it uses supervised machine learning (random forest) to classify behaviors, be it from GPS rather than ACC data. The rabc package only supports classification in a supervised fashion, which requires users to label ACC data with the corresponding behavior types. However, in some cases behavioral data may not be available and for those circumstances users may want to resort to using the Ethographer package in Igor Pro (WaveMetrics Inc., USA) for processing ACC data in an unsupervised fashion (e.g., Berlincourt et al., 2015).
A non-R tool designed for animal behavior classification that also uses ACC data in combination with behavioral observations is AcceleRater. Like rabc, AcceleRater trains behavior classification models, yet, there are three major differences between AcceleRater and the rabc package. Firstly, the rabc package is written in R and used in the R environment, which gives users ample freedom of preprocessing and postprocessing the data. Secondly, rather than offering a "black-box" training process, the visualization tools within rabc assist users in building an understanding of the behavior classification process and why some behaviors can be better classified than others, providing avenues to modify or improve the behavior classification model. Finally, the classification model trained in rabc can be exported and used on-board of trackers as for instance used in . It is worth noting that the features calculated in the rabc package can be further extended if deemed necessary. Users can develop additional features and include these in the here described analyses and the F I G U R E 8 ACC data visualization including behavior classification results using a dynamic graph. White stork ACC data are shown from segment 55 to 80 (cf Figure 2b). Vertical black, dashed lines separate different originally observed behavior types, while vertical gray dotted lines mark incorrect predictions with predicted behavior type labeled at the top plot_wrong_classifications(df_raw = whitestork_acc_sorted, df_result = predictions) # produces Figure 8 ultimate generation of a behavior classification model. Although we only use XGboost as the supervised machine learning model in this package, users can potentially use the output from the rabc package as input to the "caret" package. This will allow for the use of other machine learning models in generating behavior classification models such as decision tree, support vector machine, and random forest. Finally, although ACC data from different animal species and under a variety of circumstances are increasingly becoming available, where possible, we encourage making these accessible with the associated behavior labels. Such data may not only be used to guide studies on new species with comparable behavioral repertoires, but also have the potential to ultimately generate cross-species behavioral classification models.

ACK N OWLED G M ENTS
We gratefully acknowledge Ran Nathan for sharing the dataset used in this study. We thank Batbayar Galtbalt and Tobias Alexander Ross for testing the software and providing feedback. We thank Rocío Joo and an anonymous reviewer for their constructive comments on an earlier version of the manuscript and the rabc R package.

CO N FLI C T O F I NTE R E S T
The authors declare that there is no conflict of interest.

DATA AVA I L A B I L I T Y S TAT E M E N T
The white stork dataset used in this paper is accessible from the online software AcceleRater website: http://accapp.move-ecol-miner va.huji.ac.il/. The dataset is also archived at: https://doi.org/10.5061/ dryad.dz08k prxv.