如何为隐马尔可夫模型的 GPS 数据创建规则时间间隔

How to create regular time intervals on GPS data for a Hidden Markov Model

我想对我拥有的一些跟踪数据应用隐马尔可夫模型。我读到需要定期对数据进行采样才能使该模型正常工作。但是,我的足迹并不总是完全规则的。如何在 R 中规范化我的数据集?

这里有一些模拟数据供您使用

tracks <- read.table(text = 
                   "latitude,  longitude time
                 52.111122,  -10.544271  12:00
                 52.10944,   -10.554933  13:00
                 52.108898,  -10.558025  14:00
                 52.108871,  -10.560946  15:01
                 52.113991,  -10.582005  16:22
                 52.157223,  -10.626506  17:00
                 52.194977,  -10.652878  18:04
                 52.240215,  -10.678817  19:09
                 52.26421,   -10.720366  20:00
                 52.264015,  -10.720642  21:05"
                 , header = TRUE, sep = ",")

谢谢

使用 approx 您可以将位置插值到固定间隔:

首先,将时间改为POSIXct:

tracks$time <- as.POSIXct(sprintf("%s %s", Sys.Date(), tracks$time))
tracks$type = "original"

计算间隔:

n <- nrow(tracks)
tracks2 <- data.frame(
  latitude = approx(x = tracks$time, y = tracks$latitude, n = n)$y,
  longitude = approx(x = tracks$time, y = tracks$longitude, n = n)$y,
  time = as.POSIXct(approx(tracks$time, 1:nrow(tracks), n = n)$x, origin = "1970-01-01"),
  type = "corrected"
)

您可以使用 ggplot 检查位置的变化:

ggplot(rbind(tracks, tracks2), aes(x = time, y = latitude, color = type)) +
  geom_line() +
  geom_point() +
  scale_x_datetime(breaks = tracks2$time, minor_breaks = NULL, labels = format(tracks2$time, format = "%H:%M") ) +
  theme_minimal()