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

data_slice isn't handling logical variables when not specified in the slice #222

Closed gavinsimpson closed 1 year ago

gavinsimpson commented 1 year ago
library("gratia")
library("mgcv")
library("dplyr")

dat <- data_sim("eg1", n = 1500, dist = "normal", scale = 20, seed = 3) |>
    mutate(fac = as.factor( sample(c("A1", "A2", "A3"), 1500, replace = TRUE) ),
           logi = as.logical( sample(c(TRUE, FALSE), 1500, replace = TRUE) ))
b <- gam(y ~ x0 + x1 + I(x1^2) + s(x2, bs = "cr", k = 12) + fac + x3:fac + I(x1*x2) + logi,
         data = dat)

# get a data slice where only `fac` varies
# hold all other variables are representative values
ds <- data_slice(b, fac = evenly(fac)) 

Note that in ds, logi has been coerced to a double:

> ds                                                                          
# A tibble: 3 × 6
  fac      x0    x1    x2  logi    x3
  <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A1    0.511 0.521 0.493     0 0.476
2 A2    0.511 0.521 0.493     0 0.476
3 A3    0.511 0.521 0.493     0 0.476

The issue is in typical_values()