2021年3月12日星期五

R Stan: "Error in sampler$call_sampler(args_list[[i]])

I am modeling the following distribution using Stan -

enter image description here

Here is my code -

library("rstan")  set.seed(8889)    model_default_prior = "  data {  int<lower=0> N;  int<lower=0> number_of_counties;  real <lower = 0> radon[N];  int <lower = 0> floor_[N];  int <lower = 0> county[N];  }  parameters {  real<lower=0> eta;  real<lower=0> nu;  real<lower=0> tau;  real<lower=0> mu;  real<lower=0> sigma_sq[county[N]];  real<lower=0> theta[county[N]];  }    model {  nu~exponential(0.1);//Check this  eta~exponential(0.1);//Check this  tau~cauchy(0,1);  mu~normal(0,100);  for(k in 1:N){  sigma_sq~gamma(nu/2, nu*eta/2);  theta~normal(mu, tau^2);  }  for(k in 1:N){    radon~normal(theta[k], sigma_sq[k]);}  }  "  radon.dat = list(    radon = radon$radon,    floor_ = radon$floor,    county = as.integer(factor(radon$county)),    number_of_counties = max(unique(as.integer(factor(radon$county)))),    N = nrow(radon)  )  m = stan_model(model_code = model_default_prior)  r.d = sampling(    m,    radon.dat,    c("nu", "eta", "tau", "mu",      "sigma_sq", "theta"),    iter = 10000,    control = list(adapt_delta = 0.90)  )   r.d  

Here is the error -

SAMPLING FOR MODEL '78f7512ebd3a6d8abb93c2893a92158a' NOW (CHAIN 1). Chain 1: Unrecoverable error evaluating the log probability at the initial value. Chain 1: Exception: []: accessing element out of range. index 86 out of range; expecting index to be between 1 and 85; index position = 1sigma_sq (in 'model55a8525e1ef6_78f7512ebd3a6d8abb93c2893a92158a' at line 29)

1 "Error in sampler$call_sampler(args_list[[i]]) : "
2 " Exception: []: accessing element out of range. index 86 out of range; expecting index to be between 1 and 85; index position = 1sigma_sq (in 'model55a8525e1ef6_78f7512ebd3a6d8abb93c2893a92158a' at line 29)" 1 "error occurred during calling the sampler; sampling not done"

The top 20 rows of my dataset for reference are as follows -

enter image description here

https://stackoverflow.com/questions/66609473/r-stan-error-in-samplercall-samplerargs-listi March 13, 2021 at 10:06AM

没有评论:

发表评论