Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glance method for gam fails: no 'nobs' method is available #737

Closed
IndrajeetPatil opened this issue Aug 10, 2019 · 7 comments
Closed

glance method for gam fails: no 'nobs' method is available #737

IndrajeetPatil opened this issue Aug 10, 2019 · 7 comments
Labels
beginner-friendly bug an unexpected problem or unintended behavior

Comments

@IndrajeetPatil
Copy link
Contributor

# setup
library(gamm4)
#> Loading required package: Matrix
#> Loading required package: lme4
#> Loading required package: mgcv
#> Loading required package: nlme
#> 
#> Attaching package: 'nlme'
#> The following object is masked from 'package:lme4':
#> 
#>     lmList
#> This is mgcv 1.8-28. For overview type 'help("mgcv-package")'.
#> This is gamm4 0.2-5
set.seed(123)

# data
dat <- gamSim(1, n = 400, scale = 2)
#> 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

# model object
br <- gamm4::gamm4(
  formula = y ~ s(x0) + x1 + s(x2),
  data = dat,
  random = ~ (1 | fac)
)

# checking class
class(br$gam)
#> [1] "gam"

# checking gam component
broom::tidy(br$gam)
#> # A tibble: 2 x 5
#>   term    edf ref.df statistic   p.value
#>   <chr> <dbl>  <dbl>     <dbl>     <dbl>
#> 1 s(x0)  3.64   3.64      11.6 3.65e-  8
#> 2 s(x2)  8.20   8.20      72.9 4.52e-104

# model summary
broom::glance(br$gam)
#> Error in nobs.default(x): no 'nobs' method is available

Traceback:

8. stop("no 'nobs' method is available") 
7. nobs.default(x) 
6. stats::nobs(x) 
5. eval_tidy(xs[[i]], unique_output) 
4. lst_quos(xs, transform = expand_lst) 
3. tibble(df = sum(x$edf), logLik = as.numeric(stats::logLik(x)), 
    AIC = stats::AIC(x), BIC = stats::BIC(x), deviance = stats::deviance(x), 
    df.residual = stats::df.residual(x), nobs = stats::nobs(x)) at mgcv-tidiers.R#91
2. glance.gam(br$gam) 
1. broom::glance(br$gam) 

Created on 2019-08-10 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> - Session info ----------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.6.1 (2019-07-05)
#>  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-08-10                  
#> 
#> - Packages --------------------------------------------------------------
#>  package     * version     date       lib
#>  assertthat    0.2.1       2019-03-21 [1]
#>  backports     1.1.4       2019-04-10 [1]
#>  boot          1.3-23      2019-07-05 [1]
#>  broom         0.5.2.9002  2019-08-08 [1]
#>  callr         3.3.1       2019-07-18 [1]
#>  cli           1.1.0       2019-03-19 [1]
#>  crayon        1.3.4       2017-09-16 [1]
#>  desc          1.2.0       2019-04-03 [1]
#>  devtools      2.1.0       2019-07-06 [1]
#>  digest        0.6.20      2019-07-04 [1]
#>  dplyr         0.8.3       2019-07-04 [1]
#>  evaluate      0.14        2019-05-28 [1]
#>  fansi         0.4.0       2018-11-05 [1]
#>  fs            1.3.1       2019-05-06 [1]
#>  gamm4       * 0.2-5       2017-07-25 [1]
#>  generics      0.0.2       2019-03-05 [1]
#>  glue          1.3.1       2019-03-12 [1]
#>  highr         0.8         2019-03-20 [1]
#>  htmltools     0.3.6       2017-04-28 [1]
#>  knitr         1.24        2019-08-08 [1]
#>  lattice       0.20-38     2018-11-04 [2]
#>  lifecycle     0.1.0       2019-08-01 [1]
#>  lme4        * 1.1-21      2019-03-05 [1]
#>  magrittr      1.5         2014-11-22 [1]
#>  MASS          7.3-51.4    2019-03-31 [1]
#>  Matrix      * 1.2-17      2019-03-22 [1]
#>  memoise       1.1.0       2017-04-21 [1]
#>  mgcv        * 1.8-28      2019-03-21 [1]
#>  minqa         1.2.4       2014-10-09 [1]
#>  nlme        * 3.1-140     2019-05-12 [2]
#>  nloptr        1.2.1       2018-10-03 [1]
#>  pillar        1.4.2       2019-06-29 [1]
#>  pkgbuild      1.0.4       2019-08-05 [1]
#>  pkgconfig     2.0.2       2018-08-16 [1]
#>  pkgload       1.0.2       2018-10-29 [1]
#>  prettyunits   1.0.2       2015-07-13 [1]
#>  processx      3.4.1       2019-07-18 [1]
#>  ps            1.3.0       2018-12-21 [1]
#>  purrr         0.3.2       2019-03-15 [1]
#>  R6            2.4.0       2019-02-14 [1]
#>  Rcpp          1.0.2       2019-07-25 [1]
#>  remotes       2.1.0       2019-06-24 [1]
#>  rlang         0.4.0       2019-06-25 [1]
#>  rmarkdown     1.14.3      2019-08-02 [1]
#>  rprojroot     1.3-2       2018-01-03 [1]
#>  sessioninfo   1.1.1       2018-11-05 [1]
#>  stringi       1.4.3       2019-03-12 [1]
#>  stringr       1.4.0       2019-02-10 [1]
#>  testthat      2.2.1       2019-07-25 [1]
#>  tibble        2.1.3       2019-06-06 [1]
#>  tidyr         0.8.99.9000 2019-08-10 [1]
#>  tidyselect    0.2.5       2018-10-11 [1]
#>  usethis       1.5.1.9000  2019-07-27 [1]
#>  utf8          1.1.4       2018-05-24 [1]
#>  vctrs         0.2.0       2019-07-05 [1]
#>  withr         2.1.2       2018-03-15 [1]
#>  xfun          0.8         2019-06-25 [1]
#>  yaml          2.2.0       2018-07-25 [1]
#>  zeallot       0.1.0       2018-01-28 [1]
#>  source                            
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.1)                    
#>  local                             
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  Github (r-lib/desc@c860e7b)       
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  Github (brodieG/fansi@ab11e9c)    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  Github (r-lib/generics@c15ac43)   
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  Github (rstudio/rmarkdown@37bcb8f)
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#>  Github (tidyverse/tidyr@ac90dfa)  
#>  CRAN (R 3.5.1)                    
#>  Github (r-lib/usethis@b241420)    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#> 
#> [1] C:/Users/inp099/Documents/R/win-library/3.6
#> [2] C:/Program Files/R/R-3.6.1/library
@alexpghayes alexpghayes added beginner-friendly bug an unexpected problem or unintended behavior labels Aug 10, 2019
@alexpghayes
Copy link
Collaborator

This might be a collation issue? Might need something like #' @include nobs.R to take advantage of nobs() methods from #597.

@PirateGrunt
Copy link
Contributor

I actually get a different error:

library(gamm4)
#> Loading required package: Matrix
#> Loading required package: lme4
#> Loading required package: mgcv
#> Loading required package: nlme
#> 
#> Attaching package: 'nlme'
#> The following object is masked from 'package:lme4':
#> 
#>     lmList
#> This is mgcv 1.8-28. For overview type 'help("mgcv-package")'.
#> This is gamm4 0.2-5
set.seed(123)

dat <- gamSim(1, n = 400, scale = 2)
#> Gu & Wahba 4 term additive model

dat$fac <- fac <- as.factor(sample(1:20, 400, replace = TRUE))
dat$y <- dat$y + model.matrix(~ fac - 1) %*% rnorm(20) * .5

br <- gamm4::gamm4(
  formula = y ~ s(x0) + x1 + s(x2),
  data = dat,
  random = ~ (1 | fac)
)

class(br$gam)
#> [1] "gam"

broom::tidy(br$gam)
#> # A tibble: 2 x 5
#>   term    edf ref.df statistic   p.value
#>   <chr> <dbl>  <dbl>     <dbl>     <dbl>
#> 1 s(x0)  3.64   3.64      11.6 3.65e-  8
#> 2 s(x2)  8.20   8.20      72.9 4.52e-104
broom::glance(br$gam)
#> Error in `$<-.data.frame`(`*tmp*`, "logLik", value = numeric(0)): replacement has 0 rows, data has 1

Created on 2019-09-12 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.1 (2019-07-05)
#>  os       Ubuntu 19.04                
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2019-09-12                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version  date       lib source        
#>  assertthat    0.2.1    2019-03-21 [1] CRAN (R 3.6.1)
#>  backports     1.1.4    2019-04-10 [1] CRAN (R 3.6.1)
#>  boot          1.3-23   2019-07-05 [5] CRAN (R 3.6.1)
#>  broom         0.5.2    2019-04-07 [1] CRAN (R 3.6.1)
#>  callr         3.3.1    2019-07-18 [1] CRAN (R 3.6.1)
#>  cli           1.1.0    2019-03-19 [1] CRAN (R 3.6.1)
#>  crayon        1.3.4    2017-09-16 [1] CRAN (R 3.6.1)
#>  desc          1.2.0    2018-05-01 [1] CRAN (R 3.6.1)
#>  devtools      2.1.0    2019-07-06 [1] CRAN (R 3.6.1)
#>  digest        0.6.20   2019-07-04 [1] CRAN (R 3.6.1)
#>  dplyr         0.8.3    2019-07-04 [1] CRAN (R 3.6.1)
#>  evaluate      0.14     2019-05-28 [1] CRAN (R 3.6.1)
#>  fansi         0.4.0    2018-10-05 [1] CRAN (R 3.6.1)
#>  fs            1.3.1    2019-05-06 [1] CRAN (R 3.6.1)
#>  gamm4       * 0.2-5    2017-07-25 [1] CRAN (R 3.6.1)
#>  generics      0.0.2    2018-11-29 [1] CRAN (R 3.6.1)
#>  glue          1.3.1    2019-03-12 [1] CRAN (R 3.6.1)
#>  highr         0.8      2019-03-20 [1] CRAN (R 3.6.1)
#>  htmltools     0.3.6    2017-04-28 [1] CRAN (R 3.6.1)
#>  knitr         1.24     2019-08-08 [1] CRAN (R 3.6.1)
#>  lattice       0.20-38  2018-11-04 [1] CRAN (R 3.6.1)
#>  lme4        * 1.1-21   2019-03-05 [1] CRAN (R 3.6.1)
#>  magrittr      1.5      2014-11-22 [1] CRAN (R 3.6.1)
#>  MASS          7.3-51.1 2018-11-01 [1] CRAN (R 3.6.1)
#>  Matrix      * 1.2-17   2019-03-22 [1] CRAN (R 3.6.1)
#>  memoise       1.1.0    2017-04-21 [1] CRAN (R 3.6.1)
#>  mgcv        * 1.8-28   2019-03-21 [1] CRAN (R 3.6.1)
#>  minqa         1.2.4    2014-10-09 [1] CRAN (R 3.6.1)
#>  nlme        * 3.1-140  2019-05-12 [1] CRAN (R 3.6.1)
#>  nloptr        1.2.1    2018-10-03 [1] CRAN (R 3.6.1)
#>  pillar        1.4.2    2019-06-29 [1] CRAN (R 3.6.1)
#>  pkgbuild      1.0.3    2019-03-20 [1] CRAN (R 3.6.1)
#>  pkgconfig     2.0.2    2018-08-16 [1] CRAN (R 3.6.1)
#>  pkgload       1.0.2    2018-10-29 [1] CRAN (R 3.6.1)
#>  plyr          1.8.4    2016-06-08 [1] CRAN (R 3.6.1)
#>  prettyunits   1.0.2    2015-07-13 [1] CRAN (R 3.6.1)
#>  processx      3.4.1    2019-07-18 [1] CRAN (R 3.6.1)
#>  ps            1.3.0    2018-12-21 [1] CRAN (R 3.6.1)
#>  purrr         0.3.2    2019-03-15 [1] CRAN (R 3.6.1)
#>  R6            2.4.0    2019-02-14 [1] CRAN (R 3.6.1)
#>  Rcpp          1.0.2    2019-07-25 [1] CRAN (R 3.6.1)
#>  remotes       2.1.0    2019-06-24 [1] CRAN (R 3.6.1)
#>  rlang         0.4.0    2019-06-25 [1] CRAN (R 3.6.1)
#>  rmarkdown     1.14     2019-07-12 [1] CRAN (R 3.6.1)
#>  rprojroot     1.3-2    2018-01-03 [1] CRAN (R 3.6.1)
#>  sessioninfo   1.1.1    2018-11-05 [1] CRAN (R 3.6.1)
#>  stringi       1.4.3    2019-03-12 [1] CRAN (R 3.6.1)
#>  stringr       1.4.0    2019-02-10 [1] CRAN (R 3.6.1)
#>  testthat      2.1.1    2019-04-23 [1] CRAN (R 3.6.1)
#>  tibble        2.1.3    2019-06-06 [1] CRAN (R 3.6.1)
#>  tidyr         0.8.3    2019-03-01 [1] CRAN (R 3.6.1)
#>  tidyselect    0.2.5    2018-10-11 [1] CRAN (R 3.6.1)
#>  usethis       1.5.1    2019-07-04 [1] CRAN (R 3.6.1)
#>  utf8          1.1.4    2018-05-24 [1] CRAN (R 3.6.1)
#>  vctrs         0.2.0    2019-07-05 [1] CRAN (R 3.6.1)
#>  withr         2.1.2    2018-03-15 [1] CRAN (R 3.6.1)
#>  xfun          0.8      2019-06-25 [1] CRAN (R 3.6.1)
#>  yaml          2.2.0    2018-07-25 [1] CRAN (R 3.6.1)
#>  zeallot       0.1.0    2018-01-28 [1] CRAN (R 3.6.1)
#> 
#> [1] /home/brian/Rlibs
#> [2] /home/brian/R/x86_64-pc-linux-gnu-library/3.6
#> [3] /usr/local/lib/R/site-library
#> [4] /usr/lib/R/site-library
#> [5] /usr/lib/R/library

@IndrajeetPatil
Copy link
Contributor Author

Maybe because you are using broom 0.5.2 , while I am using the development version (broom 0.5.2.9002)?

@PirateGrunt
Copy link
Contributor

D'oh! Forgot I was running the CRAN version. Updating to dev now. Sorry!

@PirateGrunt
Copy link
Contributor

There is no nobs.gam() method. I checked nobs.R in the package source and don't see one. One is not needed for mgcv::gam(), because those objects also have class lm and glm.

@vincentarelbundock
Copy link
Contributor

vincentarelbundock commented Jun 12, 2020

I think this issue should be closed. As @PirateGrunt noted, glance works perfectly fine for objects produced by mgcv::gam, which is the package that broom officially supports in mgcv-tidiers.R. The package gamm4 is not supported (yet?), and it confusingly produces objects with the same class name as mgcv.

If you want to add nobs, I recommend you define a new method as such, but note that this will still leave you without LogLik, AIC, etc.

library(gamm4)
library(mgcv)
library(broom)

dat <- gamSim(1, n = 400, scale = 2)
dat$fac <- fac <- as.factor(sample(1:20, 400, replace = TRUE))
dat$y <- dat$y + model.matrix(~ fac - 1) %*% rnorm(20) * .5

f1 <- y ~ s(x0) + x1 + s(x2)
f2 <- ~ (1 | fac)
mod1 <- mgcv::gam(formula = f1, data = dat, random = f2)
mod2 <- gamm4::gamm4(formula = f1, data = dat, random = f2)$gam

nobs.gam <- function(object) length(object$y)
glance(mod1)
#> # A tibble: 1 x 7
#>      df logLik   AIC   BIC deviance df.residual  nobs
#>   <dbl>  <dbl> <dbl> <dbl>    <dbl>       <dbl> <int>
#> 1  12.6  -872. 1772. 1826.    1836.        387.   400
glance(mod2)
#> # A tibble: 1 x 7
#>      df logLik   AIC   BIC deviance df.residual  nobs
#>   <dbl>  <dbl> <dbl> <dbl>    <dbl>       <dbl> <int>
#> 1  13.3     NA    NA    NA       NA        387.   400

Created on 2020-06-12 by the reprex package (v0.3.0)

@github-actions
Copy link

github-actions bot commented Mar 7, 2021

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
beginner-friendly bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants