300億円欲しい

メジャーリーグのデータ解析します

Rstanを動かしたいけど動かないと思ったら動いた

StanでMCMCサンプリングをしたいです.

Rstanを動かしたい

データ解析のための統計モデリング入門を読んでいます.

この本の計算をRstanでやる素敵な人がいました.
R: RStanをためす (2):Taglibro de H:So-netブログ
Stanを使ってMCMCがしたいので, 使ってみたいのですが動きません.

リンク先からコードを拝借しました. ありがとうございます.
MacOSXで実行しました.

## Kubo Book Chapter 9

library(rstan)

## load data
load(url("http://hosho.ees.hokudai.ac.jp/~kubo/stat/iwanamibook/fig/gibbs/d.RData"))

model <- '
data {
int<lower=0> N;     // number of data
real X[N];          // explanatory variable
real MeanX;         // mean of X
int<lower=0> Y[N];  // response variable
}
parameters {
real beta1;
real beta2;
}
transformed parameters {
real<lower=0> lambda[N];

for (i in 1:N) {
lambda[i] <- exp(beta1 + beta2 * (X[i] - MeanX));
}
}
model {
for (i in 1:N) {
Y[i] ~ poisson(lambda[i]);
}
beta1 ~ normal(0, 100);
beta2 ~ normal(0, 100);
}'

data <- list(N = nrow(d),
             X = d$x,
             Y = d$y,
             MeanX = mean(d$x))

fit <- stan(model_code = model, data = data, 
            warmup = 100, iter = 1600, chains = 3, verbose=TRUE)

RからC++でmodelを書いて速くMCMCができるらしいです.
しかし, このようなエラーが

> fit <- stan(model_code = model, data = data, 
+             warmup = 100, iter = 1600, chains = 3, verbose=TRUE)

TRANSLATING MODEL 'model' FROM Stan CODE TO C++ CODE NOW.
 以下にエラー stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'model' with error message:
EXPECTATION FAILURE LOCATION: file=input; line=25, column=2

}
 ^-- here


DIAGNOSTIC(S) FROM PARSER:
Parser expecting: "*/"

hereじゃねえよ. どこだよ.
model部分の最後のところがおかしい, と言われています. いやいや.
他のサンプルコードでも, 同様のエラーメッセージを頂きます.
C++コンパイラが変?

RcppでC++コードを実行することはできています.
どうすればいいんでしょうね.

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8

attached base packages:
[1] grDevices datasets  parallel  splines   graphics  utils     stats     methods   base     

other attached packages:
 [1] rstan_2.2.0        inline_0.3.13      Rcpp_0.11.0        pingr_0.1          RPostgreSQL_0.4    DBI_0.2-7          reshape_0.8.4     
 [8] plyr_1.8           gbm_2.1            lattice_0.20-24    survival_2.37-7    randomForest_4.6-7 stringr_0.6.2      dplyr_0.1.1       
[15] data.table_1.8.10  ggplot2_0.9.3.1   

loaded via a namespace (and not attached):
 [1] assertthat_0.1.0.99 audio_0.1-5         colorspace_1.2-4    dichromat_2.0-0     digest_0.6.4        grid_3.0.2          gtable_0.1.2       
 [8] labeling_0.2        MASS_7.3-29         munsell_0.4.2       proto_0.3-10        RColorBrewer_1.0-5  reshape2_1.2.2      scales_0.2.3       
[15] stats4_3.0.2        tools_3.0.2        

動いた

RStan Getting Started · stan-dev/rstan Wiki · GitHub

Special Steps for Xcode 5.0.1 + R 3.0.2 + Mac OS X 10.9 (Mavericks)

To use the latest tools

Mac OS X version 10.9 (aka Mavericks),
Xcode toolset version 5.0.1 including the latest command-line tools for Mavericks (now a separate download from Xcode), and
R version 3.0.2 (installed in the usual way from the precompiled binary .pkg file),
You need to create or edit the file

~/.R/Makevars
so that its content is exactly

CXX=g++ -arch x86_64 -ftemplate-depth-256 -stdlib=libstdc++
before proceeding. Without the architecture and library link, the library will build but can't be linked to R.

はい...

設定したらちゃんとStanできました...