RStan v2.8.2でエラーが発生する場合の対処法
(2015.1.7追記) このポストとほぼ同時にRSrtan v2.9.0がCRANに上がってた。。。orz
久しぶりにRStanを実行しようとしたら色々ハマったのでメモしとく。
(@sinhrks氏と@hoxo_m親分に大感謝)
環境
- MacBook Pro (15-inch, Late 2011)
- OS X EI Capitan(10.11.2)
- R version 3.2.3 (2015-12-10)
エラー内容
基礎からのベイズ統計学: ハミルトニアンモンテカルロ法による実践的入門
- 作者: 豊田秀樹
- 出版社/メーカー: 朝倉書店
- 発売日: 2015/06/25
- メディア: 単行本
- この商品を含むブログ (4件) を見る
の6章のコードを実行すると"stan/services/init/init_adapt.hpp"が見つからなくてエラーになる。
> fit <- stan(file=scr, data=data, iter=ite, seed=see, warmup=war, + pars=par,chains=cha) (中略) compileCode(f, code, language = language, verbose = verbose) でエラー: Compilation ERROR, function(s)/method(s) not created! clang: warning: optimization flag '-ffat-lto-objects' is not supported clang: warning: argument unused during compilation: '-ffat-lto-objects' In file included from file7843c269091.cpp:444: In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rstan/include/rstan/rstaninc.hpp:3: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rstan/include/rstan/stan_fit.hpp:69:10: fatal error: 'stan/services/init/init_adapt.hpp' file not found #include <stan/services/init/init_adapt.hpp> ^ 1 error generated. make: *** [file7843c269091.o] Error 1 追加情報: 警告メッセージ: 命令 '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file7843c269091.cpp 2> file7843c269091.cpp.err.txt' の実行は状態 1 を持ちました
githubには既にissueとして認識されていてv2.9.0で修正されるみたい。
move src/stan/services · Issue #1616 · stan-dev/stan · GitHub
対処方法
RStan / PyStan 開発版を GitHub からインストールする - StatsFragments
を参考にgithubのコードをコンパイルしてインストールする。
git clone git://github.com/stan-dev/stan.git git clone git://github.com/stan-dev/rstan.git cp -R stan rstan/stan vi makefile make build make install
上記リンク先の
自分は TeX 設定してないので、vignette, manual をビルドしないように設定 ( makefile 108 行目のコメントアウトを外す)。
は2016.1.5時点では66行目になっていた。
動作確認
> fit <- stan(file=scr, data=data, iter=ite, seed=see, warmup=war, + pars=par,chains=cha) SAMPLING FOR MODEL 'model611' NOW (CHAIN 1). Chain 1, Iteration: 1 / 11000 [ 0%] (Warmup) Chain 1, Iteration: 1001 / 11000 [ 9%] (Sampling) Chain 1, Iteration: 2100 / 11000 [ 19%] (Sampling) Chain 1, Iteration: 3200 / 11000 [ 29%] (Sampling) Chain 1, Iteration: 4300 / 11000 [ 39%] (Sampling) Chain 1, Iteration: 5400 / 11000 [ 49%] (Sampling) Chain 1, Iteration: 6500 / 11000 [ 59%] (Sampling) Chain 1, Iteration: 7600 / 11000 [ 69%] (Sampling) Chain 1, Iteration: 8700 / 11000 [ 79%] (Sampling) Chain 1, Iteration: 9800 / 11000 [ 89%] (Sampling) Chain 1, Iteration: 10900 / 11000 [ 99%] (Sampling) Chain 1, Iteration: 11000 / 11000 [100%] (Sampling)# # Elapsed Time: 0.107373 seconds (Warm-up) # 0.207752 seconds (Sampling) # 0.315125 seconds (Total) #
無事に動いた!
からの罠orz
> traceplot(fit, inc_warmup=F) if (debug) { でエラー: 引数の長さが 0 です > plot(fit) ci_level: 0.8 (80% intervals) outer_level: 0.95 (95% intervals) if (debug) { でエラー: 引数の長さが 0 です
R で関数のデフォルト引数を変更する(魔改造編) #rstatsj - Qiitaによるとrstanがggplot2のバージョンアップに対応できていないとのこと。
この件については@hoxo_m 親分が既に魔改造以外のワークアラウンドを用意してくれている。
rstan 2.8.2 - bug in traceplot under Windows · Issue #247 · stan-dev/rstan · GitHub
devtools::install_github("hoxo-m/fixer") fixer::fix_params_persist(ggplot2:::titleGrob(debug = FALSE))
上記のワークアラウンド実行後にtraceplot, plotをそれぞれ実行すると正常に表示された。
> traceplot(fit, inc_warmup=F)
> plot(fit)