rmcelreath / glmer2stan

Define Stan models using glmer-style (lme4) formulas
55 stars 11 forks source link

Inability to parse within formula variable transformations #3

Open jknowles opened 10 years ago

jknowles commented 10 years ago

First of all, thanks for this tremendous work. This is a huge step forward in increasing the accessibility of building Bayesian mixed-effect models efficiently and effectively.

I'm not sure that it is necessary to support the feature of within formula variable transformation, but it should be documented that inline variable transformations do not always compile as expected. Here is a simple example:

library(lme4)
library(rstan)
library(glmer2stan)
data(sleepstudy) # built into lme4

# fit with lme4
m1_lme4 <- lmer( Reaction ~ Days + (Days | Subject), sleepstudy, REML=FALSE )

# construct subject index --- glmer2stan forces you to manage your own cluster indices
sleepstudy$subject_index <- as.integer(as.factor(sleepstudy$Subject))

# fit with glmer2stan
m1_g2s <- lmer2stan( Reaction ~ Days + (Days | subject_index), data=sleepstudy )

# add squared terms

m2_lme4 <- lmer( Reaction ~ Days + I(Days^2) + (Days | Subject), sleepstudy, REML=FALSE )

m2_g2s <- lmer2stan( Reaction ~ Days + I(Days^2) + (Days | subject_index), data=sleepstudy )

In this case, m2_g2s fails to compile:

Starting Stan model

TRANSLATING MODEL 'Reaction ~ Days + I(Days^2) + (Days | subject_index) [gaussian]' FROM Stan CODE TO C++ CODE NOW.
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'Reaction ~ Days + I(Days^2) + (Days | subject_index) [gaussian]' with error message:
EXPECTATION FAILURE LOCATION: file=input; line=5, column=15

    real IDays^2[N];
              ^-- here

DIAGNOSTIC(S) FROM PARSER:
Parser expecting: ";"

And here is the sessionInfo(), though I have also tested on a 64bit SLES platform and found the same results:

R version 3.0.2 (2013-09-25)
Platform: i386-w64-mingw32/i386 (32-bit)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.0.1      glmer2stan_0.994 inline_0.3.13    Rcpp_0.10.6      devtools_1.4.1  
[6] lme4_1.0-5       Matrix_1.1-0     lattice_0.20-24 

loaded via a namespace (and not attached):
 [1] codetools_0.2-8 digest_0.6.4    evaluate_0.5.1  grid_3.0.2      httr_0.2       
 [6] MASS_7.3-29     memoise_0.1     minqa_1.2.1     nlme_3.1-113    parallel_3.0.2 
[11] RCurl_1.95-4.1  splines_3.0.2   stats4_3.0.2    stringr_0.6.2   tools_3.0.2    
[16] whisker_0.3-2  

I don't think this is a bug -- just a fact that should be documented at some point.

rmcelreath commented 10 years ago

Thanks for this. I'll make a note for myself to add at least an informative error message. I need to deal with index (cluster) variable issues as well, so I'll aim to get them both done in the next publish.