问题描述
我正在尝试使用depmix将简单的隐藏markov模型拟合到R中.但是我有时会得到一些晦涩的错误(在外部函数调用中为Na/NaN/Inf).例如
I'm trying to fit simple hidden markov models in R using depmix. But I sometimes get obscure errors (Na/NaN/Inf in foreign function call). For instance
require(depmixS4)
t = data.frame(v=c(0.0622031327669583,-0.12564002739468,-0.117354660120178,0.0115062213361335,0.122992418345013,-0.0177816909620965,0.0164821157439354,0.161981367176501,-0.174367935386872,0.00429417498601576,0.00870091566593177,-0.00324734222267713,-0.0609817740148078,0.0840679943325736,-0.0722982123741866,0.00309386232501072,0.0136237132601905,-0.0569072400881981,0.102323872007477,-0.0390675463642003,0.0373248728294635,-0.0839484669503484,0.0514620475651086,-0.0306598076180909,-0.0664992242224042,0.826857872461293,-0.172970803143762,-0.071091459861684,-0.0128631184461384,-0.0439382422065227,-0.0552809574423446,0.0596321725192134,-0.06043926984848,0.0398700063815422))
mod = depmix(response=v~1, data=t, nstates=2)
fit(mod)
...
NA/NaN/Inf in foreign function call (arg 10)
我可以输入几乎相同大小和复杂度的输入...在这里是否有首选的工具来进行depmixS4?
And I can have input of almost identical size and complexity work fine...Is there a preferred tool to depmixS4 here?
推荐答案
不能保证EM算法可以为给定任意数量状态的每个数据集找到合适的位置.例如,如果您尝试将2状态高斯模型拟合到lambda = 1的泊松分布生成的数据,则会收到相同的错误.
There is no guarantee that the EM algorithm can find a fit for every dataset given an arbitrary number of states. For example were you to try fit a 2 state gaussian model to data generated from a Poisson distribution with lambda = 1 you would receive the same error.
set.seed(3)
ydf <- data.frame(y=rpois(100,1))
m1 <- depmix(y~1,ns=2,family=gaussian(),data=ydf)
fit(m1)
iteration 0 logLik: -135.6268
iteration 5 logLik: -134.2392
iteration 10 logLik: -128.7834
iteration 15 logLik: -111.5922
Error in fb(init = init, A = trDens, B = dens, ntimes = ntimes(object), :
NA/NaN/Inf in foreign function call (arg 10)
关于数据,您可以将模型拟合为数据,状态只有1种就可以了.对于2个状态,该算法无法找到解决方案(即使使用10000个随机起始值).对于3个状态,问题似乎在于模型的初始状态的初始化.如果您尝试使用您提供的数据运行同一模型100次,那么您会在100次迭代中获得收敛.下面的示例:
With regards to your data, you can fit a model to your data just fine with 1 state. With 2 states the algorithm cannot find a solution (even with 10000 random starts). For 3 states, the issue seems to be with the initialization of the starting states of the model. If one attempts to run the same model 100 times with the data you provided you get a convergence in some of the 100 iterations. Example below:
>require(depmixS4)
>t = data.frame(v=c(0.0622031327669583,-0.12564002739468,-0.117354660120178,0.0115062213361335,0.122992418345013,-0.0177816909620965,0.0164821157439354,0.161981367176501,-0.174367935386872,0.00429417498601576,0.00870091566593177,-0.00324734222267713,-0.0609817740148078,0.0840679943325736,-0.0722982123741866,0.00309386232501072,0.0136237132601905,-0.0569072400881981,0.102323872007477,-0.0390675463642003,0.0373248728294635,-0.0839484669503484,0.0514620475651086,-0.0306598076180909,-0.0664992242224042,0.826857872461293,-0.172970803143762,-0.071091459861684,-0.0128631184461384,-0.0439382422065227,-0.0552809574423446,0.0596321725192134,-0.06043926984848,0.0398700063815422))
>mod = depmix(response=v~1, data=t, nstates=2)
>fit(mod)
...
NA/NaN/Inf in foreign function call (arg 10)
>replicate(100, try(fit(mod, verbose = F)))
[[1]]
[1] "Error in fb(init = init, A = trDens, B = dens, ntimes = ntimes(object), : \n NA/NaN/Inf in foreign function call (arg 10)\n"
[[2]]
[1] "Error in fb(init = init, A = trDens, B = dens, ntimes = ntimes(object), : \n NA/NaN/Inf in foreign function call (arg 10)\n"
[[3]]
Convergence info: Log likelihood converged to within tol. (relative change)
'log Lik.' 34.0344 (df=14)
AIC: -40.0688
BIC: -18.69975
... output truncated
这篇关于在R中使用depmixS4拟合HMM时,NA/NaN/Inf错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!