Short summary of the problem
I am trying to run a multilevel model (Bayesian hierarchical model with non-informative priors) in rstan and have used previously successful code as an example for the creation of this model, but keep getting the following error:
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘717474c1587c8a50a0c1ff7237d9d5da’ due to the above error.
There is no line information or other information to help me solve the issue.
The data for this model is not currently publically available, but a simulation of the data is available in the next chunk.
The code I am using is below:
BhLm_Code <- "
data{
int<lower=0> n; // number of data points
int<lower=0> ngr1; // number of groups in group 1
real y[n]; // withdrawal values
real x[n]; // year
int<lower=0, upper=ngr1> group1[n]; // grouping IDs for 1st grouping
}
parameters{
real alpha; // intercept
real beta; // slope
vector[ngr1] alpha_g1; // group-level influence on alpha for group 1
vector[ngr1] beta_g1; // group-level influence on beta for group 1
real<lower=0,upper=100> sigmay;
real<lower=0> sigmaa1;
real<lower=0> sigmab1;
}
transformed parameters{
vector[n] mu;
for (i in 1:n){
mu[i]=(alpha_g1[group1[i]]) + ((beta_g1[group1[i]]) * (x[i])));
}
}
model{
alpha_g1~normal(alpha, sigmaa1);
beta_g1~normal(beta, sigmab1);
y[i]~normal(mu[i], sigmay);
}
"
infile<-With_AQU
response<-"with_calib_mgd_log"
Year<-"Year"
Group1<-"ic_site_id"
y <- infile[,response]
temp <- !is.na(y)
##getting rows with a non n/a response
infile <- infile[temp,]
##removing n/a rows from dataframe
y <- y[temp]
##getting responses from updated dataframe
x <- infile[,Year]
##getting year/predictor from updated dataframe
Group1o<-infile[,Group1]
group1O <- ordered(Group1o)
group1 <- as.numeric(group1O)
##getting numeric list for group 1
n <- length(y)
n.gr1 <- max(group1)
BhLm_data <- list(n=n, ngr1=n.gr1, y=y, x=x, group1=group1)
BhLm_inits <- list()
for (i in 1:4)
BhLm_inits[[i]]<- list(alpha=rnorm(1), beta=runif(1), alpha_g1=rep(0, n.gr1),
beta_g1=rep(0, n.gr1),
sigmay=runif(1, 0, 3), sigmaa1=runif(1), sigmab1=runif(1) )
BhLm_pars <- c("alpha", "beta", "alpha_g1", "beta_g1", "sigmay", "sigmaa1","sigmab1")
fit_b <- stan(model_code = BhLm_Code, data = BhLm_data, iter = 1, chains = 4, init = BhLm_inits, pars = BhLm_pars, control = list(adapt_delta = 0.91, max_treedepth=12))
print(fit_b)
A simulation of the dataset for this code can be made with:
ic_site_id<-c(rep(1, 20), rep(2,20), rep(3,20), rep(4,20), rep(5,20))
Year<-rep(1991:2010, 5)
With_AQU<-data.frame(Year=Year, ic_site_id=ic_site_id)
With_AQU$with_calib_mgd_log<-log(1+(Year*0.2))
Thank you for any and all help!
~Stephanie