我正在尝试将隐马尔可夫模型拟合到一些动物运动数据。
但是,当我的数据的多行具有完全相同的 x 和 y 坐标值(动物没有移动)时,建模函数会引入 0 表示步长,引入 NA 表示角度。 HMM 的所得系数在生物学解释方面是一致的,但当涉及到进一步绘图时,由于这些 Na,我无法做到这一点。
为了适应 HMM,我使用 R 中的
moveHMM
库。
> packageVersion("moveHMM")
[1] ‘1.9’
> sessionInfo()
R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22000)
我的数据集结构如下:
> str(data)
'data.frame': 16515 obs. of 3 variables:
$ ID: num 1 1 1 1 1 1 1 1 1 1 ...
$ x : num 582629 582629 582628 582628 582628 ...
$ y : num -2195359 -2195359 -2195358 -2195358 -2195358 ...
然后,按照包指南 (https://cran.r-project.org/web/packages/moveHMM/vignettes/moveHMM-guide.pdf),我转换数据以计算步长和角度:
# Convert data to moveData object using prepData
move_data <- prepData(data, type = "UTM", coordNames = c("x", "y"))
接下来是Na的介绍:
> print(na_count_per_column)
ID step angle x y
0 1 1010 0 0
接下来是建模:
# Fit the HMM with the adjusted parameters
hmm_model <- fitHMM(data = move_data, nbStates = 3, stepPar0 = stepPar0, anglePar0 = anglePar0, formula = ~1)
并给出公平的结果:
> print(hmm_model)
Value of the maximum log-likelihood: 27526.22
Step length parameters:
----------------------
state 1 state 2 state 3
mean 3.426185e-15 8.306167e-02 4.9993906
sd 8.356259e+09 1.131159e-01 1.0002724
zero-mass 1.000000e+00 2.256299e-09 0.4078989
Turning angle parameters:
------------------------
state 1 state 2 state 3
mean 0.001312714 0.001319986 -3.313687e-10
concentration 2.827507199 22.348484965 1.000000e+01
Regression coeffs for the transition probabilities:
--------------------------------------------------
1 -> 2 1 -> 3 2 -> 1 2 -> 3 3 -> 1 3 -> 2
intercept -6.914171 -37.97005 -9.650105 -25.74398 -0.4295815 -1.285255
Transition probability matrix:
-----------------------------
[,1] [,2] [,3]
[1,] 9.990074e-01 0.000992619 3.231352e-17
[2,] 6.441467e-05 0.999935585 6.599375e-12
[3,] 3.376541e-01 0.143501890 5.188441e-01
Initial distribution:
--------------------
[1] 2.674974e-10 1.000000e+00 1.316610e-14
然后问题就出现了:
> plot(hmm_model, type = "states")
Decoding states sequence... DONE
Error in if (maxdens > ymax & maxdens < 1.5 * ymax) { :
missing value where TRUE/FALSE needed
在套件中提供的
elk data
上尝试该方法时,绘图成功。尽管如此,也有一些 NA 被引入。
> print(na_count_per_column)
ID step angle x y dist_water
0 4 10 0 0 0
最后,正如包装指南中所述,
在moveHMM包内,将自动实现零通胀 如果步数为零,则包括在内。
如果您的数据不太敏感(并且我要建议的操作也不太难处理),一些预处理可能会解决这个问题。
在整个数据集上运行的卷积核可以应用少量的 PRNG 布朗噪声/运动。这将防止绝对零运动的不连续性。
R 确实支持卷积核。