GithubHelp home page GithubHelp logo

Compatibility with mgcv::gam() about broom HOT 21 CLOSED

ouzor avatar ouzor commented on August 20, 2024
Compatibility with mgcv::gam()

from broom.

Comments (21)

angoodkind avatar angoodkind commented on August 20, 2024 1

@gavinsimpson When I try to run it on mgcv::bam, I get:

> tidy(gam.rnn.0)
data frame with 0 columns and 0 rows
> glance(gam.rnn.0)
Error in `$<-.data.frame`(`*tmp*`, logLik, value = -1814706.9989743) : 
  replacement has 1 row, data has 0

from broom.

alexpghayes avatar alexpghayes commented on August 20, 2024 1

Your best bet is to write and contribute this code yourself if you can. Or it might be in https://gavinsimpson.github.io/gratia/ already, I haven't checked in a while.

from broom.

 avatar commented on August 20, 2024

Has there been an update on a linkage between broom and mgcv? broom has been a fantastic package, but this is the one that is missing for my analysis.

from broom.

gavinsimpson avatar gavinsimpson commented on August 20, 2024

This is going to be tricky but not impossible and I'll volunteer to write mgcv::gam methods for tidy(), augment() and glance(). One way to manage this would be to have broom:::tidy.gam() inspect x using some simple heuristics to decide which package, gam or mgcv, produced the model fit. Then add a meta class to the object (say mgcv_gam or gam_gam) and then dispatch to tidy() methods for those meta classes.

This would require moving the current code the tidy.gam() to tidy.gam_gam (or whatever meta class name was preferred). This way things should continue to work seamlessly not breaking backwards compatibility.

Thoughts? Suggestions for the meta class names?

from broom.

dgrtwo avatar dgrtwo commented on August 20, 2024

This would be great!

The only place we do something like this is tidy.list, which decides which function of tidy_optim and tidy_xyz (which are not exported) to use (see this issue for discussion). So we've used the pattern so far of having the S3 method make a decision of which non-S3 method to call.

Thus, perhaps the convention would be to have two non-exported functions called tidy_mgcv_gam or tidy_gam_gam, then:

tidy.gam <- function(x, ...) {
    # heuristics to figure out which it is, then ...
    if (is_from_mgcv) {
        return(tidy_mgcv_gam(x))
    } else {
        return(tidy_gam_gam(x))
    }
}

The docs are the last annoying part; perhaps ?gam_tidiers should be connected to tidy_gam_gam, but at the top specify it is just for gam::gam and link to ?mgcv_tidiers with the rest? I'd rather avoid gam_gam_tidiers since I am already tired of typing the syllables "gam-gam" this much (video), and since it preserves reverse compatibility of docs.

from broom.

gavinsimpson avatar gavinsimpson commented on August 20, 2024

I also suppose that a simpler alternative would be to stick the mgcv things in a separate pkg, importing selectively from broom. (There is bam() and gamm() in mgcv that we'd want methods for too but these don't conflict with gam the pkg.)

Whether what you propose is workable (I'm not very familiar with gam::gam() and the things it returns, whereas I am with mgcv::gam()) re documenting what the tidy etc methods returned. But probably best to implement something and then see how that looks/works.

from broom.

dgrtwo avatar dgrtwo commented on August 20, 2024

I'd rather all tidiers live either in broom or in the package alongside the objects its tidying. So I'd be happy for tidy.gam to be placed in mgcv, but don't think it's worth spinning off to "mgcvbroom".

tidy.gam is already written so if you write tidy_mgcv_gam (and augment etc as necessary) we'll be set!

from broom.

gavinsimpson avatar gavinsimpson commented on August 20, 2024

Cool; I'll start on that!

from broom.

hadley avatar hadley commented on August 20, 2024

👍

from broom.

lselzer avatar lselzer commented on August 20, 2024

@gavinsimpson Hey, has any work been done on this? If not, I'm willing to work on it.

from broom.

dgrtwo avatar dgrtwo commented on August 20, 2024

@lselzer I actually have most of this done! This is the push I needed to finish it up, thanks

from broom.

SimonGoring avatar SimonGoring commented on August 20, 2024

If this isn't fixed it would be really helpful to have the tidy.gam fail gracefully when an mgcv::gam is passed. I was really confused as to why my output was disappearing.

from broom.

anghyflawn avatar anghyflawn commented on August 20, 2024

Thanks for providing this! Would it be possible to also support the output of mgcv::bam()? This doesn't seem to work for me:

> library(broom)
> library(mgcv)
> sessionInfo()
R version 3.2.4 Revised (2016-03-16 r70336)
Platform: x86_64-apple-darwin12.5.0 (64-bit)
Running under: OS X 10.8.5 (Mountain Lion)

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

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

other attached packages:
[1] mgcv_1.8-14  nlme_3.1-125 broom_0.4.1 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.6     lattice_0.20-33 tidyr_0.6.0     psych_1.6.6    
 [5] dplyr_0.5.0     assertthat_0.1  grid_3.2.4      R6_2.1.3       
 [9] plyr_1.8.4      DBI_0.5         magrittr_1.5    stringi_1.1.1  
[13] lazyeval_0.2.0  reshape2_1.4.1  Matrix_1.2-4    tools_3.2.4    
[17] stringr_1.1.0   parallel_3.2.4  compiler_3.2.4  mnormt_1.5-4   
[21] tibble_1.2

> data(mtcars)

# This works

> fit <- gam(mpg ~ cyl + s(hp), data=mtcars)
> tidy(fit)
   term      edf   ref.df statistic    p.value
1 s(hp) 5.506492 6.601482  2.257316 0.06910746
> glance(fit)
        df    logLik      AIC      BIC deviance df.residual
1 7.506492 -72.95905 162.9311 175.3994 179.0741    24.49351

# But this doesn't

> fit <- bam(mpg ~ cyl + s(hp), data=mtcars)
> tidy(fit)
data frame with 0 columns and 0 rows
> glance(fit)
Error in `$<-.data.frame`(`*tmp*`, "logLik", value = -77.1853352336568) : 
  replacement has 1 row, data has 0

According to ?bam the value is a gam object so I'm guessing it shouldn't be to difficult to add this.

Thanks!

from broom.

roxannebeauclair avatar roxannebeauclair commented on August 20, 2024

Is there any plan to make tidy compatible with gamm() from mgcv?

from broom.

mebrooks avatar mebrooks commented on August 20, 2024

It seems that the method doesn't work for gam models with family = ziplss(). Here is an example

data(Owls, package="glmmTMB")
Owls = plyr::rename(Owls, c(SiblingNegotiation="NCalls"))
Owls = transform(Owls, ArrivalTime=scale(ArrivalTime, center=TRUE, scale=FALSE))
m1.gam = gam(list(NCalls ~ (FoodTreatment + ArrivalTime) * SexParent + offset(logBroodSize) + s(Nest, bs = "re"),
                   ~ 1), data = Owls, family = ziplss(), method = "REML")
tidy(m1.gam)

from broom.

angoodkind avatar angoodkind commented on August 20, 2024

Was this ever built in to either broom or mgcv/gamm4? If so, how do I call it?

from broom.

gavinsimpson avatar gavinsimpson commented on August 20, 2024

@angoodkind This was merged a couple of years ago, although as Mollie mentions above, this may not work for all of mgcv::gam models and related functions.

You ought to be able to just use tidy, glance etc as for any other model supported by broom.

from broom.

gavinsimpson avatar gavinsimpson commented on August 20, 2024

@angoodkind it wasn't clear to me that by "this" you meant bam(). No; as far as I can tell there is no support yet for bam() or for some of the newer families and features of mgcv. I'm happy to implement this stuff, but, as mentioned in #239, this would probably be more efficient if the code were ins a separate broom support package rather than the main package.

from broom.

angoodkind avatar angoodkind commented on August 20, 2024

@gavinsimpson. Thanks. I might take a crack at that, as well.

from broom.

timothyslau avatar timothyslau commented on August 20, 2024

3 years later... has there been tidy() support developed for bam()? outside or inside broom?

from broom.

github-actions avatar github-actions commented on August 20, 2024

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.

from broom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.