gavinsimpson / gratia

ggplot-based graphics and useful functions for GAMs fitted using the mgcv package
https://gavinsimpson.github.io/gratia/
Other
206 stars 28 forks source link

draw fails with ti(...) terms from smooth_estimates(...) #260

Closed wStockhausen closed 8 months ago

wStockhausen commented 8 months ago

The example from smooth_estimates(...) is:

load_mgcv()
# example data
df <- data_sim("eg1", seed = 21)
# fit GAM
m <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = df, method = "REML")
# plot all of the estimated smooths
sm <- smooth_estimates(m)
draw(sm)

When s(x0) is replaced by ti(X0), the example fails when draw'ing the sm object:

load_mgcv()
# example data
df <- data_sim("eg1", seed = 21)
# fit GAM
m <- gam(y ~ ti(x0) + s(x1) + s(x2) + s(x3), data = df, method = "REML")
# plot all of the estimated smooths
sm <- smooth_estimates(m)
draw(sm)

with error message: Error in wrap_plots(): ! Only know how to add and/or objects Run rlang::last_trace() to see where the error occurred.

gavinsimpson commented 8 months ago

Thanks for the bug report, @wStockhausen. I wasn't anticipating univariate tensor products when I was building plot methods internally in the package. These univariate tensor products are now given their own unique type (to distinguish them in the outputs that these are univariate) but are plotted like standard mgcv smooths:

> smooth_estimates(m_univar_ti)
# A tibble: 100 × 6
   .smooth .type                  .by   .estimate   .se      x2
   <chr>   <chr>                  <chr>     <dbl> <dbl>   <dbl>
 1 ti(x2)  1d Tensor product int. NA       -4.58  0.614 0.00164
 2 ti(x2)  1d Tensor product int. NA       -4.11  0.575 0.0117 
 3 ti(x2)  1d Tensor product int. NA       -3.65  0.536 0.0218 
 4 ti(x2)  1d Tensor product int. NA       -3.18  0.499 0.0318 
 5 ti(x2)  1d Tensor product int. NA       -2.72  0.463 0.0419 
 6 ti(x2)  1d Tensor product int. NA       -2.26  0.429 0.0520 
 7 ti(x2)  1d Tensor product int. NA       -1.81  0.397 0.0621 
 8 ti(x2)  1d Tensor product int. NA       -1.37  0.368 0.0721 
 9 ti(x2)  1d Tensor product int. NA       -0.934 0.341 0.0822 
10 ti(x2)  1d Tensor product int. NA       -0.508 0.318 0.0923 
# ℹ 90 more rows
# ℹ Use `print(n = ...)` to see more rows

This change is live now in the GitHub development version of gratia so you can install from there with

remotes::install_github("gavinsimpson/gratia")

if you are able to build from source. If not, I'd expect my r-universe to have a binary build ready in the next few hours and to install from there use

install.packages("gratia", repos = c(
  "https://gavinsimpson.r-universe.dev",
  "https://cloud.r-project.org"
))

And a version that fixes this should be on CRAN before the end of the month if all goes well.

wStockhausen commented 8 months ago

Wow! Thanks for the instant response. BTW, love the package.

William T. Stockhausen

Research Fishery Biologist, Alaska Fisheries Science Center

NOAA Fisheries | U.S. Department of Commerce

Office: (206) 526-4241

www.fisheries.noaa.gov

On Mon, Mar 11, 2024 at 2:09 PM Gavin Simpson @.***> wrote:

Thanks for the bug report, @wStockhausen https://github.com/wStockhausen. I wasn't anticipating univariate tensor products when I was building plot methods internally in the package. These univariate tensor products are now given their own unique type (to distinguish them in the outputs that these are univariate) but are plotted like standard mgcv smooths:

smooth_estimates(m_univar_ti)

A tibble: 100 × 6

.smooth .type .by .estimate .se x2

1 ti(x2) 1d Tensor product int. NA -4.58 0.614 0.00164 2 ti(x2) 1d Tensor product int. NA -4.11 0.575 0.0117 3 ti(x2) 1d Tensor product int. NA -3.65 0.536 0.0218 4 ti(x2) 1d Tensor product int. NA -3.18 0.499 0.0318 5 ti(x2) 1d Tensor product int. NA -2.72 0.463 0.0419 6 ti(x2) 1d Tensor product int. NA -2.26 0.429 0.0520 7 ti(x2) 1d Tensor product int. NA -1.81 0.397 0.0621 8 ti(x2) 1d Tensor product int. NA -1.37 0.368 0.0721 9 ti(x2) 1d Tensor product int. NA -0.934 0.341 0.0822 10 ti(x2) 1d Tensor product int. NA -0.508 0.318 0.0923 # ℹ 90 more rows # ℹ Use `print(n = ...)` to see more rows

This change is live now in the GitHub development version of gratia so you can install from there with

remotes::install_github("gavinsimpson/gratia")

if you are able to build from source. If not, I'd expect my r-universe to have a binary build ready in the next few hours and to install from there use

install.packages("gratia", repos = c( "https://gavinsimpson.r-universe.dev", "https://cloud.r-project.org" ))

And a version that fixes this should be on CRAN before the end of the month if all goes well.

— Reply to this email directly, view it on GitHub https://github.com/gavinsimpson/gratia/issues/260#issuecomment-1989448493, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMAD5AD5VKFLTEEPK6UJRDYXYMPXAVCNFSM6AAAAABEQYBKSCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOBZGQ2DQNBZGM . You are receiving this because you were mentioned.Message ID: @.***>