-3

I am analysing several animal behaviours during a defined time period.

I watch videos of the animals and their behaviours. I record when each behaviour is displayed. They will display each behaviour several times during the recording (which correspond to the different events). Sometimes 2 or 3 behaviours can be displayed at the same time during the recording, but they don't usually start/finish exactly at the same time (so they overlap partly).

I end up with a series of events for each behaviour, and for each event I have their onset, duration and end point (see example hereafter).

I need to extract from this data the total amount during which behaviour 1 overlaps with behaviour 2 / behaviour 1 overlaps with behaviour 3 / behaviour 2 overlaps with behaviour 3. This is so that I can find correlations between behaviours, which ones tend to be displayed at the same time, which ones do not, ...

I am only a beginner with programming (mostly R) and I find it hard to get started. Can you please advise me how to proceed? Many thanks!

Example with a series of events for 3 behaviours:

Event tracked           Onset  Duration   End
Behaviour 1 _event 1    7.40    548.88  556.28
Behaviour 1 _event 2    36.20   0.47    36.67
Behaviour 1 _event 3    48.45   0.25    48.70
Behaviour 1 _event 4    68.92   1.53    70.45
Behaviour 1 _event 5    75.48   0.22    75.70
Behaviour 1 _event 6    89.75   0.66    90.41
Behaviour 1 _event 7    94.62   0.16    94.78
Behaviour 1 _event 8    101.78  0.22    102.00
Behaviour 1 _event 9    108.86  0.59    109.45
Behaviour 1 _event 10   146.35  0.66    147.00
Behaviour 1 _event 11   150.20  0.75    150.95
Behaviour 1 _event 12   152.98  0.66    153.64
Behaviour 1 _event 13   157.84  0.56    158.41
Behaviour 2_event 1     7.52    0.38    7.90
Behaviour 2_event 2     18.73   0.16    18.88
Behaviour 2_event 3     19.95   2.25    22.20
Behaviour 2_event 4     26.41   0.25    26.66
Behaviour 2_event 5     35.91   0.16    36.07
Behaviour 2_event 6     37.29   0.34    37.63
Behaviour 2_event 7     38.13   0.72    38.85
Behaviour 2_event 8     40.19   0.31    40.51
Behaviour 2_event 9     44.26   0.16    44.41
Behaviour 2_event 10    45.32   0.16    45.48
Behaviour 2_event 11    54.84   1.44    56.27
Behaviour 2_event 12    56.65   1.19    57.84
Behaviour 2_event 13    61.59   1.03    62.62
Behaviour 2_event 14    81.13   3.83    84.96
Behaviour 2_event 15    86.65   0.31    86.96
Behaviour 2_event 16    90.15   0.19    90.34
Behaviour 2_event 17    96.97   0.53    97.50
Behaviour 2_event 18    107.12  0.22    107.34
Behaviour 2_event 19    118.53  0.41    118.94
Behaviour 2_event 20    127.76  0.25    128.01
Behaviour 2_event 21    129.45  0.69    130.13
Behaviour 2_event 22    130.60  2.31    132.91
Behaviour 2_event 23    141.01  0.41    141.41
Behaviour 2_event 24    152.85  0.37    153.23
Behaviour 2_event 25    156.54  0.13    156.66
Behaviour 3_event 1     7.71    1.94    9.65
Behaviour 3_event 2     11.12   1.53    12.65
Behaviour 3_event 3     19.01   0.19    19.20
Behaviour 3_event 4     20.01   3.97    23.98
Behaviour 3_event 5     24.95   4.22    29.16
Behaviour 3_event 6     29.70   2.19    31.88
Behaviour 3_event 7     33.23   2.50    35.73
Behaviour 3_event 8     36.82   0.44    37.26
Behaviour 3_event 9     38.20   1.16    39.35
Behaviour 3_event 10    39.91   2.13    42.04
Behaviour 3_event 11    42.49   3.62    46.11
Behaviour 3_event 12    47.09   0.53    47.62
Behaviour 3_event 13    48.15   0.34    48.49
Behaviour 3_event 14    49.40   2.13    51.52
Behaviour 3_event 15    57.57   2.25    59.82
Behaviour 3_event 16    60.89   0.88    61.76
Behaviour 3_event 17    66.85   6.78    73.63
Behaviour 3_event 18    75.65   3.03    78.68
user5249203
  • 4,436
  • 1
  • 19
  • 45
  • 3
    Hi and welcome to SO! Please have a look here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example to improve your question so that you can get more answers. – User2321 Nov 23 '16 at 18:08
  • What is your expected output ? – user5249203 Nov 23 '16 at 19:14

1 Answers1

0

In order to do the kind of study you want to do, it might be easiest to convert the data to a time series with variables on states (i.e. whether behavior 1, 2, 3, etc. is being displayed.) So you want to transform the dataset you have to one that looks like

time   animal    behav_1  behav_2  behav_3
0      1         FALSE    TRUE     FALSE
0      2         TRUE     FALSE    FALSE
1      1         FALSE    TRUE     FALSE
1      2         TRUE     FALSE    TRUE
...    ...       ...      ...      ...

Each row tells whether a particular animal is displaying each of the three behaviors at the given time. (I am assuming here that you have multiple animals and you want to keep their behavior data separate.)

Then you could easily approximate many of the quantities you are interested in. For example, you could compute the probability that an animal is doing behavior 1 given it is doing behavior 2 by

  1. Computing a column data$behav_1_and_2 <- data$behav_1 & data$behav_2
  2. Dividing the sum of the col behav_1_and_2 by the sum of behav_2: sum(data$behav_1_and_2) / sum(data$behav_2)

Okay, but how do you transform the data? First, decide how many time points you want to check. Maybe you should increment by about 0.1.

num_animals <- 10 // How many animals you have
time_seq <- seq(from = 0, to = 600, by = 0.1) // to should be end of video
data <- expand.grid(time = time_seq, animal = num_animals)

That gets you the first two columns of the data frame you want. Then you need to compute the three behavior columns. Define a function that takes the time, animal, and name of the behavior column, and returns TRUE if the animal is doing that behavior at the time, or FALSE if not.

has_behavior <- function(time, animal, behavior) {
    ...
}

(I'm going to let you figure out how to make that function.) With that function in hand, you can then create the last three columns with a loop:

// First create empty columns
data$behav_1 <- logical(nrow(data))
data$behav_2 <- logical(nrow(data))
data$behav_3 <- logical(nrow(data))

// Now loop through rows
for (i in 1:nrow(data)) {
    data$behav_1[i] <- has_behavior(data$time[i], data$animal[i], 1)
    data$behav_2[i] <- has_behavior(data$time[i], data$animal[i], 2)
    data$behav_3[i] <- has_behavior(data$time[i], data$animal[i], 3)
}

With data in this format, you should be able to study the problem much more easily. You can compute those summary quantities easily, as I outlined earlier. This data frame is also set up to be useful for doing time series modeling. And it's also tidy, making it easy to use with packages like dplyr for data summarising and ggplot2 for visualization. (You can learn more about those last two tools in the free online book R for Data Science by Hadley Wickham.)

Will Jones
  • 36
  • 2