0

I am trying to use the prepData function in the R package moveHMM. I am getting "Error in prepData(x, coordNames = c("lon", "lat")) : Each animal's obervations must be contiguous."

x is a data.frame with column names "ID", "long", "lat". ID column is the name of each animal as a character, and lon/lat are numeric. There are no NA values, no missing rows.

I do not know what this error means nor can I fix it. Help please.

x <- data.frame(dat$ID, dat$lon, dat$lat)
hmmgps <- prepData(x, coordNames=c("lon", "lat"))
Rachel
  • 109
  • 8

1 Answers1

0

The function prepData assumes that the rows for each track (or each animal) are grouped together in the data frame. The error message indicates that it is not the case, and that at least one track is split. For example, the following (artificial) data set would cause this error:

> data
  ID      lon       lat
1  1 54.08658 12.190313
2  1 54.20608 12.101203
3  1 54.18977 12.270896
4  2 55.79217  9.943341
5  2 55.88145  9.986028
6  2 55.91742  9.887342
7  1 54.25305 12.374541
8  1 54.28061 12.190078

This is because the track with ID "1" is split into two parts, separated by the track with ID "2".

The tracks need to be contiguous, i.e. all observations with ID "1" should come first, followed by all observations with ID "2". One possible solution would be to order the data by ID and by date.

Consider the same data set, with a "date" column:

> data
  ID      lon       lat                date
1  1 54.08658 12.190313 2019-09-06 14:20:00
2  1 54.20608 12.101203 2019-09-06 15:20:00
3  1 54.18977 12.270896 2019-09-06 16:20:00
4  2 55.79217  9.943341 2019-09-04 07:55:00
5  2 55.88145  9.986028 2019-09-04 08:55:00
6  2 55.91742  9.887342 2019-09-04 09:55:00
7  1 54.25305 12.374541 2019-09-06 17:20:00
8  1 54.28061 12.190078 2019-09-06 18:20:00

Following the answer to that question, you can define the ordered data set with:

> data_ordered <- data[with(data, order(ID, date)),]
> data_ordered
  ID      lon       lat                date
1  1 54.08658 12.190313 2019-09-06 14:20:00
2  1 54.20608 12.101203 2019-09-06 15:20:00
3  1 54.18977 12.270896 2019-09-06 16:20:00
7  1 54.25305 12.374541 2019-09-06 17:20:00
8  1 54.28061 12.190078 2019-09-06 18:20:00
4  2 55.79217  9.943341 2019-09-04 07:55:00
5  2 55.88145  9.986028 2019-09-04 08:55:00
6  2 55.91742  9.887342 2019-09-04 09:55:00

Then, the ordered data (excluding the date column) can be passed to prepData:

> hmmgps <- prepData(data_ordered[,1:3], coordNames = c("lon", "lat"))
> hmmgps
  ID     step      angle        x         y
1  1 16.32042         NA 54.08658 12.190313
2  1 18.85560  2.3133191 54.20608 12.101203
3  1 13.37296 -0.6347523 54.18977 12.270896
4  1 20.62507 -2.4551318 54.25305 12.374541
5  1       NA         NA 54.28061 12.190078
6  2 10.86906         NA 55.79217  9.943341
7  2 11.60618 -1.6734604 55.88145  9.986028
8  2       NA         NA 55.91742  9.887342

I hope that this helps.