Rstanarm: mgcv::betar family

The release news for rstanarm 2.17.2 state that
"* The mgcv::betar family is supported, allowing for beta
regressions with lme4-style group terms …"

I read this to mean that i can use “betar” as a familiy when calling stan_glmer. This does however not work.

What is the correct comment to use the mgcv::betar family in a stan_glmer model?

Best, Guido

##############################
######## SESSION INFO: ########
##############################

R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Norwegian Bokmål_Norway.1252 LC_CTYPE=Norwegian Bokmål_Norway.1252 LC_MONETARY=Norwegian Bokmål_Norway.1252
[4] LC_NUMERIC=C LC_TIME=Norwegian Bokmål_Norway.1252

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

other attached packages:
[1] rstanarm_2.17.2 Rcpp_0.12.14 rstan_2.17.2 StanHeaders_2.17.1 ggplot2_2.2.1

loaded via a namespace (and not attached):
[1] lattice_0.20-35 zoo_1.8-0 gtools_3.5.0 assertthat_0.2.0 digest_0.6.13 mime_0.5
[7] R6_2.2.2 plyr_1.8.4 stats4_3.4.1 colourpicker_1.0 rlang_0.1.4 lazyeval_0.2.1
[13] minqa_1.2.4 miniUI_0.1.1 nloptr_1.0.4 Matrix_1.2-12 DT_0.2 shinythemes_1.1.1
[19] splines_3.4.1 shinyjs_0.9.1 lme4_1.1-14 stringr_1.2.0 htmlwidgets_0.9 loo_1.1.0
[25] igraph_1.1.2 munsell_0.4.3 shiny_1.0.5 compiler_3.4.1 httpuv_1.3.5 pkgconfig_2.0.1
[31] base64enc_0.1-3 rstantools_1.3.0 htmltools_0.3.6 tibble_1.3.4 gridExtra_2.3 codetools_0.2-15
[37] threejs_0.3.1 matrixStats_0.52.2 dplyr_0.7.4 MASS_7.3-47 grid_3.4.1 nlme_3.1-131
[43] xtable_1.8-2 gtable_0.2.0 magrittr_1.5 scales_0.5.0 stringi_1.1.6 reshape2_1.4.3
[49] bindrcpp_0.2 dygraphs_1.1.1.4 xts_0.10-0 tools_3.4.1 glue_1.2.0 shinystan_2.4.0
[55] markdown_0.8 crosstalk_1.0.0 survival_2.41-3 rsconnect_0.8.5 parallel_3.4.1 inline_0.3.14
[61] colorspace_1.3-2 bayesplot_1.4.0 bindr_0.1

It seems to accept the syntax but have difficult initializing the model. I will look into it more closely.

I tried I again and now it looks as if it works when I first load the mgcv package.

I am having issues with this regression as well. The model works and converges, but when I call posterior_predict on it, I get an “Error in exp(eta) : non-numeric argument to mathematical function” error. Any clues about what might be going on here?

It is a bug. To work around it, first draw from the posterior distribution of the conditional mean

mu <- posterior_linpred(fit, transform = TRUE)

then dig out the posterior distribution of the other parameter

phi <- as.data.frame(fit)$`(phi)`

then draw from a beta distribution to get the posterior predictive

PPD <- matrix(rbeta(prod(dim(mu)), shape1 = mu * phi, shape2 = (1 - mu) * phi),
                         nrow = nrow(mu), ncol = ncol(mu))
dimnames(PPD) <- dimnames(mu)

Thanks, this worked!

An easier short term solution I think is this:

class(fit) <- c(class(fit), "betareg")
posterior_predict(fit) # No error

Full example in my comment on GitHub