bbolker / broom.mixed

tidy methods for mixed models in R
227 stars 23 forks source link

tidier for `rjags` models not working #54

Closed IndrajeetPatil closed 5 years ago

IndrajeetPatil commented 5 years ago
# setup
set.seed(123)
library(R2jags)
#> Loading required package: rjags
#> Loading required package: coda
#> Linked to JAGS 4.3.0
#> Loaded modules: basemod,bugs
#> 
#> Attaching package: 'R2jags'
#> The following object is masked from 'package:coda':
#> 
#>     traceplot

# An example model file is given in:
model.file <- system.file(package = "R2jags", "model", "schools.txt")

# Let's take a look:
file.show(model.file)

# data
J <- 8.0
y <- c(28.4, 7.9, -2.8, 6.8, -0.6, 0.6, 18.0, 12.2)
sd <- c(14.9, 10.2, 16.3, 11.0, 9.4, 11.4, 10.4, 17.6)

# setting up model
jags.data <- list("y", "sd", "J")
jags.params <- c("mu", "sigma", "theta")
jags.inits <- function() {
  list("mu" = rnorm(1), "sigma" = runif(1), "theta" = rnorm(J))
}

# model fitting
jagsfit <- R2jags::jags(
  data = list("y", "sd", "J"),
  inits = jags.inits,
  jags.params,
  n.iter = 10,
  model.file = model.file
)
#> module glm loaded
#> Compiling model graph
#>    Resolving undeclared variables
#>    Allocating nodes
#> Graph information:
#>    Observed stochastic nodes: 8
#>    Unobserved stochastic nodes: 10
#>    Total graph size: 41
#> 
#> Initializing model

# checking class
class(jagsfit)
#> [1] "rjags"

# printing output
print(jagsfit)
#> Inference for Bugs model at "C:/Users/inp099/Documents/R/win-library/3.6/R2jags/model/schools.txt", fit using jags,
#>  3 chains, each with 10 iterations (first 5 discarded)
#>  n.sims = 15 iterations saved
#>          mu.vect sd.vect   2.5%    25%    50%    75%  97.5%   Rhat n.eff
#> mu        -0.321   1.373 -1.696 -1.668 -0.771  1.226  1.851  9.327     3
#> sigma      0.710   0.535  0.036  0.088  0.764  1.120  1.437  6.747     3
#> theta[1]  -0.004   1.620 -1.682 -1.627 -0.211  0.685  2.922  3.214     4
#> theta[2]  -0.073   1.654 -2.087 -1.653 -0.100  1.191  2.703  3.419     3
#> theta[3]  -0.613   1.168 -1.764 -1.695 -0.727 -0.133  1.857  2.210     4
#> theta[4]  -0.719   1.237 -1.776 -1.653 -1.622  0.116  1.713  2.520     4
#> theta[5]  -0.439   1.377 -1.715 -1.597 -0.866  0.540  2.181  2.313     4
#> theta[6]  -0.094   1.701 -1.714 -1.586 -0.472  1.173  2.841  4.855     3
#> theta[7]  -0.312   1.571 -1.722 -1.629 -1.041  1.043  2.415  5.438     3
#> theta[8]  -0.556   1.335 -2.465 -1.621 -0.746  0.420  1.584  3.034     4
#> deviance  63.376   1.274 61.435 61.983 63.673 64.674 64.713 10.813     3
#> 
#> For each parameter, n.eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor (at convergence, Rhat=1).
#> 
#> DIC info (using the rule, pD = var(deviance)/2)
#> pD = 0.0 and DIC = 63.4
#> DIC is an estimate of expected predictive error (lower deviance is better).

# getting tidy output
broom.mixed::tidy(
  x = jagsfit,
  conf.int = TRUE,
  conf.method = "quantile"
)
#> Error in character(ncol(y)): invalid 'length' argument

Here is the traceback

> traceback()
6: character(ncol(y))
5: as.matrix.mcmc(x)
4: as.matrix(x)
3: tidyMCMC(as.mcmc(x$BUGS), estimate.method, conf.int, conf.level, 
       conf.method, drop.pars = "deviance")
2: tidy.rjags(x = jagsfit, conf.int = TRUE, conf.method = "quantile")
1: broom.mixed::tidy(x = jagsfit, conf.int = TRUE, conf.method = "quantile")
Session info ``` r devtools::session_info() #> - Session info ---------------------------------------------------------- #> setting value #> version R Under development (unstable) (2018-11-30 r75724) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.1252 #> ctype English_United States.1252 #> tz America/New_York #> date 2019-02-05 #> #> - Packages -------------------------------------------------------------- #> package * version date lib #> abind 1.4-5 2016-07-21 [1] #> assertthat 0.2.0 2017-04-11 [1] #> backports 1.1.3 2018-12-14 [1] #> boot 1.3-20 2017-08-06 [2] #> broom 0.5.1.9000 2019-01-20 [1] #> broom.mixed 0.2.3.9000 2019-01-23 [1] #> callr 3.1.1 2018-12-21 [1] #> cli 1.0.1.9000 2019-01-20 [1] #> coda * 0.19-2 2018-10-08 [1] #> crayon 1.3.4 2017-09-16 [1] #> desc 1.2.0 2019-01-21 [1] #> devtools 2.0.1.9000 2019-01-29 [1] #> digest 0.6.18 2018-10-10 [1] #> dplyr 0.8.0 2019-01-31 [1] #> evaluate 0.12 2018-10-09 [1] #> fs 1.2.6 2018-08-23 [1] #> generics 0.0.2 2019-01-20 [1] #> glue 1.3.0 2018-07-17 [1] #> highr 0.7 2018-06-09 [1] #> htmltools 0.3.6 2017-04-28 [1] #> knitr 1.21 2018-12-10 [1] #> lattice 0.20-38 2018-11-04 [2] #> magrittr 1.5 2014-11-22 [1] #> Matrix 1.2-15 2018-11-01 [1] #> memoise 1.1.0 2017-04-21 [1] #> nlme 3.1-137 2018-04-07 [2] #> pillar 1.3.1 2018-12-15 [1] #> pkgbuild 1.0.2 2018-10-16 [1] #> pkgconfig 2.0.2 2018-08-16 [1] #> pkgload 1.0.2 2018-10-29 [1] #> plyr 1.8.4 2016-06-08 [1] #> prettyunits 1.0.2 2015-07-13 [1] #> processx 3.2.1 2018-12-05 [1] #> ps 1.3.0 2018-12-21 [1] #> purrr 0.3.0 2019-01-25 [1] #> R2jags * 0.5-7 2015-08-23 [1] #> R2WinBUGS 2.1-21 2015-07-30 [1] #> R6 2.3.0 2018-10-04 [1] #> Rcpp 1.0.0 2018-11-07 [1] #> remotes 2.0.2 2018-10-30 [1] #> reshape2 1.4.3 2017-12-11 [1] #> rjags * 4-8 2018-10-19 [1] #> rlang 0.3.1 2019-01-08 [1] #> rmarkdown 1.11 2018-12-08 [1] #> rprojroot 1.3-2 2018-01-03 [1] #> sessioninfo 1.1.1 2018-11-05 [1] #> stringi 1.2.4 2018-07-20 [1] #> stringr 1.3.1 2018-05-10 [1] #> testthat 2.0.1 2018-10-13 [1] #> tibble 2.0.1 2019-01-12 [1] #> tidyr 0.8.2 2018-10-28 [1] #> tidyselect 0.2.5 2018-10-11 [1] #> TMB 1.7.15 2018-11-09 [1] #> usethis 1.4.0.9000 2019-01-29 [1] #> withr 2.1.2 2018-03-15 [1] #> xfun 0.4 2018-10-23 [1] #> yaml 2.2.0 2018-07-25 [1] #> source #> CRAN (R 3.5.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> Github (tidymodels/broom@66411c4) #> Github (bbolker/broom.mixed@fbe9597) #> CRAN (R 3.6.0) #> Github (r-lib/cli@94e2fc5) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> Github (r-lib/desc@42b9578) #> Github (r-lib/devtools@e4e57aa) #> CRAN (R 3.5.1) #> Github (tidyverse/dplyr@67b9f4f) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> Github (r-lib/generics@d59e6b4) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> Github (tidyverse/purrr@240f7b2) #> CRAN (R 3.5.2) #> CRAN (R 3.5.2) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.2) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> Github (r-lib/usethis@5a0680b) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> #> [1] C:/Users/inp099/Documents/R/win-library/3.6 #> [2] C:/Program Files/R/R-devel/library ```
IndrajeetPatil commented 5 years ago

Another comment I had was that there is no example for rjags model in the documentation for broom.mixed::tidy.rjags. It will be helpful for the user to have an example here.

bbolker commented 5 years ago

fixed in 16b327937

IndrajeetPatil commented 5 years ago

Can confirm that this works as expected. Here is the reprex-

# setup
set.seed(123)
library(R2jags)
#> Loading required package: rjags
#> Loading required package: coda
#> Linked to JAGS 4.3.0
#> Loaded modules: basemod,bugs
#> 
#> Attaching package: 'R2jags'
#> The following object is masked from 'package:coda':
#> 
#>     traceplot

# An example model file is given in:
model.file <- system.file(package = "R2jags", "model", "schools.txt")

# Let's take a look:
file.show(model.file)

# data
J <- 8.0
y <- c(28.4, 7.9, -2.8, 6.8, -0.6, 0.6, 18.0, 12.2)
sd <- c(14.9, 10.2, 16.3, 11.0, 9.4, 11.4, 10.4, 17.6)

# setting up model
jags.data <- list("y", "sd", "J")
jags.params <- c("mu", "sigma", "theta")
jags.inits <- function() {
  list("mu" = rnorm(1), "sigma" = runif(1), "theta" = rnorm(J))
}

# model fitting
jagsfit <- R2jags::jags(
  data = list("y", "sd", "J"),
  inits = jags.inits,
  jags.params,
  n.iter = 10,
  model.file = model.file
)
#> module glm loaded
#> Compiling model graph
#>    Resolving undeclared variables
#>    Allocating nodes
#> Graph information:
#>    Observed stochastic nodes: 8
#>    Unobserved stochastic nodes: 10
#>    Total graph size: 41
#> 
#> Initializing model

# tidy
broom.mixed::tidy(jagsfit)
#> Registered S3 methods overwritten by 'broom.mixed':
#>   method         from 
#>   augment.lme    broom
#>   augment.merMod broom
#>   glance.lme     broom
#>   glance.merMod  broom
#>   glance.stanreg broom
#>   tidy.brmsfit   broom
#>   tidy.gamlss    broom
#>   tidy.lme       broom
#>   tidy.merMod    broom
#>   tidy.rjags     broom
#>   tidy.stanfit   broom
#>   tidy.stanreg   broom
#> # A tibble: 10 x 3
#>    term     estimate std.error
#>    <chr>       <dbl>     <dbl>
#>  1 mu        -0.771      1.37 
#>  2 sigma      0.764      0.535
#>  3 theta[1]  -0.211      1.62 
#>  4 theta[2]  -0.0995     1.65 
#>  5 theta[3]  -0.727      1.17 
#>  6 theta[4]  -1.62       1.24 
#>  7 theta[5]  -0.866      1.38 
#>  8 theta[6]  -0.472      1.70 
#>  9 theta[7]  -1.04       1.57 
#> 10 theta[8]  -0.746      1.34

# glance
broom.mixed::glance(jagsfit)
#> Error: No glance method for objects of class rjags

# augment
broom.mixed::augment(jagsfit)
#> Error: No augment method for objects of class rjags

Created on 2019-02-21 by the reprex package (v0.2.1.9000)

Session info ``` r devtools::session_info() #> - Session info ---------------------------------------------------------- #> setting value #> version R Under development (unstable) (2018-11-30 r75724) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.1252 #> ctype English_United States.1252 #> tz America/New_York #> date 2019-02-21 #> #> - Packages -------------------------------------------------------------- #> package * version date lib #> abind 1.4-5 2016-07-21 [1] #> assertthat 0.2.0 2017-04-11 [1] #> backports 1.1.3 2018-12-14 [1] #> boot 1.3-20 2017-08-06 [2] #> broom 0.5.1.9000 2019-01-20 [1] #> broom.mixed 0.2.4 2019-02-21 [1] #> callr 3.1.1 2018-12-21 [1] #> cli 1.0.1.9000 2019-01-20 [1] #> coda * 0.19-2 2018-10-08 [1] #> crayon 1.3.4 2017-09-16 [1] #> desc 1.2.0 2019-01-21 [1] #> devtools 2.0.1.9000 2019-02-18 [1] #> digest 0.6.18 2018-10-10 [1] #> dplyr 0.8.0.9000 2019-02-15 [1] #> evaluate 0.13 2019-02-12 [1] #> fansi 0.4.0 2018-11-05 [1] #> fs 1.2.6 2018-08-23 [1] #> generics 0.0.2 2019-01-20 [1] #> glue 1.3.0 2018-07-17 [1] #> highr 0.7 2018-06-09 [1] #> htmltools 0.3.6 2017-04-28 [1] #> knitr 1.21 2018-12-10 [1] #> lattice 0.20-38 2018-11-04 [2] #> magrittr 1.5 2014-11-22 [1] #> Matrix 1.2-15 2018-11-01 [1] #> memoise 1.1.0 2017-04-21 [1] #> nlme 3.1-137 2018-04-07 [2] #> pillar 1.3.1 2018-12-15 [1] #> pkgbuild 1.0.2 2018-10-16 [1] #> pkgconfig 2.0.2 2018-08-16 [1] #> pkgload 1.0.2 2018-10-29 [1] #> plyr 1.8.4 2016-06-08 [1] #> prettyunits 1.0.2 2015-07-13 [1] #> processx 3.2.1 2018-12-05 [1] #> ps 1.3.0 2018-12-21 [1] #> purrr 0.3.0 2019-01-27 [1] #> R2jags * 0.5-7 2015-08-23 [1] #> R2WinBUGS 2.1-21 2015-07-30 [1] #> R6 2.4.0 2019-02-14 [1] #> Rcpp 1.0.0 2018-11-07 [1] #> remotes 2.0.2 2018-10-30 [1] #> reshape2 1.4.3 2017-12-11 [1] #> rjags * 4-8 2018-10-19 [1] #> rlang 0.3.1 2019-01-08 [1] #> rmarkdown 1.11.6 2019-02-14 [1] #> rprojroot 1.3-2 2018-01-03 [1] #> sessioninfo 1.1.1 2018-11-05 [1] #> stringi 1.3.1 2019-02-13 [1] #> stringr 1.4.0 2019-02-10 [1] #> testthat 2.0.1 2018-10-13 [1] #> tibble 2.0.1 2019-01-12 [1] #> tidyr 0.8.2 2018-10-28 [1] #> tidyselect 0.2.5 2018-10-11 [1] #> TMB 1.7.15 2018-11-09 [1] #> usethis 1.4.0.9000 2019-02-18 [1] #> utf8 1.1.4 2018-05-24 [1] #> withr 2.1.2 2018-03-15 [1] #> xfun 0.5 2019-02-20 [1] #> yaml 2.2.0 2018-07-25 [1] #> source #> CRAN (R 3.5.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> Github (tidymodels/broom@66411c4) #> Github (bbolker/broom.mixed@b1d3c17) #> CRAN (R 3.6.0) #> Github (r-lib/cli@94e2fc5) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> Github (r-lib/desc@42b9578) #> Github (r-lib/devtools@188a613) #> CRAN (R 3.5.1) #> Github (tidyverse/dplyr@b541182) #> CRAN (R 3.6.0) #> Github (brodieG/fansi@ab11e9c) #> CRAN (R 3.5.1) #> Github (r-lib/generics@d59e6b4) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.2) #> CRAN (R 3.5.2) #> CRAN (R 3.5.2) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.2) #> CRAN (R 3.6.0) #> Github (rstudio/rmarkdown@bbd0786) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> Github (r-lib/usethis@ed9ae17) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> #> [1] C:/Users/inp099/Documents/R/win-library/3.6 #> [2] C:/Program Files/R/R-devel/library ```