【问题标题】:Overcome the error: Initialization failed in rstan::sampling()克服错误:rstan::sampling()中的初始化失败
【发布时间】:2019-07-02 08:31:31
【问题描述】:

rstan::sampling() 失败并出现以下错误。

[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
[1] "error occurred during calling the sampler; sampling not done"
Stan model 'foo' does not contain samples.
Stan model 'foo' does not contain samples.
Stan model 'foo' does not contain samples.
Stan model 'foo' does not contain samples.

其中,foo 表示 stan 文件的名称 foo.stan。

【问题讨论】:

  • 请用示例数据和所需输出详细说明您的问题
  • 我们真的可以在这里使用minimal reproducible example ...
  • 感谢您的回复,@SaiPrabhanjanReddy,我尝试制作上述可重现的示例,请稍候。说实话,这是几周前的错误,我忘记了如何复制它。
  • 谢谢@BenBolker,这发生在我在 CRAN 上的包中,所以,我尝试编写代码以产生上述错误。

标签: r stan rstan


【解决方案1】:

不一致且因此无法初始化的可重现示例:

model <- 
  stan_model(model_code = "parameters { real<lower = 0> y; }
                           transformed parameters { real<upper = -1> z = y; }")

fit <- sampling(model)

这会在 RStan 2.18.1 中产生以下输出:

> fit <- sampling(model)

SAMPLING FOR MODEL '64719d6dccb64c32b0d897ef1f340d74' NOW (CHAIN 1).
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
error occurred during calling the sampler; sampling not done

这是生成的 fit 结构的开头:

> str(fit)
Formal class 'stanfit' [package "rstan"] with 10 slots
  ..@ model_name: chr "64719d6dccb64c32b0d897ef1f340d74"
  ..@ model_pars: chr [1:3] "y" "z" "lp__"
  ..@ par_dims  :List of 3
  .. ..$ y   : num(0) 
  .. ..$ z   : num(0) 
  .. ..$ lp__: num(0) 
  ..@ mode      : int 2
  ..@ sim       : list()

所以你可以使用测试

length(fit@sim) == 0

如果有样本,它们将在变量sim 中显示为列表。

警告:这个问题经常出现,因为没有很好地对参数进行约束。 Stan 假设满足约束的参数的每个值在模型中都有支持。如果没有,模型通常将无法初始化。最好的解决方案是固定参数的约束条件和参数的尺度,使模型可以随机初始化。如果约束条件制定得当,数值问题也会导致问题。将参数缩放到单位比例会有所帮助。此外,大间隔均匀先验可能是一个问题,因为初始化将在中间点附近。如果问题得到很好的扩展并且约束条件很好,那么您可以减少初始化的间隔,这会有所帮助。否则,您真的必须提供自己的合理 init 才能使事情变得健壮。如果 100 次尝试已经失败,则重试通常不会起作用。

【讨论】:

  • 谢谢。我更改了.stan 文件,使参数块中的上限和下限在parametere block 和transformed paramter block 之间兼容,然后我可以自行删除上述错误,不再需要表示错误的R 对象。非常感谢你。发布这个问题对我有好处,因为我可以获得您富有成效的答案。谢谢。
猜你喜欢
  • 2022-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 2015-04-19
相关资源
最近更新 更多