"Stan model 'negbin-glm' does not contain samples." error

503 views
Skip to first unread message

malai...@gmail.com

unread,
Sep 20, 2016, 9:39:36 AM9/20/16
to Stan users mailing list
When i run the attached code i am getting an error "Stan model 'negbin-glm' does not contain samples", please help clarifying why I am getting that error and how i can debug it.

Thanks.


# data processing
getwd()
setwd("C:/Users/n01066737/Desktop/Angela")
getwd()
crashdata<-read.csv(file.choose(), header = T)
crashdata
summary(crashdata)
names(crashdata)
str(crashdata)

### converting factor variables to as.factors
crashdata$time_indicator<-as.factor(crashdata$time_indicator)
crashdata$treatment<-as.factor(crashdata$treatment)
crashdata$median_type<-as.factor(crashdata$median_type)
crashdata$landuse<-as.factor(crashdata$landuse)
crashdata$speed<-as.factor(crashdata$speed)

### checking the structure after converting the factor variables

str(crashdata)


### loading the rstan package

library(rstan)

### creating dummy variables for factor variables

cat_time<-model.matrix(~crashdata$time_indicator, data=crashdata)
cat_treatment<-model.matrix(~crashdata$treatment, data=crashdata)
cat_median_type<-model.matrix(~crashdata$median_type, data=crashdata)
cat_landuse<-model.matrix(~crashdata$landuse, data=crashdata)
cat_speed<-model.matrix(~crashdata$speed, data=crashdata)

### specifying the poisson-gamma mixture model

negbin<-"
data{
int<lower=0> N;                           # rows of data
vector[N] cat_time;                       # predictor variable
vector[N] cat_treatment;                  # predictor variable
vector[N] cat_median_type;                # predictor variable
vector[N] cat_landuse;                    # predictor variable
vector[N] cat_speed;                      # predictor variable
vector[N] ln_maj_aadt;                    # predictor variable
vector[N] ln_min_aadt;                    # predictor variable
vector[N] no_lanes_maj;                   # predictor variable
vector[N] no_lanes_min;                   # predictor variable
vector[N] no_lanes_min;                   # predictor variable
vector[N] median_width;                   # predictor variable
int<lower=0> total_crashes[N];            # response variable
}

parameters {
real<lower=0> phi;      # negative binomial dispersion parameter
real beta0;             # intercept
real beta1;
real beta2;
real beta3;
real beta4;
real beta5;
real beta6;
real beta7;
real beta8;
real beta9;
real beta10;
}

model {
### priors
phi ~    cauchy(0.001,0.001);
beta0 ~  normal(0.0, 1.0E4);
beta1 ~  normal(0.0, 1.0E4);
beta2 ~  normal(0.0, 1.0E4);
beta3 ~  normal(0.0, 1.0E4);
beta4 ~  normal(0.0, 1.0E4);
beta5 ~  normal(0.0, 1.0E4);
beta6 ~  normal(0.0, 1.0E4);
beta7 ~  normal(0.0, 1.0E4);
beta8 ~  normal(0.0, 1.0E4);
beta9 ~  normal(0.0, 1.0E4);
beta10 ~ normal(0.0, 1.0E4);
# data model
total_crashes~ neg_binomial_2_log(beta0 + beta1*cat_time + beta2*cat_treatment + beta3*cat_median_type + beta4*cat_landuse + beta5*cat_speed + beta6*ln_maj_aadt + beta7*ln_min_aadt + beta8*no_lanes_maj + beta9*no_lanes_min +beta10*median_width, phi);
}
"
write(negbin, file="negbin-glm.stan")
sm<-stan_model("negbin-glm.stan")
set.seed(1000)
N<- 849
phi<-0.7349047  
beta0<--3.150702
beta1<-0.0479721
beta2<-0.3503469
beta3<-0.0028539
beta4<--0.1059735
beta5<-0.6936886 
beta6<-0.2642703
beta7<-0.121047
beta8<-0.0255357
beta9<-0.1235194
beta10<--0.0093531
expected_crashes<-sampling(sm, data=list(N=N, crashes=crashdata$total_crashes, cat_time=crashdata$time_indicator -mean((crashdata$time_indicator)), cat_treatment=crashdata$treatment-mean((crashdata$treatment)), cat_median_type=crashdata$median-type-mean((crashdata$median-type)), cat_landuse=crashdata$landuse-mean((crashdata$landuse)), cat_speed=crashdata$speed-mean((crashdata$speed)), ln_maj_aadt=crashdata$ln_aadt_maj-mean((crashdata$ln_aadt_maj)), ln_min_aadt=crashdata$ln_min_aadt-mean((crashdata$ln_min_aadt)), no_lanes_maj=crashdata$no_lanes_maj-mean((crashdata$no_lanes_maj)), no_lanes_min=crashdata$no_lanes_min-mean((crashdata$no_lanes_min)), median_width=crashdata$median_width-mean((crashdata$median_width))), pars=c("beta0", "beta1", "beta2", "beta3", "beta4", "beta5", "beta6", "beta7", "beta8", "beta9", "beta10", "phi"), iter=50000, warmup=20000, chains=1)
expected_crashes
# plot the output
rstan::traceplot(expected_crashes, inc_warmup=FALSE)
library(rstanarm)
library(shinystan)
launch_shinystan(expected_crashes)

FULL-BAYES-DATA.csv
pcs_project_1.R

Bob Carpenter

unread,
Sep 20, 2016, 12:58:33 PM9/20/16
to stan-...@googlegroups.com
I'd suggest starting with a simpler example and working up.
Or you can try to use print statements in the existing program
to see where things go wrong if you're not getting a specific
line number out. Test with simulated data on the simpler thing
to make sure it works.

Usually sampling fails due to specific exceptions being
thrown in numerical methods that expect inputs in certain ranges
or will overflow and underflow.

Putting variables on very different scales, as in this:

phi ~ cauchy(0.001,0.001);

can be problematic. Ideally for speed and stability,
parameters posteriors will be roughly normal(0, 1)---otherwise,
we have to rescale during adaptation.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> <FULL-BAYES-DATA.csv><pcs_project_1.R>

Lu Zhang

unread,
Sep 20, 2016, 6:30:53 PM9/20/16
to Stan users mailing list

I agree that the prior "phi ~ Cauchy(0.001,0.001)" could be problematic. It could be too informative.  See the following picture:











And I noticed that the name of your variable in the CSV file contain "." while all the variable names you use in the code have "_". It may cause problems 
Finally, I would recommend you use vector beta instead of setting coefficient one by one. You could use 
data{
    int<lower = 0> N;                               # rows of data
    int<lower = 0> P;                               # colums of design matrix
    matrix[N, P] X;                                  # design matrix
    int<lower=0> total_crashes[N];         # response variable
}
parameters {
real<lower=0> phi;      # negative binomial dispersion parameter
vector[P] Beta;
}

model {
### priors
phi ~    cauchy(0, 0.1);
for (i in 1:P) {
  Beta[i] ~ normal(0.0, 1.0E4);
}
# data model
total_crashes~ neg_binomial_2_log(X*Beta, phi);
}

Best,

Lu Zhang

Angel Edes

unread,
Sep 20, 2016, 7:30:12 PM9/20/16
to stan-...@googlegroups.com
Thank you so much Bob, and Lu.
I will work on you're recommendations.

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/J-o6ea9Ed4I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@googlegroups.com.

malai...@gmail.com

unread,
Sep 21, 2016, 11:32:50 AM9/21/16
to Stan users mailing list
Hi All,

I dropped all the variables in my code and remained with one variable, and when i run it again i received an error

"Error : variable does not exist; processing stage=data initialization; variable name=minaadt; base type=vector_d
failed to create the sampler; sampling not done"

What does this indicate?

Please help

Bob Carpenter

unread,
Sep 21, 2016, 3:50:07 PM9/21/16
to stan-...@googlegroups.com
A variable named minaadt isn't getting a value in the data input
for the program.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

malai...@gmail.com

unread,
Sep 30, 2016, 11:44:36 AM9/30/16
to Stan users mailing list
Thank you Bob,

I was able to figure out the problem.
Reply all
Reply to author
Forward
0 new messages