r - simple Gamma GLM in STAN -
i'm trying simple gamma glm in stan , r, crashes immediately
generate data:
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)
this stan model:
model_string <- " data { int<lower=0> n; //the number of observations int<lower=0> k; //the number of columns in 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))); }"
when run model, r crashes:
m <- stan(model_code = model_string, data = list(x=x, k=3, n=500, y=y), chains = 1, cores=1)
update : think problem somewhere in vectorization can running model pass every column of x vector.
update2: works
for(i in 1:n) y[i] ~ gamma(shape, (shape / exp(x[i,] * betas)));
the problem original code there no operator defined in stan scalar divided vector. in case, shape / exp(x * betas)
might able shape[1:n] ./ exp(x * betas)
or failing that, (shape * ones_vector) ./ exp(x * betas)
Comments
Post a Comment