paul-buerkner / brms

brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
https://paul-buerkner.github.io/brms/
GNU General Public License v2.0
1.27k stars 182 forks source link

predict not working with negative binomial data and ARMA errors #1149

Closed tbuehrens closed 3 years ago

tbuehrens commented 3 years ago

I saw some of the past issues with prediction (forecasting) with arma errors. However, it appeared these had been fixed. Nonetheless, the predict function is throwing the following error: "Error in eval(substitute(expr), data, enclos = parent.frame()) : object 'begin_tg' not found" when running the code below. Specifically, both mod and mod2 will fit, and the predict function works for mod2 (the normal model), but does not work for the negative binomial model and instead throws the error.

sims<-50
int = 11.3
b = 0.3
var<-rnorm(n=sims,0,1)
resid<-arima.sim(list(ar = c(0.7), ma = c(-0.3), sd = 0.5),n=sims)
ypred <- int + b * var + resid
y <- rnegbin(n=sims,mu=exp(ypred),theta = 6)
dat <- data.frame(ypred=ypred,y = as.numeric(y),var=var)

mod<- brm(y ~ var + arma(p = 1, q = 1, cov=TRUE)
          ,family = negbinomial(link="log")
          ,data = dat
          ,chains = 4
          ,cores = 4
          ,iter = 4000
          ,warmup = 3000
          ,control=list(adapt_delta = 0.95)
)

mod2<- brm(ypred ~ var + arma(p = 1, q = 1, cov=TRUE)
          ,data = dat
          ,chains = 4
          ,cores = 4
          ,iter = 4000
          ,warmup = 3000
          ,control=list(adapt_delta = 0.95)
)

newdat<-data.frame(matrix(NA,ncol=3,nrow=1))
colnames(newdat)<-colnames(dat)
newdat$ypred<-NA
newdat$y<-NA
newdat$var<-0

predict(mod,newdata = dat)
predict(mod2,newdata = dat)

sessionInfo: R version 4.0.3 (2020-10-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

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

other attached packages: [1] brms_2.15.0 Rcpp_1.0.5 RColorBrewer_1.1-2 MuMIn_1.43.17 fpp_0.5
[6] tseries_0.10-48 lmtest_0.9-38 zoo_1.8-9 expsmooth_2.3 fma_2.4
[11] MASS_7.3-53.1 mgcv_1.8-34 nlme_3.1-152 forecast_8.14 forcats_0.5.0
[16] stringr_1.4.0 dplyr_1.0.5 purrr_0.3.4 readr_1.3.1 tidyr_1.1.1
[21] tibble_3.0.3 ggplot2_3.3.3 tidyverse_1.3.0

loaded via a namespace (and not attached): [1] minqa_1.2.4 colorspace_1.4-1 ellipsis_0.3.1 ggridges_0.5.3
[5] rsconnect_0.8.16 estimability_1.3 markdown_1.1 base64enc_0.1-3
[9] fs_1.5.0 rstudioapi_0.13 rstan_2.21.2 DT_0.17
[13] fansi_0.4.1 mvtnorm_1.1-1 lubridate_1.7.9 xml2_1.3.2
[17] codetools_0.2-18 bridgesampling_1.0-0 splines_4.0.3 shinythemes_1.2.0
[21] bayesplot_1.8.0 projpred_2.0.2 jsonlite_1.7.2 nloptr_1.2.2.2
[25] broom_0.7.5 dbplyr_2.1.0 shiny_1.6.0 compiler_4.0.3
[29] httr_1.4.2 emmeans_1.5.4 backports_1.1.7 assertthat_0.2.1
[33] Matrix_1.3-2 fastmap_1.1.0 cli_2.3.1 later_1.1.0.1
[37] prettyunits_1.1.1 htmltools_0.5.1.1 tools_4.0.3 igraph_1.2.6
[41] coda_0.19-4 gtable_0.3.0 glue_1.4.1 reshape2_1.4.4
[45] V8_3.4.0 cellranger_1.1.0 fracdiff_1.5-1 vctrs_0.3.6
[49] urca_1.3-0 crosstalk_1.1.1 timeDate_3043.102 ps_1.6.0
[53] lme4_1.1-26 rvest_1.0.0 miniUI_0.1.1.1 mime_0.10
[57] lifecycle_1.0.0 gtools_3.8.2 statmod_1.4.35 scales_1.1.1
[61] colourpicker_1.1.0 hms_1.0.0 promises_1.2.0.1 Brobdingnag_1.2-6
[65] parallel_4.0.3 inline_0.3.17 shinystan_2.5.0 gamm4_0.2-6
[69] quantmod_0.4.18 curl_4.3 gridExtra_2.3 StanHeaders_2.21.0-7 [73] loo_2.4.1 stringi_1.4.6 dygraphs_1.1.1.6 TTR_0.24.2
[77] pkgbuild_1.2.0 boot_1.3-27 rlang_0.4.10 pkgconfig_2.0.3
[81] matrixStats_0.58.0 lattice_0.20-41 rstantools_2.1.1 htmlwidgets_1.5.3
[85] processx_3.4.5 tidyselect_1.1.0 plyr_1.8.6 magrittr_2.0.1
[89] R6_2.5.0 generics_0.1.0 DBI_1.1.1 pillar_1.5.1
[93] haven_2.3.1 withr_2.4.1 xts_0.12.1 abind_1.4-5
[97] nnet_7.3-15 modelr_0.1.8 crayon_1.4.1 utf8_1.2.1
[101] grid_4.0.3 readxl_1.3.1 callr_3.5.1 threejs_0.3.3
[105] reprex_1.0.0 digest_0.6.25 xtable_1.8-4 httpuv_1.5.5
[109] RcppParallel_5.0.3 stats4_4.0.3 munsell_0.5.0 shinyjs_2.0.0
[113] quadprog_1.5-8

paul-buerkner commented 3 years ago

Thanks! Should now be fixed on github.