STAN と R で単純な Gamma GLM を試していますが、すぐにクラッシュします。
データを生成します:
set.seed(1)
library(rstan)
N<-500 #sample size
dat<-data.frame(x1=runif(N,-1,1),x2=runif(N,-1,1))
#the model
X<-model.matrix(~.,dat)
K<-dim(X)[2] #number of regression params
#the regression slopes
betas<-runif(K,-1,1)
shape <- 10
#simulate gamma data
mus<-exp(X%*%betas)
y<-rgamma(500,shape=shape,rate=shape/mus)
これは私の STAN モデルです:
model_string <- "
data {
int<lower=0> N; //the number of observations
int<lower=0> K; //the number of columns in the model matrix
matrix[N,K] X; //the model matrix
vector[N] y; //the response
}
parameters {
vector[K] betas; //the regression parameters
real<lower=0, upper=1000> shape; //the shape parameter
}
model {
y ~ gamma(shape, (shape/exp(X * betas)));
}"
このモデルを実行すると、R がすぐにクラッシュします。
m <- stan(model_code = model_string, data = list(X=X, K=3, N=500, y=y), chains = 1, cores=1)
更新: X のすべての列をベクトルとして渡す実行中のモデルを取得できるため、問題はベクトル化のどこかにあると思います。
update2 : これも機能します
for(i in 1:N)
y[i] ~ gamma(shape, (shape / exp(X[i,] * betas)));