stan-dev / rstanarm

rstanarm R package for Bayesian applied regression modeling
https://mc-stan.org/rstanarm
GNU General Public License v3.0
389 stars 134 forks source link

offsets do not work correctly in stan_gamm4 #253

Open wds15 opened 6 years ago

wds15 commented 6 years ago

Summary:

The offsets don't work right in stan_gamm4. The example here is for a gaussian family, but it is particularly harmful in poisson regression.

Description:

Specifying an offset inside the formula or as argument to the function does not work if the offset referred to is a column inside the data frame. This is also problematic when predicting.

Reproducible Steps:

## example from gamm
library(rstanarm)

dat <- mgcv::gamSim(1, n = 400, scale = 2) ## simulate 4 term additive truth
#> Gu & Wahba 4 term additive model
## Now add 20 level random effect `fac'...
dat$fac <- fac <- as.factor(sample(1:20, 400, replace = TRUE))
dat$y <- dat$y + model.matrix(~ fac - 1) %*% rnorm(20) * .5

br <- stan_gamm4(y ~ s(x0) + x1 + s(x2) + offset(f2), data = dat, random = ~ (1 | fac),
                 chains = 1, iter = 200) # for example speed

RStanARM Version:

2.17.2

R Version:

3.4.3

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] de_CH.UTF-8/de_CH.UTF-8/de_CH.UTF-8/C/de_CH.UTF-8/de_CH.UTF-8

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

other attached packages:
[1] rstanarm_2.17.2 Rcpp_0.12.14   

loaded via a namespace (and not attached):
 [1] lattice_0.20-35    zoo_1.8-0          gtools_3.5.0       assertthat_0.2.0  
 [5] digest_0.6.12      mime_0.5           R6_2.2.2           plyr_1.8.4        
 [9] stats4_3.4.3       ggplot2_2.2.1      colourpicker_0.3   rlang_0.1.2       
[13] lazyeval_0.2.0     minqa_1.2.4        miniUI_0.1.1       nloptr_1.0.4      
[17] Matrix_1.2-12      DT_0.2             shinythemes_1.1.1  splines_3.4.3     
[21] shinyjs_0.9.1      lme4_1.1-13        stringr_1.2.0      htmlwidgets_0.9   
[25] loo_1.1.0          igraph_1.1.2       munsell_0.4.3      shiny_1.0.5       
[29] compiler_3.4.3     httpuv_1.3.5       rstan_2.17.3       pkgconfig_2.0.1   
[33] base64enc_0.1-3    mgcv_1.8-22        rstantools_1.3.0   htmltools_0.3.6   
[37] tibble_1.3.4       gridExtra_2.3      codetools_0.2-15   threejs_0.3.1     
[41] matrixStats_0.52.2 dplyr_0.7.3        MASS_7.3-47        grid_3.4.3        
[45] nlme_3.1-131       xtable_1.8-2       gtable_0.2.0       magrittr_1.5      
[49] StanHeaders_2.17.2 scales_0.5.0       stringi_1.1.5      reshape2_1.4.2    
[53] bindrcpp_0.2       dygraphs_1.1.1.4   xts_0.10-0         tools_3.4.3       
[57] glue_1.1.1         shinystan_2.4.0    markdown_0.8       crosstalk_1.0.0   
[61] rsconnect_0.8.5    parallel_3.4.3     survival_2.41-3    inline_0.3.14     
[65] colorspace_1.3-2   bayesplot_1.4.0    bindr_0.1         

Operating System:

Your operating system (e.g., OS X 10.11.3)

jgabry commented 6 years ago

Thanks for reporting. Not sure what the problem is but will look into it. It might work if you use the offset argument instead of putting it in the formula.

caimiao0714 commented 5 years ago

This offset problem still exists in the current version of rstanarm:

require(rstanarm)

fit = stan_gamm4(carb ~ gear + s(mpg), offset = log(disp), 
                 family=poisson(), 
            data = mtcars, chains = 1, iter = 200)

> fit = stan_gamm4(carb ~ gear + s(mpg), offset = log(disp), 
+                  family=poisson(), 
+             data = mtcars, chains = 1, iter = 200)

Error in stan_glm.fit(x = X, y = y, weights = weights, offset = offset,  : 
  formal argument "offset" matched by multiple actual arguments

My rstanarm and rstan version is 2.18.2. My R version is 3.6.0.